bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.71k stars 2.11k forks source link

Function to determine address type by address or redeemScript #2102

Closed cpuchainorg closed 5 months ago

cpuchainorg commented 5 months ago

Would like to know if there is a reference function to determine address type from script or address string

I have a code like this but it doesn't cover every address types

function getScriptType(script) {
    if (script[0] === bitcoin.opcodes.OP_1 && script[1] === 32) {
        return 'taproot'
    }

    if (script[0] == bitcoin.opcodes.OP_0 && script[1] == 20) {
        return 'bech32'
    }

    if (script[0] == bitcoin.opcodes.OP_HASH160 && script[1] == 20) {
        return 'segwit'
    }

    if (
        script[0] == bitcoin.opcodes.OP_DUP &&
        script[1] == bitcoin.opcodes.OP_HASH160 &&
        script[2] == 20
    ) {
        return 'legacy'
    }
}
jasonandjay commented 5 months ago

We have encapsulated these methods

I think this is enough You can visit here https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/ts_src/psbt/psbtutils.ts#L18-L24