@btc-vision/transaction
Version:
OPNet transaction library allows you to create and sign transactions for the OPNet network.
29 lines • 1.2 kB
JavaScript
import { bech32, bech32m } from 'bech32';
import { ripemd160 } from '@btc-vision/bitcoin';
export class AddressGenerator {
// Generate a valid SegWit address from random bytes
static generatePKSH(sha256Hash, network) {
if (sha256Hash.length !== 32)
throw new Error('Invalid hash length');
const pkh = ripemd160(sha256Hash);
return this.toSegwitAddress(pkh, network);
}
// Generate a valid Taproot address from a public key
static generateTaprootAddress(pubKey, network) {
if (pubKey.length !== 32)
throw new Error('Invalid public key length');
// Convert the public key to words
const words = bech32m.toWords(pubKey);
// Prepend the witness version (0x01 for Taproot)
words.unshift(0x01);
// Encode using Bech32m
return bech32m.encode(network.bech32, words);
}
// Convert a hash to a SegWit address
static toSegwitAddress(pkh, network) {
const words = bech32.toWords(pkh);
words.unshift(0x00); // Add the witness version byte (0x00 for P2WPKH)
return bech32.encode(network.bech32, words);
}
}
//# sourceMappingURL=AddressGenerator.js.map