question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Decoding pubkey addresses

See original GitHub issue

I was trying to get an address from an outputscript using address.fromOutputScript; but when I try it with a pubkey script (OP_DATA() + OP_CHECKSIG), the library fails with the error:

02397df78c9c3fecbcbcee12025230fc4b30e6e617f5fc9c10513eebd0cf4b327d OP_CHECKSIG has no matching Address

Inspecting the library code I found the problem here: https://github.com/bitcoinjs/bitcoinjs-lib/blob/413495b101e6dec80b4a310a52ab1a28cd9f7bec/src/address.js#L51 the pubkey template is not used. I know that pubkey addresses are deprecated, but they are used on coinbase transactions. How should I do to decode these type of addresses?

My solution is this, but it seems too tricky:

let s = bitcoinjs.script.pubKey.output.decode(txout.script);
s = bitcoinjs.crypto.sha256(s);
s = bitcoinjs.crypto.ripemd160(s);
spendableby = bitcoinjs.address.toBase58Check(bitcoinjs.script.compile(s), _network.pubKeyHash);

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
dcousenscommented, Feb 1, 2018
  • <> represents data or script
  • {} represents data that must prefixed by OP_PUSHDATA
  • [] represents multiple {}

Standard Scripts

PubKey (pay-to-pubkey / P2PK)

Address: N/A
scriptPubKey: {pubKey} OP_CHECKSIG
scriptSig: {signature}

PubKeyHash (pay-to-pubkeyhash / P2PKH)

Address: Base58(0x00 <hash160 pubKey> <checksum>)
scriptPubKey: OP_DUP OP_HASH160 {hash160(pubKey)} OP_EQUALVERIFY OP_CHECKSIG
scriptSig: {signature} {pubKey}

ScriptHash (pay-to-scripthash / P2SH)

Address: Base58(0x05 <hash160 script> <checksum>)
scriptPubKey: OP_HASH160 {hash160(scriptPubKey2)} OP_EQUAL
scriptSig: [scriptSig2 ...] {scriptPubKey2}

MultiSig (pay-to-multisig / P2MS)

Address: N/A
scriptPubKey: m [pubKeys ...] n OP_CHECKMULTISIG
scriptSig: OP_0 [signatures ...]

Witness PubKeyHash (pay-to-witness-pubkeyhash / P2WPKH)

Address: Bech32('bc' {hash160(pubKey)})
scriptPubKey: 0 {hash160(pubKey)}
scriptSig: (empty)
witness: {signature} {pubKey}

Witness ScriptHash (pay-to-witness-scripthash / P2WSH)

Address: Bech32('bc' {sha256(script)})
scriptPubKey: 0 {sha256(scriptPubKey2)}
scriptSig: (empty)
witness: [scriptSig2 ...] {scriptPubKey2}

Non-standard Scripts

OP_RETURN

Address: N/A
scriptPubKey: OP_RETURN {data}
scriptSig: N/A

Anyone-can-spend

Address: N/A
scriptPubKey: N/A
scriptSig: OP_TRUE

Transaction-puzzle

Address: N/A
scriptPubKey: OP_HASH256 {hash} OP_EQUAL
scriptSig: <data>

Ref https://gist.github.com/dcousens/1d8c24d01e3f34bee453

2reactions
dakkcommented, Feb 1, 2018

ok got it

Read more comments on GitHub >

github_iconTop Results From Across the Web

4. Keys, Addresses - Mastering Bitcoin, 2nd Edition [Book]
We will review the various encoding formats used to represent private and ... The relationship between private key, public key, and bitcoin address...
Read more >
Address - Learn Me A Bitcoin
Decoded : When someone creates a locking script from this address, they just decode the base58 to retrieve the hash160 inside it, then...
Read more >
What are public keys, private keys and wallet addresses?
Understanding addresses, public keys, and private keys is critical to understanding how cryptocurrencies work. A wallet address is a randomly generated set ...
Read more >
Is it possible to get the public key of a bitcoin address I do not ...
You can retrieve the public key from address with the reference client using the validateaddress RPC call (or in the debug window of...
Read more >
How public and private key encryption works - PreVeil
In public key cryptography, every public key matches to only one private key. Together, they are used to encrypt and decrypt messages. If...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found