@alexbosworth/blockchain 4.9.0 → 4.11.0

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 CHANGED
@@ -1,5 +1,13 @@
1
1
  # Versions
2
2
 
3
+ ## 4.11.0
4
+
5
+ - `decodeWif`: Add method to decode a WIF encoded private key
6
+
7
+ ## 4.10.0
8
+
9
+ - `outputScriptForAddress`: Add support for `btctestnet4` network
10
+
3
11
  ## 4.9.0
4
12
 
5
13
  - `outputScriptForAddress`: Add method to get the output script for an address
package/README.md CHANGED
@@ -9,6 +9,7 @@ Utility methods for working with Blockchain data
9
9
  - [decodeBase58](#decodebase58)
10
10
  - [decodeBase58Address](#decodebase58address)
11
11
  - [decodeBech32Address](#decodebech32address)
12
+ - [decodeWif](#decodewif)
12
13
  - [encodeBase58Address](#encodebase58address)
13
14
  - [encodeBech32Address](#encodebech32address)
14
15
  - [hashForP2pkh](#hashforp2pkh)
@@ -138,6 +139,54 @@ Decode a bech32 address string to derive the address details
138
139
  version: <Witness Version Number>
139
140
  }
140
141
 
142
+ ### decodeWif
143
+
144
+ Decode a WIF encoded private key
145
+
146
+ Derive the public key in compressed form when `is_compressed` is true
147
+
148
+ When a network is not specified, a WIF for any known network is accepted
149
+
150
+ Supported networks: btc, btcregtest, btcsignet, btctestnet, btctestnet4
151
+
152
+ {
153
+ [network]: <Network Name String>
154
+ wif: <WIF Encoded Private Key String>
155
+ }
156
+
157
+ @throws
158
+ <Error>
159
+
160
+ @returns
161
+ {
162
+ is_compressed: <Public Key Is Compressed Bool>
163
+ private_key: <Private Key Buffer Object>
164
+ }
165
+
166
+ Example:
167
+
168
+ ```node
169
+ const {createECDH} = require('node:crypto');
170
+
171
+ const {decodeWif} = require('@alexbosworth/blockchain');
172
+
173
+ // Decode the private key and the public key compression from a mainnet WIF
174
+ const {is_compressed, private_key} = decodeWif({
175
+ network: 'btc',
176
+ wif: 'KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617',
177
+ });
178
+
179
+ // The WIF signals if the public key is compressed or uncompressed
180
+ const format = is_compressed ? 'compressed' : 'uncompressed';
181
+
182
+ const ecdh = createECDH('secp256k1');
183
+
184
+ // Derive the public key from the private key
185
+ ecdh.setPrivateKey(private_key);
186
+
187
+ const publicKey = ecdh.getPublicKey(null, format);
188
+ ```
189
+
141
190
  ### encodeBase58Address
142
191
 
143
192
  Encode output hash and version data as a base58 address string
@@ -365,7 +414,7 @@ Get the output script for an address
365
414
 
366
415
  Supported addresses: P2PKH, P2SH, P2WPKH, P2WSH, P2TR, future segwit versions
367
416
 
368
- Supported networks: btc, btcregtest, btcsignet, btctestnet
417
+ Supported networks: btc, btcregtest, btcsignet, btctestnet, btctestnet4
369
418
 
370
419
  {
371
420
  address: <Address String>
package/index.js CHANGED
@@ -3,6 +3,7 @@ const {componentsOfTransaction} = require('./transactions');
3
3
  const {decodeBase58} = require('./addresses');
4
4
  const {decodeBase58Address} = require('./addresses');
5
5
  const {decodeBech32Address} = require('./addresses');
6
+ const {decodeWif} = require('./keys');
6
7
  const {encodeBase58Address} = require('./addresses');
7
8
  const {encodeBech32Address} = require('./addresses');
8
9
  const {hashForP2pkh} = require('./hashes');
@@ -35,6 +36,7 @@ module.exports = {
35
36
  decodeBase58,
36
37
  decodeBase58Address,
37
38
  decodeBech32Address,
39
+ decodeWif,
38
40
  encodeBase58Address,
39
41
  encodeBech32Address,
40
42
  hashForP2pkh,
@@ -0,0 +1,17 @@
1
+ {
2
+ "btc": {
3
+ "wif": 128
4
+ },
5
+ "btcregtest": {
6
+ "wif": 239
7
+ },
8
+ "btcsignet": {
9
+ "wif": 239
10
+ },
11
+ "btctestnet": {
12
+ "wif": 239
13
+ },
14
+ "btctestnet4": {
15
+ "wif": 239
16
+ }
17
+ }
@@ -0,0 +1,99 @@
1
+ const {decodeBase58} = require('./../addresses');
2
+ const networks = require('./conf/networks.json');
3
+
4
+ const compressedFlag = 0x01;
5
+ const copyOf = bytes => Buffer.alloc(bytes.length, bytes);
6
+ const countFlagBytes = 1;
7
+ const countPrivateKeyBytes = 32;
8
+ const countVersionBytes = 1;
9
+ const curveOrder = Buffer.from('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 'hex');
10
+ const isDefined = n => n !== undefined && n !== null;
11
+ const isZero = bytes => bytes.every(byte => !byte);
12
+ const {keys} = Object;
13
+ const maxWifLength = 52;
14
+ const {values} = Object;
15
+ const wipe = bytes => bytes.fill(0);
16
+
17
+ /** Decode a WIF encoded private key
18
+
19
+ Derive the public key in compressed form when `is_compressed` is true
20
+
21
+ When a network is not specified, a WIF for any known network is accepted
22
+
23
+ Supported networks: btc, btcregtest, btcsignet, btctestnet, btctestnet4
24
+
25
+ {
26
+ [network]: <Network Name String>
27
+ wif: <WIF Encoded Private Key String>
28
+ }
29
+
30
+ @throws
31
+ <Error>
32
+
33
+ @returns
34
+ {
35
+ is_compressed: <Public Key Is Compressed Bool>
36
+ private_key: <Private Key Buffer Object>
37
+ }
38
+ */
39
+ module.exports = ({network, wif}) => {
40
+ if (isDefined(network) && !keys(networks).includes(network)) {
41
+ throw new Error('ExpectedKnownNetworkNameToDecodeWif');
42
+ }
43
+
44
+ if (typeof wif !== 'string') {
45
+ throw new Error('ExpectedWifEncodedPrivateKeyStringToDecode');
46
+ }
47
+
48
+ // Exit early with error when the string is too long to be a WIF to decode
49
+ if (wif.trim().length > maxWifLength) {
50
+ throw new Error('ExpectedShorterWifEncodedPrivateKeyString');
51
+ }
52
+
53
+ // Decode the base58check string and validate its checksum
54
+ const decoded = decodeBase58({encoded: wif});
55
+
56
+ // Copy the payload into its own memory, it was decoded into shared memory
57
+ const payload = copyOf(decoded.payload);
58
+
59
+ // Wipe the shared memory copy of the payload since it has the private key
60
+ wipe(decoded.payload);
61
+
62
+ // The payload is a version byte and then the private key, maybe with a flag
63
+ const keyEnd = countVersionBytes + countPrivateKeyBytes;
64
+
65
+ // A compressed public key is signaled with a flag byte after the private key
66
+ const isCompressed = payload.length === keyEnd + countFlagBytes;
67
+
68
+ // Make sure that the payload has the version and the private key
69
+ if (payload.length !== keyEnd && !isCompressed) {
70
+ throw new Error('ExpectedVersionAnd32BytePrivateKeyInWifPayload');
71
+ }
72
+
73
+ // The compressed public key flag byte only has a single valid value
74
+ if (isCompressed && payload[keyEnd] !== compressedFlag) {
75
+ throw new Error('UnexpectedCompressionFlagValueInWifPayload');
76
+ }
77
+
78
+ const [version] = payload;
79
+
80
+ // Exit early with error when the version is not for the specified network
81
+ if (isDefined(network) && networks[network].wif !== version) {
82
+ throw new Error('UnexpectedWifVersionForNetwork');
83
+ }
84
+
85
+ // Exit early with error when the version is not for any known network
86
+ if (!values(networks).some(n => n.wif === version)) {
87
+ throw new Error('UnexpectedWifVersionForKnownNetworks');
88
+ }
89
+
90
+ // The private key gets its own memory, apart from the rest of the payload
91
+ const privateKey = copyOf(payload.subarray(countVersionBytes, keyEnd));
92
+
93
+ // A private key must be greater than zero and less than the curve order
94
+ if (isZero(privateKey) || privateKey.compare(curveOrder) >= 0) {
95
+ throw new Error('ExpectedValidSecp256k1PrivateKeyInWifPayload');
96
+ }
97
+
98
+ return {is_compressed: isCompressed, private_key: privateKey};
99
+ };
package/keys/index.js ADDED
@@ -0,0 +1,3 @@
1
+ const decodeWif = require('./decode_wif');
2
+
3
+ module.exports = {decodeWif};
package/package.json CHANGED
@@ -16,6 +16,7 @@
16
16
  "CHANGELOG.md",
17
17
  "hashes",
18
18
  "index.js",
19
+ "keys",
19
20
  "numbers",
20
21
  "script",
21
22
  "transactions"
@@ -27,7 +28,7 @@
27
28
  "url": "git+https://github.com/alexbosworth/blockchain.git"
28
29
  },
29
30
  "scripts": {
30
- "test": "node --experimental-test-coverage --test test/5bit/*.js test/addresses/*.js test/hashes/*.js test/numbers/*.js test/script/*.js test/transactions/*.js"
31
+ "test": "node --experimental-test-coverage --test test/5bit/*.js test/addresses/*.js test/hashes/*.js test/keys/*.js test/numbers/*.js test/script/*.js test/transactions/*.js"
31
32
  },
32
- "version": "4.9.0"
33
+ "version": "4.11.0"
33
34
  }
@@ -18,5 +18,10 @@
18
18
  "bech32": "tb",
19
19
  "p2pkh": 111,
20
20
  "p2sh": 196
21
+ },
22
+ "btctestnet4": {
23
+ "bech32": "tb",
24
+ "p2pkh": 111,
25
+ "p2sh": 196
21
26
  }
22
27
  }
@@ -19,7 +19,7 @@ const witnessVersionInitial = 0;
19
19
 
20
20
  Supported addresses: P2PKH, P2SH, P2WPKH, P2WSH, P2TR, future segwit versions
21
21
 
22
- Supported networks: btc, btcregtest, btcsignet, btctestnet
22
+ Supported networks: btc, btcregtest, btcsignet, btctestnet, btctestnet4
23
23
 
24
24
  {
25
25
  address: <Address String>