@btc-vision/transaction
Version:
OPNet transaction library allows you to create and sign transactions for the OPNet network.
237 lines (236 loc) • 10.4 kB
JavaScript
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _Address_p2tr, _Address_p2op, _Address_network, _Address_originalPublicKey, _Address_keyPair, _Address_uncompressed, _Address_tweakedUncompressed;
import { decompressPublicKey, toXOnly } from '@btc-vision/bitcoin';
import { ADDRESS_BYTE_LENGTH } from '../utils/lengths.js';
import { AddressVerificator } from './AddressVerificator.js';
import { EcKeyPair } from './EcKeyPair.js';
import { ContractAddress } from '../transaction/ContractAddress.js';
import { BitcoinUtils } from '../utils/BitcoinUtils.js';
export class Address extends Uint8Array {
constructor(bytes) {
super(ADDRESS_BYTE_LENGTH);
_Address_p2tr.set(this, void 0);
_Address_p2op.set(this, void 0);
_Address_network.set(this, void 0);
_Address_originalPublicKey.set(this, void 0);
_Address_keyPair.set(this, void 0);
_Address_uncompressed.set(this, void 0);
_Address_tweakedUncompressed.set(this, void 0);
if (!bytes) {
return;
}
this.set(bytes);
}
get originalPublicKey() {
return __classPrivateFieldGet(this, _Address_originalPublicKey, "f");
}
get keyPair() {
if (!__classPrivateFieldGet(this, _Address_keyPair, "f")) {
throw new Error('Public key not set for address');
}
return __classPrivateFieldGet(this, _Address_keyPair, "f");
}
static dead() {
return Address.fromString('0x04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f');
}
static zero() {
return new Address();
}
static fromString(pubKey) {
if (!pubKey) {
throw new Error('Invalid public key');
}
if (pubKey.startsWith('0x')) {
pubKey = pubKey.slice(2);
}
if (!BitcoinUtils.isValidHex(pubKey)) {
throw new Error('You must only pass public keys in hexadecimal format. If you have an address such as bc1q... you must convert it to a public key first. Please refer to await provider.getPublicKeyInfo("bc1q..."). If the public key associated with the address is not found, you must force the user to enter the destination public key. It looks like: 0x020373626d317ae8788ce3280b491068610d840c23ecb64c14075bbb9f670af52c.');
}
return new Address(Buffer.from(pubKey, 'hex'));
}
static wrap(bytes) {
return new Address(bytes);
}
static uncompressedToCompressed(publicKey) {
const buffer = Uint8Array.from(publicKey);
const x = buffer.slice(1, 33);
const y = buffer.slice(33);
const compressed = Buffer.alloc(33);
compressed[0] = 0x02 + (y[y.length - 1] & 0x01);
compressed.set(x, 1);
return compressed;
}
toHex() {
return '0x' + Buffer.from(this).toString('hex');
}
toBuffer() {
return Buffer.from(this);
}
toUncompressedHex() {
if (!__classPrivateFieldGet(this, _Address_uncompressed, "f")) {
throw new Error('Public key not set');
}
return '0x' + __classPrivateFieldGet(this, _Address_uncompressed, "f").uncompressed.toString('hex');
}
toUncompressedBuffer() {
if (!__classPrivateFieldGet(this, _Address_uncompressed, "f")) {
throw new Error('Public key not set');
}
return __classPrivateFieldGet(this, _Address_uncompressed, "f").uncompressed;
}
toHybridPublicKeyHex() {
if (!__classPrivateFieldGet(this, _Address_uncompressed, "f")) {
throw new Error('Public key not set');
}
return '0x' + __classPrivateFieldGet(this, _Address_uncompressed, "f").hybrid.toString('hex');
}
toHybridPublicKeyBuffer() {
if (!__classPrivateFieldGet(this, _Address_uncompressed, "f")) {
throw new Error('Public key not set');
}
return __classPrivateFieldGet(this, _Address_uncompressed, "f").hybrid;
}
originalPublicKeyBuffer() {
if (!__classPrivateFieldGet(this, _Address_originalPublicKey, "f")) {
throw new Error('Public key not set');
}
return Buffer.from(__classPrivateFieldGet(this, _Address_originalPublicKey, "f"));
}
equals(a) {
const b = this;
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < b.length; i++) {
if (b[i] !== a[i]) {
return false;
}
}
return true;
}
lessThan(a) {
const b = this;
for (let i = 0; i < ADDRESS_BYTE_LENGTH; i++) {
const thisByte = b[i];
const aByte = a[i];
if (thisByte < aByte) {
return true;
}
else if (thisByte > aByte) {
return false;
}
}
return false;
}
greaterThan(a) {
const b = this;
for (let i = 0; i < ADDRESS_BYTE_LENGTH; i++) {
const thisByte = b[i];
const aByte = a[i];
if (thisByte > aByte) {
return true;
}
else if (thisByte < aByte) {
return false;
}
}
return false;
}
set(publicKey) {
const validLengths = [ADDRESS_BYTE_LENGTH, 33, 65];
if (!validLengths.includes(publicKey.length)) {
throw new Error(`Invalid public key length ${publicKey.length}`);
}
if (publicKey.length === ADDRESS_BYTE_LENGTH) {
const buf = Buffer.alloc(ADDRESS_BYTE_LENGTH);
buf.set(publicKey);
__classPrivateFieldSet(this, _Address_tweakedUncompressed, ContractAddress.generateHybridKeyFromHash(buf), "f");
super.set(publicKey);
}
else {
this.autoFormat(publicKey);
}
}
isValid(network) {
return AddressVerificator.isValidPublicKey(Buffer.from(this).toString('hex'), network);
}
p2pk() {
return this.toHex();
}
p2wpkh(network) {
return EcKeyPair.getP2WPKHAddress(this.keyPair, network);
}
p2pkh(network) {
return EcKeyPair.getLegacyAddress(this.keyPair, network);
}
p2shp2wpkh(network) {
return EcKeyPair.getLegacySegwitAddress(this.keyPair, network);
}
toString() {
return this.toHex();
}
toJSON() {
return this.toHex();
}
p2tr(network) {
if (__classPrivateFieldGet(this, _Address_p2tr, "f") && __classPrivateFieldGet(this, _Address_network, "f") === network) {
return __classPrivateFieldGet(this, _Address_p2tr, "f");
}
const p2trAddy = EcKeyPair.tweakedPubKeyBufferToAddress(this, network);
if (p2trAddy) {
__classPrivateFieldSet(this, _Address_network, network, "f");
__classPrivateFieldSet(this, _Address_p2tr, p2trAddy, "f");
return p2trAddy;
}
throw new Error('Public key not set');
}
p2op(network) {
if (__classPrivateFieldGet(this, _Address_p2op, "f") && __classPrivateFieldGet(this, _Address_network, "f") === network) {
return __classPrivateFieldGet(this, _Address_p2op, "f");
}
const p2opAddy = EcKeyPair.p2op(this, network);
if (p2opAddy) {
__classPrivateFieldSet(this, _Address_network, network, "f");
__classPrivateFieldSet(this, _Address_p2op, p2opAddy, "f");
return p2opAddy;
}
throw new Error('Public key not set');
}
toTweakedHybridPublicKeyHex() {
if (!__classPrivateFieldGet(this, _Address_tweakedUncompressed, "f")) {
throw new Error('Public key not set');
}
return '0x' + __classPrivateFieldGet(this, _Address_tweakedUncompressed, "f").toString('hex');
}
toTweakedHybridPublicKeyBuffer() {
if (!__classPrivateFieldGet(this, _Address_tweakedUncompressed, "f")) {
throw new Error('Public key not set');
}
return __classPrivateFieldGet(this, _Address_tweakedUncompressed, "f");
}
autoFormat(publicKey) {
const firstByte = publicKey[0];
if (firstByte === 0x03 || firstByte === 0x02) {
}
else if (firstByte === 0x04 || firstByte === 0x06 || firstByte === 0x07) {
publicKey = Address.uncompressedToCompressed(publicKey);
}
__classPrivateFieldSet(this, _Address_originalPublicKey, Uint8Array.from(publicKey), "f");
__classPrivateFieldSet(this, _Address_keyPair, EcKeyPair.fromPublicKey(__classPrivateFieldGet(this, _Address_originalPublicKey, "f")), "f");
__classPrivateFieldSet(this, _Address_uncompressed, decompressPublicKey(__classPrivateFieldGet(this, _Address_originalPublicKey, "f")), "f");
const tweakedBytes = toXOnly(EcKeyPair.tweakPublicKey(Buffer.from(__classPrivateFieldGet(this, _Address_originalPublicKey, "f"))));
__classPrivateFieldSet(this, _Address_tweakedUncompressed, ContractAddress.generateHybridKeyFromHash(tweakedBytes), "f");
super.set(tweakedBytes);
}
}
_Address_p2tr = new WeakMap(), _Address_p2op = new WeakMap(), _Address_network = new WeakMap(), _Address_originalPublicKey = new WeakMap(), _Address_keyPair = new WeakMap(), _Address_uncompressed = new WeakMap(), _Address_tweakedUncompressed = new WeakMap();