Is it possible to convert a p2pkh address to a p2sh address?
Also, will the balance of the p2pkh address carry over to the p2sh address?
Bitcoin Stack Exchange is a question and answer site for Bitcoin users, developers, and enthusiasts. It only takes a minute to sign up.Sign up to join this community
Is it possible to covert a p2pkh adrress to a p2sh adress?
Addresses are determined by the wallet. It's the receiver's wallet saying "I will accept payment when it arrives at address X". Sending it to another address may mean the wallet doesn't recognize it. Worst case, if the receiver has some hardware security module that stores the key, it may literally be impossible to even recover the funds when sent to the wrong address.
This is no different from owing a friend some money, and "repaying" them by burying an envelope with cash in their lawn. You may technically consider that a transfer of funds to them, but if it's not done in a matter known or even recognizable by them, it's very hard to consider it a payment.
As a sender, you shouldn't care about what type of address the receiver gives you, and certainly not try to guess how you can modify it.
As a receiver, if you want to give out a P2SH address (presumably because you want multisig security or segwit?), just upgrade your wallet to software that supports said feature and create a new address.
Also will the balance of the p2pkh address carry over to p2sh address?
No, addresses don't have balances.
While I agree with Pieter that you generally should not do this, there is some technical understanding to be gained by knowing how this could be done. The following is for reference:
A P2PKH address A is created from the public key K by the following:
A = BASE58CHECK( 0x00 HASH160( K ) )
Where HASH160( K ) is equivalent to RIPEMD160( SHA256( K ) ) and 0x00 represents the version byte (for P2PKH)
With P2SH addresses, instead of hashing the public key, you hash a transaction script.
A 1 of 1 multisig transaction is created by concatenating the following byte values:
OP_0 0x14 HASH160( K )
Therefore, to create a
P2SH address which utilizes a 1 to 1 multisig transaction, you construct it as follows:
BASE58CHECK( 0x05 HASH160( OP_0 0x14 HASH160( K ) ) )
Where 0x05 is the version byte for script hash addresses and 0x14 is the amount of bytes to expect in the HASH160( K ).