@arklabs/wallet-sdk
Version:
Bitcoin wallet SDK with Taproot and Ark integration
46 lines (45 loc) • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArkAddress = void 0;
const base_1 = require("@scure/base");
const btc_signer_1 = require("@scure/btc-signer");
// ArkAddress is a bech32m encoded address with a custom HRP (ark/tark)
class ArkAddress {
constructor(serverPubKey, tweakedPubKey, hrp) {
this.serverPubKey = serverPubKey;
this.tweakedPubKey = tweakedPubKey;
this.hrp = hrp;
if (serverPubKey.length !== 32) {
throw new Error("Invalid server public key length");
}
if (tweakedPubKey.length !== 32) {
throw new Error("Invalid tweaked public key length");
}
}
static decode(address) {
const decoded = base_1.bech32m.decodeUnsafe(address, 1023);
if (!decoded) {
throw new Error("Invalid address");
}
const data = new Uint8Array(base_1.bech32m.fromWords(decoded.words));
// First 32 bytes are server pubkey, next 32 bytes are tweaked pubkey
if (data.length !== 64) {
throw new Error("Invalid data length");
}
const serverPubKey = data.slice(0, 32);
const tweakedPubKey = data.slice(32, 64);
return new ArkAddress(serverPubKey, tweakedPubKey, decoded.prefix);
}
encode() {
// Combine server pubkey and tweaked pubkey
const data = new Uint8Array(64);
data.set(this.serverPubKey, 0);
data.set(this.tweakedPubKey, 32);
const words = base_1.bech32m.toWords(data);
return base_1.bech32m.encode(this.hrp, words, 1023);
}
get pkScript() {
return btc_signer_1.Script.encode(["OP_1", this.tweakedPubKey]);
}
}
exports.ArkAddress = ArkAddress;