-1

Person A holds a bitcoin and he wants to transfer it to Person B. This transaction will have a input which holds the unlocking script and output which holds the locking script. So in the transaction's locking scrip(ScriptPubkey) a public key has to be added. My understanding is this locking script will contain Person B's public key. But how will person A get person B's public key to create the transaction?

Mukesh Kumar
  • 302
  • 1
  • 2
  • 9
  • As the tag says **general Bitcoin questions belong on https://bitcoin.stackexchange.com** but briefly: since about 2010 most bitcoin transactions use an _address_ which is a _hash_ of the publickey, called P2PKH (Pay to Public Key Hash) or since 2017 often P2WPKH (Pay to Witness Public Key Hash, part of a scheme called 'segwit' to make bitcoin block processing more efficient). People/systems can provide or distribute their addresses in lots of ways, often as encoded strings that look like 1asdfasdfasdfasdf or bc1asdfasdfasdfasdfasdf. – dave_thompson_085 Oct 26 '21 at 08:03

1 Answers1

-1

As dave_thompson_085 has pointed out, most bitcoin transactions do not actually contain the public key as part of an output script and instead contain the hash of the public key or the hash of a script. There is an old scheme known as P2PK where the public key is part of the output script but this is almost never used today in favour of P2PKH, P2WPKH and soon P2TR.

As for communicating the required information to create a transaction, an address is used to encode the hash of the public key or script with different output script variations using different encoding variations to indicate what output script should be used.

Here are some of the encoding schemes for common locking scripts:

  • P2PKH uses Base58 check encoding where the encoded data consists of a version prefix of 0x00 for mainnet followed by the Hash160 of the receiving public key. This results in an address starting with the character 1.
  • P2SH uses Base58 check encoding where the encoded data consists of a version prefix of 0x05 for mainnet followed by the Hash160 of the redeem script. This results in an address starting with the character 3.
  • P2WPKH uses Bech32 encoding where the encoded data consists of a witness version followed by the Hash160 of the public key. The witness version for segwit outputs is 0 and results in the address starting with bc1q.
  • P2WSH uses Bech32 encoding where the encoded data consists of a witness version followed by the SHA256 of the script. Because P2WSH uses SHA256 as opposed to Hash160, the address is longer than P2WPKH addresses. The address also starts with bc1q.
  • P2TR uses Bech32m encoding where the encoded data consists of the witness version followed by the tweaked key unhashed. The witness version here is 1 and results in the address bc1p.

If your question is regarding the actual transmission of Person B's address to Person A, there is no official method to do this. However if you want to check if an address received is valid, the checksum at the end of each encoded address can be used. For addresses encoded in Base58, the checksum is the last 4 characters and for Bech32 it is the last 6 characters. Bech32 also has error correcting capability as per [BIP-0173][1]
nlanson
  • 357
  • 1
  • 3
  • 14