@atomiqlabs/sdk 8.6.7 → 8.6.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -5,10 +5,12 @@ const utils_1 = require("@scure/btc-signer/utils");
|
|
|
5
5
|
const btc_signer_1 = require("@scure/btc-signer");
|
|
6
6
|
const buffer_1 = require("buffer");
|
|
7
7
|
const BitcoinWallet_1 = require("./BitcoinWallet");
|
|
8
|
+
const base_1 = require("@atomiqlabs/base");
|
|
8
9
|
const bip32_1 = require("@scure/bip32");
|
|
9
10
|
const bip39_1 = require("@scure/bip39");
|
|
10
11
|
const english_js_1 = require("@scure/bip39/wordlists/english.js");
|
|
11
12
|
const sha2_1 = require("@noble/hashes/sha2");
|
|
13
|
+
const logger = (0, base_1.getLogger)("SingleAddressBitcoinWallet: ");
|
|
12
14
|
/**
|
|
13
15
|
* Bitcoin wallet implementation deriving a single address from a WIF encoded private key
|
|
14
16
|
*
|
|
@@ -32,12 +34,24 @@ class SingleAddressBitcoinWallet extends BitcoinWallet_1.BitcoinWallet {
|
|
|
32
34
|
if (address == null)
|
|
33
35
|
throw new Error("Failed to generate p2wpkh address from the provided private key!");
|
|
34
36
|
this.address = address;
|
|
37
|
+
this.addressType = (0, BitcoinWallet_1.identifyAddressType)(this.address, network);
|
|
35
38
|
}
|
|
36
39
|
else {
|
|
37
40
|
this.address = addressDataOrWIF.address;
|
|
41
|
+
this.addressType = (0, BitcoinWallet_1.identifyAddressType)(this.address, network);
|
|
38
42
|
this.pubkey = buffer_1.Buffer.from(addressDataOrWIF.publicKey, "hex");
|
|
43
|
+
// Some wallets seem to be returning a full 33-byte compressed pubkey instead of a taproot
|
|
44
|
+
// 32-byte long X-only key. Handle these cases here
|
|
45
|
+
if (this.addressType === "p2tr") {
|
|
46
|
+
if (this.pubkey.length !== 33)
|
|
47
|
+
return;
|
|
48
|
+
const leadingByte = this.pubkey[0];
|
|
49
|
+
if (leadingByte !== 0x03 && leadingByte !== 0x02)
|
|
50
|
+
throw new Error("Invalid public key passed for taproot bitcoin wallet, expected an X-only 32-byte public key, or a compressed 33-byte public key");
|
|
51
|
+
logger.debug(`constructor(): Converting compressed public key ${addressDataOrWIF.publicKey} to taproot X-only 32-byte public key`);
|
|
52
|
+
this.pubkey = this.pubkey.slice(1);
|
|
53
|
+
}
|
|
39
54
|
}
|
|
40
|
-
this.addressType = (0, BitcoinWallet_1.identifyAddressType)(this.address, network);
|
|
41
55
|
}
|
|
42
56
|
/**
|
|
43
57
|
* Returns all the wallet addresses controlled by the wallet
|
package/package.json
CHANGED
|
@@ -3,12 +3,14 @@ import {BTC_NETWORK, NETWORK, pubECDSA, randomPrivateKeyBytes, TEST_NETWORK} fro
|
|
|
3
3
|
import {getAddress, Transaction, WIF} from "@scure/btc-signer";
|
|
4
4
|
import {Buffer} from "buffer";
|
|
5
5
|
import {identifyAddressType, BitcoinWallet} from "./BitcoinWallet";
|
|
6
|
-
import {BitcoinNetwork, BitcoinRpcWithAddressIndex} from "@atomiqlabs/base";
|
|
6
|
+
import {BitcoinNetwork, BitcoinRpcWithAddressIndex, getLogger} from "@atomiqlabs/base";
|
|
7
7
|
import {HDKey} from "@scure/bip32";
|
|
8
8
|
import {entropyToMnemonic, generateMnemonic, mnemonicToSeed} from "@scure/bip39";
|
|
9
9
|
import {wordlist} from "@scure/bip39/wordlists/english.js";
|
|
10
10
|
import {sha256} from "@noble/hashes/sha2";
|
|
11
11
|
|
|
12
|
+
const logger = getLogger("SingleAddressBitcoinWallet: ");
|
|
13
|
+
|
|
12
14
|
/**
|
|
13
15
|
* Bitcoin wallet implementation deriving a single address from a WIF encoded private key
|
|
14
16
|
*
|
|
@@ -42,11 +44,21 @@ export class SingleAddressBitcoinWallet extends BitcoinWallet {
|
|
|
42
44
|
const address = getAddress("wpkh", this.privKey, network);
|
|
43
45
|
if(address==null) throw new Error("Failed to generate p2wpkh address from the provided private key!");
|
|
44
46
|
this.address = address;
|
|
47
|
+
this.addressType = identifyAddressType(this.address, network);
|
|
45
48
|
} else {
|
|
46
49
|
this.address = addressDataOrWIF.address;
|
|
50
|
+
this.addressType = identifyAddressType(this.address, network);
|
|
47
51
|
this.pubkey = Buffer.from(addressDataOrWIF.publicKey, "hex");
|
|
52
|
+
// Some wallets seem to be returning a full 33-byte compressed pubkey instead of a taproot
|
|
53
|
+
// 32-byte long X-only key. Handle these cases here
|
|
54
|
+
if(this.addressType==="p2tr") {
|
|
55
|
+
if(this.pubkey.length!==33) return;
|
|
56
|
+
const leadingByte = this.pubkey[0];
|
|
57
|
+
if(leadingByte!==0x03 && leadingByte!==0x02) throw new Error("Invalid public key passed for taproot bitcoin wallet, expected an X-only 32-byte public key, or a compressed 33-byte public key");
|
|
58
|
+
logger.debug(`constructor(): Converting compressed public key ${addressDataOrWIF.publicKey} to taproot X-only 32-byte public key`);
|
|
59
|
+
this.pubkey = this.pubkey.slice(1);
|
|
60
|
+
}
|
|
48
61
|
}
|
|
49
|
-
this.addressType = identifyAddressType(this.address, network);
|
|
50
62
|
}
|
|
51
63
|
|
|
52
64
|
/**
|