How to generate bitcoin P2SH WIF key and address pair from sha256 private key?

2

3

lets say i have sha256 Private Key Hexadecimal Format

0000000000000000000000000000000000000000000000000000000000000001

how can i get P2SH address and its corresponding WIF private KEY ?

by P2SH i mean P2SH-P2WPKH address starting with 3. are these called 1-of-1 multisig address ?

AMB

Posted 2017-10-15T06:00:48.530

Reputation: 292

Answers

2

It's the same b58check(HASH160), but you're hashing a script.

The script for 1-to-1 multisig P2SH addresses is OP_0 hash160(pubkey).

So your address would be b58check(hash160('\x00\x14' + hash160(pubkey))).

Long story short:

1) WIF to pubkey:

secret = b58check_decode(WIF)
pubkey = ECKey(secret).pubkey

2) pubkey to P2PKH (legacy address):

addrtype = 0
addr = b58check(addrtype, hash160(pubkey))

3) pubkey to P2SH (1-to-1 multisig address):

addrtype = 5
addr = b58check(addrtype, hash160('\x00\x14' + hash160(pubkey)))    

where hash160(x) = ripemd160(sha256(x))

That's, basically, it. Also see https://github.com/joric/pywallet/issues/1

Regarding 32-byte secret to WIF conversion - bitcoin core doesn't tell apart different WIF's it's just b58check(secret) type 0x80. Importprivkey command generates 3 different addresses - P2PKH, P2SH and BECH32 from the same secret, and you can reexport-reimport the secret anytime.

Joric

Posted 2017-10-15T06:00:48.530

Reputation: 31

1

Private keys give public keys, which can give bitcoin addresses starting with a 1. But a P2SH address (starting with a 3) is simply the hash of a script, and that script could be almost anything, it isn't usually associated with a single public/private key. Perhaps you mean P2SH-P2WPKH? In that case you could read something like this: https://bitcoincore.org/en/segwit_wallet_dev/ (specifically the section called 'Creation of P2SH-P2WPKH Address'). Otherwise maybe something like a 1-of-1 multisig address or simple P2SH encoding a normal P2PK/P2PKH redeem script? I'll update this answer if you clarify exactly what you mean :)

MeshCollider

Posted 2017-10-15T06:00:48.530

Reputation: 8 735

1-of-1 multisig address.AMB 2017-10-15T11:36:48.973

Please update your answer on how to compute the P2SH address (starting with a 3) from scriptPubKey.Jus12 2017-12-16T16:04:54.310

@Jus12 that is not what the question asked. If you have a new question, please ask it separately :)MeshCollider 2017-12-16T19:31:58.793

0

next to bitcoin.org, adresses are structured this way: https://en.bitcoin.it/wiki/Protocol_specification#Addresses

and a detailed step-by-step explanation: https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses

This allowed me to understand more detailed: http://www.righto.com/2014/02/bitcoins-hard-way-using-raw-bitcoin.html

I usually use this site to verify results: http://gobittest.appspot.com/Address

pebwindkraft

Posted 2017-10-15T06:00:48.530

Reputation: 4 568

ooops, forgot the bitcoin bible:-) http://chimera.labs.oreilly.com/books/1234000001802/ch04.html#private_public_keys

pebwindkraft 2017-10-15T08:51:03.987

oh, so it is P2SH and multisig: this would be the starting point: http://www.soroushjp.com/2014/12/20/bitcoin-multisig-the-hard-way-understanding-raw-multisignature-bitcoin-transactions/

pebwindkraft 2017-10-15T21:23:23.427