UNPKG

@exodus/bip322-js

Version:

A Javascript library that provides utility functions related to the BIP-322 signature scheme

120 lines (119 loc) 4.95 kB
import * as bitcoin from '@exodus/bitcoinjs'; class Address { static isP2PKH(address) { if (address[0] === '1' || address[0] === 'm' || address[0] === 'n') { return true; } return false; } static isP2SH(address) { return address[0] === '3' || address[0] === '2'; } static isP2WPKH(address) { if (address.slice(0, 4) === 'bc1q' || address.slice(0, 4) === 'tb1q') { const scriptPubKey = this.convertAdressToScriptPubkey(address); if (scriptPubKey.byteLength === 22) { return true; } return false; } return false; } static isP2TR(address) { return address.slice(0, 4) === 'bc1p' || address.slice(0, 4) === 'tb1p'; } static isP2WPKHWitness(witness) { return (witness.length === 2 && witness[1].byteLength === 33 && (witness[1][0] === 0x02 || witness[1][0] === 0x03)); } static isSingleKeyP2TRWitness(witness) { return witness.length === 1; } static convertAdressToScriptPubkey(address) { if (address[0] === '1' || address[0] === 'm' || address[0] === 'n') { return bitcoin.payments.p2pkh({ address, network: address[0] === '1' ? bitcoin.networks.bitcoin : bitcoin.networks.testnet, }).output; } if (address[0] === '3' || address[0] === '2') { return bitcoin.payments.p2sh({ address, network: address[0] === '3' ? bitcoin.networks.bitcoin : bitcoin.networks.testnet, }).output; } if (address.slice(0, 4) === 'bc1q' || address.slice(0, 4) === 'tb1q') { if (address.length === 42) { return bitcoin.payments.p2wpkh({ address, network: address.slice(0, 4) === 'bc1q' ? bitcoin.networks.bitcoin : bitcoin.networks.testnet, }).output; } if (address.length === 62) { return bitcoin.payments.p2wsh({ address, network: address.slice(0, 4) === 'bc1q' ? bitcoin.networks.bitcoin : bitcoin.networks.testnet, }).output; } } else if ((address.slice(0, 4) === 'bc1p' || address.slice(0, 4) === 'tb1p') && address.length === 62) { return bitcoin.payments.p2tr({ address, network: address.slice(0, 4) === 'bc1p' ? bitcoin.networks.bitcoin : bitcoin.networks.testnet, }).output; } throw new Error('Unknown address type'); } static convertPubKeyIntoAddress(publicKey, addressType) { switch (addressType) { case 'p2pkh': return { mainnet: bitcoin.payments.p2pkh({ pubkey: publicKey, network: bitcoin.networks.bitcoin }) .address, testnet: bitcoin.payments.p2pkh({ pubkey: publicKey, network: bitcoin.networks.testnet }) .address, }; case 'p2sh-p2wpkh': return { mainnet: bitcoin.payments.p2sh({ redeem: bitcoin.payments.p2wpkh({ pubkey: publicKey, network: bitcoin.networks.bitcoin, }), network: bitcoin.networks.bitcoin, }).address, testnet: bitcoin.payments.p2sh({ redeem: bitcoin.payments.p2wpkh({ pubkey: publicKey, network: bitcoin.networks.testnet, }), network: bitcoin.networks.testnet, }).address, }; case 'p2wpkh': return { mainnet: bitcoin.payments.p2wpkh({ pubkey: publicKey, network: bitcoin.networks.bitcoin }) .address, testnet: bitcoin.payments.p2wpkh({ pubkey: publicKey, network: bitcoin.networks.testnet }) .address, }; case 'p2tr': const internalPubkey = publicKey.byteLength === 33 ? publicKey.subarray(1, 33) : publicKey; return { mainnet: bitcoin.payments.p2tr({ internalPubkey, network: bitcoin.networks.bitcoin, }).address, testnet: bitcoin.payments.p2tr({ internalPubkey, network: bitcoin.networks.testnet, }).address, }; default: throw new Error('Cannot convert public key into unsupported address type.'); } } } export default Address;