@exodus/bip322-js 1.1.0 → 1.2.1
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.
- package/CHANGELOG.md +61 -0
- package/README.md +13 -13
- package/dist/BIP322.d.ts +2 -33
- package/dist/BIP322.js +23 -103
- package/dist/BIP322.js.map +1 -0
- package/dist/Signer.d.ts +1 -20
- package/dist/Signer.js +32 -97
- package/dist/Signer.js.map +1 -0
- package/dist/Verifier.d.ts +0 -39
- package/dist/Verifier.js +60 -175
- package/dist/Verifier.js.map +1 -0
- package/dist/bitcoinjs/DecodeScriptSignature.d.ts +0 -1
- package/dist/bitcoinjs/DecodeScriptSignature.js +1 -7
- package/dist/bitcoinjs/DecodeScriptSignature.js.map +1 -0
- package/dist/bitcoinjs/index.d.ts +1 -2
- package/dist/bitcoinjs/index.js +1 -5
- package/dist/bitcoinjs/index.js.map +1 -0
- package/dist/helpers/Address.d.ts +2 -49
- package/dist/helpers/Address.js +57 -156
- package/dist/helpers/Address.js.map +1 -0
- package/dist/helpers/BIP137.d.ts +0 -21
- package/dist/helpers/BIP137.js +6 -61
- package/dist/helpers/BIP137.js.map +1 -0
- package/dist/helpers/VarInt.d.ts +0 -17
- package/dist/helpers/VarInt.js +11 -40
- package/dist/helpers/VarInt.js.map +1 -0
- package/dist/helpers/VarStr.d.ts +0 -17
- package/dist/helpers/VarStr.js +5 -32
- package/dist/helpers/VarStr.js.map +1 -0
- package/dist/helpers/Witness.d.ts +0 -18
- package/dist/helpers/Witness.js +10 -47
- package/dist/helpers/Witness.js.map +1 -0
- package/dist/helpers/index.d.ts +5 -6
- package/dist/helpers/index.js +5 -16
- package/dist/helpers/index.js.map +1 -0
- package/dist/index.d.ts +5 -5
- package/dist/index.js +5 -17
- package/dist/index.js.map +1 -0
- package/package.json +53 -44
package/dist/helpers/Address.js
CHANGED
|
@@ -1,219 +1,120 @@
|
|
|
1
|
-
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
// Import dependency
|
|
27
|
-
const bitcoin = __importStar(require("@exodus/bitcoinjs-lib"));
|
|
28
|
-
/**
|
|
29
|
-
* Class that implement address-related utility functions.
|
|
30
|
-
*/
|
|
1
|
+
import * as bitcoin from '@exodus/bitcoinjs';
|
|
31
2
|
class Address {
|
|
32
|
-
/**
|
|
33
|
-
* Check if a given Bitcoin address is a pay-to-public-key-hash (p2pkh) address.
|
|
34
|
-
* @param address Bitcoin address to be checked
|
|
35
|
-
* @returns True if the provided address correspond to a valid P2PKH address, false if otherwise
|
|
36
|
-
*/
|
|
37
3
|
static isP2PKH(address) {
|
|
38
|
-
// Check if the provided address is a P2PKH address
|
|
39
4
|
if (address[0] === '1' || address[0] === 'm' || address[0] === 'n') {
|
|
40
|
-
return true;
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
return false;
|
|
5
|
+
return true;
|
|
44
6
|
}
|
|
7
|
+
return false;
|
|
45
8
|
}
|
|
46
|
-
/**
|
|
47
|
-
* Check if a given Bitcoin address is a pay-to-script-hash (P2SH) address.
|
|
48
|
-
* @param address Bitcoin address to be checked
|
|
49
|
-
* @returns True if the provided address correspond to a valid P2SH address, false if otherwise
|
|
50
|
-
*/
|
|
51
9
|
static isP2SH(address) {
|
|
52
|
-
|
|
53
|
-
if (address[0] === '3' || address[0] === '2') {
|
|
54
|
-
return true; // P2SH address
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
10
|
+
return address[0] === '3' || address[0] === '2';
|
|
59
11
|
}
|
|
60
|
-
/**
|
|
61
|
-
* Check if a given Bitcoin address is a pay-to-witness-public-key-hash (P2WPKH) address.
|
|
62
|
-
* @param address Bitcoin address to be checked
|
|
63
|
-
* @returns True if the provided address correspond to a valid P2WPKH address, false if otherwise
|
|
64
|
-
*/
|
|
65
12
|
static isP2WPKH(address) {
|
|
66
|
-
// Check if the provided address is a P2WPKH/P2WSH address
|
|
67
13
|
if (address.slice(0, 4) === 'bc1q' || address.slice(0, 4) === 'tb1q') {
|
|
68
|
-
// Either a P2WPKH / P2WSH address
|
|
69
|
-
// Convert the address into a scriptPubKey
|
|
70
14
|
const scriptPubKey = this.convertAdressToScriptPubkey(address);
|
|
71
|
-
// Check if the scriptPubKey is exactly 22 bytes since P2WPKH scriptPubKey should be 0014<20-BYTE-PUBKEY-HASH>
|
|
72
15
|
if (scriptPubKey.byteLength === 22) {
|
|
73
|
-
return true;
|
|
16
|
+
return true;
|
|
74
17
|
}
|
|
75
|
-
else {
|
|
76
|
-
return false; // Not P2WPKH, probably P2WSH
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
18
|
return false;
|
|
81
19
|
}
|
|
20
|
+
return false;
|
|
82
21
|
}
|
|
83
|
-
/**
|
|
84
|
-
* Check if a given Bitcoin address is a taproot address.
|
|
85
|
-
* @param address Bitcoin address to be checked
|
|
86
|
-
* @returns True if the provided address is a taproot address, false if otherwise
|
|
87
|
-
*/
|
|
88
22
|
static isP2TR(address) {
|
|
89
|
-
|
|
90
|
-
return true; // P2TR address
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
return false;
|
|
94
|
-
}
|
|
23
|
+
return address.slice(0, 4) === 'bc1p' || address.slice(0, 4) === 'tb1p';
|
|
95
24
|
}
|
|
96
|
-
/**
|
|
97
|
-
* Check if a given witness stack corresponds to a P2WPKH address.
|
|
98
|
-
* @param witness Witness data associated with the toSign BIP-322 transaction
|
|
99
|
-
* @returns True if the provided witness stack correspond to a valid P2WPKH address, false if otherwise
|
|
100
|
-
*/
|
|
101
25
|
static isP2WPKHWitness(witness) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
return true;
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
26
|
+
return (witness.length === 2 &&
|
|
27
|
+
witness[1].byteLength === 33 &&
|
|
28
|
+
(witness[1][0] === 0x02 || witness[1][0] === 0x03));
|
|
110
29
|
}
|
|
111
|
-
/**
|
|
112
|
-
* Check if a given witness stack corresponds to a single-key-spend P2TR address.
|
|
113
|
-
* @param witness Witness data associated with the toSign BIP-322 transaction
|
|
114
|
-
* @returns True if the provided address and witness stack correspond to a valid single-key-spend P2TR address, false if otherwise
|
|
115
|
-
*/
|
|
116
30
|
static isSingleKeyP2TRWitness(witness) {
|
|
117
|
-
|
|
118
|
-
// It should contain exactly one items which is the signature for the transaction
|
|
119
|
-
if (witness.length === 1) {
|
|
120
|
-
return true;
|
|
121
|
-
}
|
|
122
|
-
else {
|
|
123
|
-
return false;
|
|
124
|
-
}
|
|
31
|
+
return witness.length === 1;
|
|
125
32
|
}
|
|
126
|
-
/**
|
|
127
|
-
* Convert a given Bitcoin address into its corresponding script public key.
|
|
128
|
-
* Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/script.py#L607
|
|
129
|
-
* @param address Bitcoin address
|
|
130
|
-
* @returns Script public key of the given Bitcoin address
|
|
131
|
-
* @throws Error when the provided address is not a valid Bitcoin address
|
|
132
|
-
*/
|
|
133
33
|
static convertAdressToScriptPubkey(address) {
|
|
134
34
|
if (address[0] === '1' || address[0] === 'm' || address[0] === 'n') {
|
|
135
|
-
// P2PKH address
|
|
136
35
|
return bitcoin.payments.p2pkh({
|
|
137
|
-
address
|
|
138
|
-
network:
|
|
36
|
+
address,
|
|
37
|
+
network: address[0] === '1' ? bitcoin.networks.bitcoin : bitcoin.networks.testnet,
|
|
139
38
|
}).output;
|
|
140
39
|
}
|
|
141
|
-
|
|
142
|
-
// P2SH address
|
|
40
|
+
if (address[0] === '3' || address[0] === '2') {
|
|
143
41
|
return bitcoin.payments.p2sh({
|
|
144
|
-
address
|
|
145
|
-
network:
|
|
42
|
+
address,
|
|
43
|
+
network: address[0] === '3' ? bitcoin.networks.bitcoin : bitcoin.networks.testnet,
|
|
146
44
|
}).output;
|
|
147
45
|
}
|
|
148
|
-
|
|
149
|
-
// P2WPKH or P2WSH address
|
|
46
|
+
if (address.slice(0, 4) === 'bc1q' || address.slice(0, 4) === 'tb1q') {
|
|
150
47
|
if (address.length === 42) {
|
|
151
|
-
// P2WPKH address
|
|
152
48
|
return bitcoin.payments.p2wpkh({
|
|
153
|
-
address
|
|
154
|
-
network:
|
|
49
|
+
address,
|
|
50
|
+
network: address.slice(0, 4) === 'bc1q' ? bitcoin.networks.bitcoin : bitcoin.networks.testnet,
|
|
155
51
|
}).output;
|
|
156
52
|
}
|
|
157
|
-
|
|
158
|
-
// P2WSH address
|
|
53
|
+
if (address.length === 62) {
|
|
159
54
|
return bitcoin.payments.p2wsh({
|
|
160
|
-
address
|
|
161
|
-
network:
|
|
55
|
+
address,
|
|
56
|
+
network: address.slice(0, 4) === 'bc1q' ? bitcoin.networks.bitcoin : bitcoin.networks.testnet,
|
|
162
57
|
}).output;
|
|
163
58
|
}
|
|
164
59
|
}
|
|
165
|
-
else if (address.slice(0, 4) === 'bc1p' || address.slice(0, 4) === 'tb1p')
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}).output;
|
|
172
|
-
}
|
|
60
|
+
else if ((address.slice(0, 4) === 'bc1p' || address.slice(0, 4) === 'tb1p') &&
|
|
61
|
+
address.length === 62) {
|
|
62
|
+
return bitcoin.payments.p2tr({
|
|
63
|
+
address,
|
|
64
|
+
network: address.slice(0, 4) === 'bc1p' ? bitcoin.networks.bitcoin : bitcoin.networks.testnet,
|
|
65
|
+
}).output;
|
|
173
66
|
}
|
|
174
|
-
throw new Error(
|
|
67
|
+
throw new Error('Unknown address type');
|
|
175
68
|
}
|
|
176
|
-
/**
|
|
177
|
-
* Convert a given public key into a corresponding Bitcoin address.
|
|
178
|
-
* @param publicKey Public key for deriving the address, or internal public key for deriving taproot address
|
|
179
|
-
* @param addressType Bitcoin address type to be derived, must be either 'p2pkh', 'p2sh-p2wpkh', 'p2wpkh', or 'p2tr'
|
|
180
|
-
* @returns Bitcoin address that correspond to the given public key in both mainnet and testnet
|
|
181
|
-
*/
|
|
182
69
|
static convertPubKeyIntoAddress(publicKey, addressType) {
|
|
183
70
|
switch (addressType) {
|
|
184
71
|
case 'p2pkh':
|
|
185
72
|
return {
|
|
186
|
-
mainnet: bitcoin.payments.p2pkh({ pubkey: publicKey, network: bitcoin.networks.bitcoin })
|
|
187
|
-
|
|
73
|
+
mainnet: bitcoin.payments.p2pkh({ pubkey: publicKey, network: bitcoin.networks.bitcoin })
|
|
74
|
+
.address,
|
|
75
|
+
testnet: bitcoin.payments.p2pkh({ pubkey: publicKey, network: bitcoin.networks.testnet })
|
|
76
|
+
.address,
|
|
188
77
|
};
|
|
189
78
|
case 'p2sh-p2wpkh':
|
|
190
|
-
// Reference: https://github.com/bitcoinjs/bitcoinjs-lib/blob/1a9119b53bcea4b83a6aa8b948f0e6370209b1b4/test/integration/addresses.spec.ts#L70
|
|
191
79
|
return {
|
|
192
80
|
mainnet: bitcoin.payments.p2sh({
|
|
193
|
-
redeem: bitcoin.payments.p2wpkh({
|
|
194
|
-
|
|
81
|
+
redeem: bitcoin.payments.p2wpkh({
|
|
82
|
+
pubkey: publicKey,
|
|
83
|
+
network: bitcoin.networks.bitcoin,
|
|
84
|
+
}),
|
|
85
|
+
network: bitcoin.networks.bitcoin,
|
|
195
86
|
}).address,
|
|
196
87
|
testnet: bitcoin.payments.p2sh({
|
|
197
|
-
redeem: bitcoin.payments.p2wpkh({
|
|
198
|
-
|
|
199
|
-
|
|
88
|
+
redeem: bitcoin.payments.p2wpkh({
|
|
89
|
+
pubkey: publicKey,
|
|
90
|
+
network: bitcoin.networks.testnet,
|
|
91
|
+
}),
|
|
92
|
+
network: bitcoin.networks.testnet,
|
|
93
|
+
}).address,
|
|
200
94
|
};
|
|
201
95
|
case 'p2wpkh':
|
|
202
96
|
return {
|
|
203
|
-
mainnet: bitcoin.payments.p2wpkh({ pubkey: publicKey, network: bitcoin.networks.bitcoin })
|
|
204
|
-
|
|
97
|
+
mainnet: bitcoin.payments.p2wpkh({ pubkey: publicKey, network: bitcoin.networks.bitcoin })
|
|
98
|
+
.address,
|
|
99
|
+
testnet: bitcoin.payments.p2wpkh({ pubkey: publicKey, network: bitcoin.networks.testnet })
|
|
100
|
+
.address,
|
|
205
101
|
};
|
|
206
102
|
case 'p2tr':
|
|
207
|
-
// Convert full-length public key into internal public key if necessary
|
|
208
103
|
const internalPubkey = publicKey.byteLength === 33 ? publicKey.subarray(1, 33) : publicKey;
|
|
209
104
|
return {
|
|
210
|
-
mainnet: bitcoin.payments.p2tr({
|
|
211
|
-
|
|
105
|
+
mainnet: bitcoin.payments.p2tr({
|
|
106
|
+
internalPubkey,
|
|
107
|
+
network: bitcoin.networks.bitcoin,
|
|
108
|
+
}).address,
|
|
109
|
+
testnet: bitcoin.payments.p2tr({
|
|
110
|
+
internalPubkey,
|
|
111
|
+
network: bitcoin.networks.testnet,
|
|
112
|
+
}).address,
|
|
212
113
|
};
|
|
213
114
|
default:
|
|
214
115
|
throw new Error('Cannot convert public key into unsupported address type.');
|
|
215
116
|
}
|
|
216
117
|
}
|
|
217
118
|
}
|
|
218
|
-
|
|
119
|
+
export default Address;
|
|
219
120
|
//# sourceMappingURL=Address.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Address.js","sourceRoot":"","sources":["../../src/helpers/Address.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,OAAO,MAAM,mBAAmB,CAAA;AAK5C,MAAM,OAAO;IAMJ,MAAM,CAAC,OAAO,CAAC,OAAe;QAGnC,IAAI,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE,CAAC;YACnE,OAAO,IAAI,CAAA;QACb,CAAC;QAED,OAAO,KAAK,CAAA;IACd,CAAC;IAOM,MAAM,CAAC,MAAM,CAAC,OAAe;QAElC,OAAO,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG,IAAI,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG,CAAA;IACjD,CAAC;IAOM,MAAM,CAAC,QAAQ,CAAC,OAAe;QAEpC,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,MAAM,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,MAAM,EAAE,CAAC;YAGrE,MAAM,YAAY,GAAG,IAAI,CAAC,2BAA2B,CAAC,OAAO,CAAC,CAAA;YAG9D,IAAI,YAAY,CAAC,UAAU,KAAK,EAAE,EAAE,CAAC;gBACnC,OA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|
|
2
|
-
/**
|
|
3
|
-
* Class that implement BIP137-related utility functions.
|
|
4
|
-
*/
|
|
5
1
|
declare class BIP137 {
|
|
6
|
-
/**
|
|
7
|
-
* Check if a given signature satisified the format of a BIP-137 signature.
|
|
8
|
-
* @param signature Base64-encoded signature to be checked
|
|
9
|
-
* @returns True if the provided signature correspond to a valid BIP-137 signature, false if otherwise
|
|
10
|
-
*/
|
|
11
2
|
static isBIP137Signature(signature: string): boolean;
|
|
12
|
-
/**
|
|
13
|
-
* Derive the public key that signed a valid BIP-137 signature.
|
|
14
|
-
* @param message Message signed by the signature
|
|
15
|
-
* @param signature Base-64 encoded signature to be decoded
|
|
16
|
-
* @returns Public key that signs the provided signature
|
|
17
|
-
*/
|
|
18
3
|
static derivePubKey(message: string, signature: string): Buffer;
|
|
19
|
-
/**
|
|
20
|
-
* Decode a BIP-137 signature.
|
|
21
|
-
* Function copied from bitcoinjs-message library.
|
|
22
|
-
* @param signature BIP-137 signature to be decoded
|
|
23
|
-
* @returns Decoded BIP-137 signature
|
|
24
|
-
*/
|
|
25
4
|
private static decodeSignature;
|
|
26
5
|
}
|
|
27
6
|
export default BIP137;
|
package/dist/helpers/BIP137.js
CHANGED
|
@@ -1,71 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
// Import dependencies
|
|
27
|
-
const secp256k1_1 = require("@exodus/secp256k1");
|
|
28
|
-
const bitcoinMessage = __importStar(require("bitcoinjs-message"));
|
|
29
|
-
/**
|
|
30
|
-
* Class that implement BIP137-related utility functions.
|
|
31
|
-
*/
|
|
1
|
+
import secp256k1 from '@exodus/secp256k1';
|
|
2
|
+
import * as bitcoinMessage from 'bitcoinjs-message';
|
|
32
3
|
class BIP137 {
|
|
33
|
-
/**
|
|
34
|
-
* Check if a given signature satisified the format of a BIP-137 signature.
|
|
35
|
-
* @param signature Base64-encoded signature to be checked
|
|
36
|
-
* @returns True if the provided signature correspond to a valid BIP-137 signature, false if otherwise
|
|
37
|
-
*/
|
|
38
4
|
static isBIP137Signature(signature) {
|
|
39
|
-
// Check if the provided signature satisified the format of a BIP-137 signature
|
|
40
5
|
const signatureBuffer = Buffer.from(signature, 'base64');
|
|
41
|
-
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
6
|
+
return signatureBuffer.byteLength === 65;
|
|
47
7
|
}
|
|
48
|
-
/**
|
|
49
|
-
* Derive the public key that signed a valid BIP-137 signature.
|
|
50
|
-
* @param message Message signed by the signature
|
|
51
|
-
* @param signature Base-64 encoded signature to be decoded
|
|
52
|
-
* @returns Public key that signs the provided signature
|
|
53
|
-
*/
|
|
54
8
|
static derivePubKey(message, signature) {
|
|
55
|
-
// Compute the hash signed by the signer
|
|
56
9
|
const messageHash = bitcoinMessage.magicHash(message);
|
|
57
|
-
// Decode the provided BIP-137 signature
|
|
58
10
|
const signatureDecoded = this.decodeSignature(Buffer.from(signature, 'base64'));
|
|
59
|
-
|
|
60
|
-
const recoveredPublicKey = (0, secp256k1_1.ecdsaRecover)(signatureDecoded.signature, signatureDecoded.recovery, messageHash, signatureDecoded.compressed);
|
|
11
|
+
const recoveredPublicKey = secp256k1.ecdsaRecover(signatureDecoded.signature, signatureDecoded.recovery, messageHash, signatureDecoded.compressed);
|
|
61
12
|
return Buffer.from(recoveredPublicKey);
|
|
62
13
|
}
|
|
63
|
-
/**
|
|
64
|
-
* Decode a BIP-137 signature.
|
|
65
|
-
* Function copied from bitcoinjs-message library.
|
|
66
|
-
* @param signature BIP-137 signature to be decoded
|
|
67
|
-
* @returns Decoded BIP-137 signature
|
|
68
|
-
*/
|
|
69
14
|
static decodeSignature(signature) {
|
|
70
15
|
if (signature.length !== 65)
|
|
71
16
|
throw new Error('Invalid signature length');
|
|
@@ -76,9 +21,9 @@ class BIP137 {
|
|
|
76
21
|
return {
|
|
77
22
|
compressed: !!(flagByte & 12),
|
|
78
23
|
recovery: flagByte & 3,
|
|
79
|
-
signature: signature.subarray(1)
|
|
24
|
+
signature: signature.subarray(1),
|
|
80
25
|
};
|
|
81
26
|
}
|
|
82
27
|
}
|
|
83
|
-
|
|
28
|
+
export default BIP137;
|
|
84
29
|
//# sourceMappingURL=BIP137.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BIP137.js","sourceRoot":"","sources":["../../src/helpers/BIP137.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,mBAAmB,CAAA;AACzC,OAAO,KAAK,cAAc,MAAM,mBAAmB,CAAA;AAKnD,MAAM,MAAM;IAMH,MAAM,CAAC,iBAAiB,CAAC,SAAiB;QAE/C,MAAM,eAAe,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS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ascript.
|
|
4
|
-
* Reference: https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
|
|
5
|
-
*/
|
|
6
1
|
declare class VarInt {
|
|
7
|
-
/**
|
|
8
|
-
* Encode an integer i as a variable length integer.
|
|
9
|
-
* Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/helper.py#L180
|
|
10
|
-
* @param i Integer to be encoded
|
|
11
|
-
* @returns Encoded varint
|
|
12
|
-
*/
|
|
13
2
|
static encode(i: number): Buffer;
|
|
14
|
-
/**
|
|
15
|
-
* Decode a variable length integer from a Buffer into a number.
|
|
16
|
-
* Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/helper.py#L160
|
|
17
|
-
* @param b Buffer which contain the varint
|
|
18
|
-
* @returns Decoded number
|
|
19
|
-
*/
|
|
20
3
|
static decode(b: Buffer): number;
|
|
21
4
|
}
|
|
22
5
|
export default VarInt;
|
package/dist/helpers/VarInt.js
CHANGED
|
@@ -1,77 +1,48 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
/**
|
|
4
|
-
* Class that implement variable length integer (VarInt) in Javascript.
|
|
5
|
-
* Reference: https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
|
|
6
|
-
*/
|
|
7
1
|
class VarInt {
|
|
8
|
-
/**
|
|
9
|
-
* Encode an integer i as a variable length integer.
|
|
10
|
-
* Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/helper.py#L180
|
|
11
|
-
* @param i Integer to be encoded
|
|
12
|
-
* @returns Encoded varint
|
|
13
|
-
*/
|
|
14
2
|
static encode(i) {
|
|
15
|
-
if (i <
|
|
3
|
+
if (i < 0xfd) {
|
|
16
4
|
const buffer = Buffer.alloc(1);
|
|
17
5
|
buffer.writeUInt8(i);
|
|
18
6
|
return buffer;
|
|
19
7
|
}
|
|
20
|
-
|
|
8
|
+
if (i < 0x1_00_00) {
|
|
21
9
|
const buffer = Buffer.alloc(3);
|
|
22
10
|
buffer.writeUInt8(0xfd);
|
|
23
11
|
buffer.writeUInt16LE(i, 1);
|
|
24
12
|
return buffer;
|
|
25
13
|
}
|
|
26
|
-
|
|
14
|
+
if (i < 0x1_00_00_00_00) {
|
|
27
15
|
const buffer = Buffer.alloc(5);
|
|
28
16
|
buffer.writeUInt8(0xfe);
|
|
29
17
|
buffer.writeUInt32LE(i, 1);
|
|
30
18
|
return buffer;
|
|
31
19
|
}
|
|
32
|
-
|
|
20
|
+
if (i < 0x1_00_00_00_00_00_00) {
|
|
33
21
|
const buffer = Buffer.alloc(9);
|
|
34
22
|
buffer.writeUInt8(0xff);
|
|
35
|
-
buffer.writeUIntLE(i, 1, 6);
|
|
36
|
-
buffer.writeUInt8(0x00, 7);
|
|
23
|
+
buffer.writeUIntLE(i, 1, 6);
|
|
24
|
+
buffer.writeUInt8(0x00, 7);
|
|
37
25
|
buffer.writeUInt8(0x00, 8);
|
|
38
26
|
return buffer;
|
|
39
27
|
}
|
|
40
|
-
|
|
41
|
-
throw new Error(`Integer too large: ${i}`);
|
|
42
|
-
}
|
|
28
|
+
throw new Error(`Integer too large: ${i}`);
|
|
43
29
|
}
|
|
44
|
-
/**
|
|
45
|
-
* Decode a variable length integer from a Buffer into a number.
|
|
46
|
-
* Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/helper.py#L160
|
|
47
|
-
* @param b Buffer which contain the varint
|
|
48
|
-
* @returns Decoded number
|
|
49
|
-
*/
|
|
50
30
|
static decode(b) {
|
|
51
|
-
// Check for empty buffer
|
|
52
31
|
if (b.byteLength === 0) {
|
|
53
32
|
throw new Error('Empty buffer provided');
|
|
54
33
|
}
|
|
55
|
-
// Read first byte from the buffer
|
|
56
34
|
const i = b.readUInt8();
|
|
57
|
-
// Check if i is indicating its length
|
|
58
35
|
if (i === 0xfd) {
|
|
59
|
-
// 0xfd means the next two bytes are the number
|
|
60
36
|
return b.readUInt16LE(1);
|
|
61
37
|
}
|
|
62
|
-
|
|
63
|
-
// 0xfe means the next four bytes are the number
|
|
38
|
+
if (i === 0xfe) {
|
|
64
39
|
return b.readUInt32LE(1);
|
|
65
40
|
}
|
|
66
|
-
|
|
67
|
-
// 0xff means the next eight bytes are the number, but Node JS can only read up to 6 bytes
|
|
41
|
+
if (i === 0xff) {
|
|
68
42
|
return b.readUIntLE(1, 6);
|
|
69
43
|
}
|
|
70
|
-
|
|
71
|
-
// Anything else is just the integer
|
|
72
|
-
return i;
|
|
73
|
-
}
|
|
44
|
+
return i;
|
|
74
45
|
}
|
|
75
46
|
}
|
|
76
|
-
|
|
47
|
+
export default VarInt;
|
|
77
48
|
//# sourceMappingURL=VarInt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VarInt.js","sourceRoot":"","sources":["../../src/helpers/VarInt.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM;IAOH,MAAM,CAAC,MAAM,CAAC,CAAS;QAC5B,IAAI,CAAC,GAAG,IAAI,EAAE,CAAC;YACb,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;YAC9B,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;YACpB,OAAO,MAAM,CAAA;QACf,CAAC;QAED,IAAI,CAAC,GAAG,SAAS,EAAE,CAAC;YAClB,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;YAC9B,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;YACvB,MAAM,CAAC,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;YAC1B,OAAO,MAAM,CAAA;QACf,CAAC;QAED,IAAI,CAAC,GAAG,eAAe,EAAE,CAAC;YACxB,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,C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,CAAA"}
|
package/dist/helpers/VarStr.d.ts
CHANGED
|
@@ -1,22 +1,5 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/**
|
|
3
|
-
* Class that implement variable length string (VarStr) in Javascript.
|
|
4
|
-
* Reference: https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_string
|
|
5
|
-
*/
|
|
6
1
|
declare class VarStr {
|
|
7
|
-
/**
|
|
8
|
-
* Encode a string buffer as a variable length string.
|
|
9
|
-
* Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/helper.py#L203
|
|
10
|
-
* @param s String buffer to be encoded
|
|
11
|
-
* @returns Encoded varstr
|
|
12
|
-
*/
|
|
13
2
|
static encode(s: Buffer): Buffer;
|
|
14
|
-
/**
|
|
15
|
-
* Decode a variable length string from a Buffer into a string buffer.
|
|
16
|
-
* Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/helper.py#L194
|
|
17
|
-
* @param v Varstr to be decoded
|
|
18
|
-
* @returns Decoded string buffer
|
|
19
|
-
*/
|
|
20
3
|
static decode(v: Buffer): Buffer;
|
|
21
4
|
}
|
|
22
5
|
export default VarStr;
|
package/dist/helpers/VarStr.js
CHANGED
|
@@ -1,41 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
// Import dependency
|
|
7
|
-
const VarInt_1 = __importDefault(require("./VarInt"));
|
|
8
|
-
/**
|
|
9
|
-
* Class that implement variable length string (VarStr) in Javascript.
|
|
10
|
-
* Reference: https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_string
|
|
11
|
-
*/
|
|
1
|
+
import VarInt from './VarInt.js';
|
|
12
2
|
class VarStr {
|
|
13
|
-
/**
|
|
14
|
-
* Encode a string buffer as a variable length string.
|
|
15
|
-
* Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/helper.py#L203
|
|
16
|
-
* @param s String buffer to be encoded
|
|
17
|
-
* @returns Encoded varstr
|
|
18
|
-
*/
|
|
19
3
|
static encode(s) {
|
|
20
|
-
|
|
21
|
-
const lengthBuffer = VarInt_1.default.encode(s.length);
|
|
22
|
-
// Concat the actual string right after the length of the string
|
|
4
|
+
const lengthBuffer = VarInt.encode(s.length);
|
|
23
5
|
return Buffer.concat([lengthBuffer, s]);
|
|
24
6
|
}
|
|
25
|
-
/**
|
|
26
|
-
* Decode a variable length string from a Buffer into a string buffer.
|
|
27
|
-
* Reference: https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/helper.py#L194
|
|
28
|
-
* @param v Varstr to be decoded
|
|
29
|
-
* @returns Decoded string buffer
|
|
30
|
-
*/
|
|
31
7
|
static decode(v) {
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
// Get the length of the VarInt used to represent the length of the string
|
|
35
|
-
const lengthByteLength = VarInt_1.default.encode(length).byteLength;
|
|
36
|
-
// Return from lengthByteLength to (length + lengthByteLength) in the buffer which contain the actual string
|
|
8
|
+
const length = VarInt.decode(v);
|
|
9
|
+
const lengthByteLength = VarInt.encode(length).byteLength;
|
|
37
10
|
return v.subarray(lengthByteLength, length + lengthByteLength);
|
|
38
11
|
}
|
|
39
12
|
}
|
|
40
|
-
|
|
13
|
+
export default VarStr;
|
|
41
14
|
//# sourceMappingURL=VarStr.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VarStr.js","sourceRoot":"","sources":["../../src/helpers/VarStr.ts"],"names":[],"mappings":"AACA,OAAO,MAAM,MAAM,aAAa,CAAA;AAMhC,MAAM,MAAM;IAOH,MAAM,CAAC,MAAM,CAAC,CAAS;QAE5B,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAA;QAE5C,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,CAAA;IACzC,CAAC;IAQM,MAAM,CAAC,MAAM,CAAC,CAAS;QAE5B,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;QAE/B,MAAM,gBAAgB,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,UAAU,CAAA;QAEzD,OAAO,CAAC,CAAC,QAAQ,CAAC,gBAAgB,EAAE,MAAM,GAAG,gBAAgB,CAAC,CAAA;IAChE,CAAC;CACF;AAED,eAAe,MAAM,CAAA"}
|
|
@@ -1,23 +1,5 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/**
|
|
3
|
-
* Class that implement witness data serialization and deserialization.
|
|
4
|
-
*/
|
|
5
1
|
declare class Witness {
|
|
6
|
-
/**
|
|
7
|
-
* Encode array of witness into its base-64 encoded format.
|
|
8
|
-
* Follows the encoding scheme found in buidl-python:
|
|
9
|
-
* https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/witness.py#L35
|
|
10
|
-
* @param witnesses Array of witness data
|
|
11
|
-
* @returns Base-64 encoded witness data
|
|
12
|
-
*/
|
|
13
2
|
static serialize(witnesses: Uint8Array[]): string;
|
|
14
|
-
/**
|
|
15
|
-
* Decode encoded witness data, either as a base-64 encoded string or as a decoded string in a buffer, into an array of witness.
|
|
16
|
-
* Follows the decoding scheme found in buidl-python:
|
|
17
|
-
* https://github.com/buidl-bitcoin/buidl-python/blob/d79e9808e8ca60975d315be41293cb40d968626d/buidl/witness.py#L62
|
|
18
|
-
* @param encodedWitness Base-64 encoded witness data, or encoded witness data that have already been decoded
|
|
19
|
-
* @returns Decoded witness data
|
|
20
|
-
*/
|
|
21
3
|
static deserialize(encodedWitness: string | Buffer): Buffer[];
|
|
22
4
|
}
|
|
23
5
|
export default Witness;
|