bitcoinjs/bitcoinjs-lib

Function to determine address type by address or redeemScript

cpuchainorg opened this issue · 1 comments

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'
	}
}

We have encapsulated these methods

  • isP2MS,
  • isP2PK,
  • isP2PKH,
  • isP2WPKH,
  • isP2WSHScript,
  • isP2SHScript,
  • isP2TR

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