@btc-vision/transaction
Version:
OPNet transaction library allows you to create and sign transactions for the OPNet network.
25 lines (24 loc) • 894 B
JavaScript
import { bech32, bech32m } from 'bech32';
import { initEccLib, ripemd160 } from '@btc-vision/bitcoin';
import * as ecc from '@bitcoinerlab/secp256k1';
initEccLib(ecc);
export class AddressGenerator {
static generatePKSH(sha256Hash, network) {
if (sha256Hash.length !== 32)
throw new Error('Invalid hash length');
const pkh = ripemd160(sha256Hash);
return this.toSegwitAddress(pkh, network);
}
static generateTaprootAddress(pubKey, network) {
if (pubKey.length !== 32)
throw new Error('Invalid public key length');
const words = bech32m.toWords(pubKey);
words.unshift(0x01);
return bech32m.encode(network.bech32, words);
}
static toSegwitAddress(pkh, network) {
const words = bech32.toWords(pkh);
words.unshift(0x00);
return bech32.encode(network.bech32, words);
}
}