@alexbosworth/blockchain 4.2.0 → 4.3.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,9 @@
1
1
  # Versions
2
2
 
3
+ ## 4.3.0
4
+
5
+ - `hashForP2pkh`: Add method to get the p2pkh hash160 for a public key
6
+
3
7
  ## 4.2.0
4
8
 
5
9
  - `encodeBech32Address`: Add method to encode a bech32/bech32m address string
package/README.md CHANGED
@@ -9,6 +9,7 @@ Utility methods for working with Blockchain data
9
9
  - [decodeBase58Address](#decodebase58address)
10
10
  - [decodeBech32Address](#decodebech32address)
11
11
  - [encodeBech32Address](#encodebech32address)
12
+ - [hashForP2pkh](#hashforp2pkh)
12
13
  - [hashForP2wpkh](#hashforp2wpkh)
13
14
  - [idForBlock](#idforblock)
14
15
  - [idForTransaction](#idfortransaction)
@@ -149,6 +150,24 @@ const {address} = encodeBech32Address({
149
150
  });
150
151
  ```
151
152
 
153
+ ### hashForP2pkh
154
+
155
+ Get the hash160 of a public key to pay to via P2PKH
156
+
157
+ Use `hash` with `p2pkhOutputScript` for a P2PKH output script
158
+
159
+ {
160
+ key: <Public Key Buffer Object>
161
+ }
162
+
163
+ @throws
164
+ <Error>
165
+
166
+ @returns
167
+ {
168
+ hash: <Public Key Hash160 Buffer Object>
169
+ }
170
+
152
171
  ### hashForP2wpkh
153
172
 
154
173
  Get the hash160 of a public key to pay to via P2WPKH
@@ -295,7 +314,7 @@ const {script} = p2wshOutputScript({hash});
295
314
 
296
315
  ### p2pkhOutputScript
297
316
 
298
- Get a Pay To Witness Public Key Hash Output Script
317
+ Get a Pay To Public Key Hash Output Script
299
318
 
300
319
  {
301
320
  hash: <Public Key Hash Buffer Object>
@@ -0,0 +1,32 @@
1
+ const {createHash} = require('node:crypto');
2
+
3
+ const {isBuffer} = Buffer;
4
+ const publicKeyLength = 33;
5
+ const ripemd160 = data => createHash('ripemd160').update(data).digest();
6
+ const sha256 = data => createHash('sha256').update(data).digest();
7
+
8
+ /** Get the hash160 of a public key
9
+
10
+ {
11
+ key: <Public Key Buffer Object>
12
+ }
13
+
14
+ @throws
15
+ <Error>
16
+
17
+ @returns
18
+ {
19
+ hash: <Public Key Hash160 Buffer Object>
20
+ }
21
+ */
22
+ module.exports = ({key}) => {
23
+ if (!isBuffer(key)) {
24
+ throw new Error('ExpectedPublicKeyToDerivePublicKeyHash160');
25
+ }
26
+
27
+ if (key.length !== publicKeyLength) {
28
+ throw new Error('ExpectedCompressedPublicKeyToDerivePublicKeyHash160');
29
+ }
30
+
31
+ return {hash: ripemd160(sha256(key))};
32
+ };
@@ -0,0 +1,19 @@
1
+ const hash160ForPublicKey = require('./hash160_for_public_key');
2
+
3
+ /** Get the hash160 of a public key to pay to via P2PKH
4
+
5
+ Use `hash` with `p2pkhOutputScript` for a P2PKH output script
6
+
7
+ {
8
+ key: <Public Key Buffer Object>
9
+ }
10
+
11
+ @throws
12
+ <Error>
13
+
14
+ @returns
15
+ {
16
+ hash: <Public Key Hash160 Buffer Object>
17
+ }
18
+ */
19
+ module.exports = ({key}) => hash160ForPublicKey({key});
@@ -1,9 +1,4 @@
1
- const {createHash} = require('node:crypto');
2
-
3
- const {isBuffer} = Buffer;
4
- const publicKeyLength = 33;
5
- const ripemd160 = data => createHash('ripemd160').update(data).digest();
6
- const sha256 = data => createHash('sha256').update(data).digest();
1
+ const hash160ForPublicKey = require('./hash160_for_public_key');
7
2
 
8
3
  /** Get the hash160 of a public key to pay to via P2WPKH
9
4
 
@@ -21,14 +16,4 @@ const sha256 = data => createHash('sha256').update(data).digest();
21
16
  hash: <Public Key Hash160 Buffer Object>
22
17
  }
23
18
  */
24
- module.exports = ({key}) => {
25
- if (!isBuffer(key)) {
26
- throw new Error('ExpectedPublicKeyToDeriveHashForP2wpkh');
27
- }
28
-
29
- if (key.length !== publicKeyLength) {
30
- throw new Error('ExpectedCompressedPublicKeyToDeriveHashForP2wpkh');
31
- }
32
-
33
- return {hash: ripemd160(sha256(key))};
34
- };
19
+ module.exports = ({key}) => hash160ForPublicKey({key});
package/hashes/index.js CHANGED
@@ -1,9 +1,11 @@
1
+ const hashForP2pkh = require('./hash_for_p2pkh');
1
2
  const hashForP2wpkh = require('./hash_for_p2wpkh');
2
3
  const idForBlock = require('./id_for_block');
3
4
  const idForTransactionComponents = require('./id_for_transaction_components');
4
5
  const previousBlockId = require('./previous_block_id');
5
6
 
6
7
  module.exports = {
8
+ hashForP2pkh,
7
9
  hashForP2wpkh,
8
10
  idForBlock,
9
11
  idForTransactionComponents,
package/index.js CHANGED
@@ -3,6 +3,7 @@ const {componentsOfTransaction} = require('./transactions');
3
3
  const {decodeBase58Address} = require('./addresses');
4
4
  const {decodeBech32Address} = require('./addresses');
5
5
  const {encodeBech32Address} = require('./addresses');
6
+ const {hashForP2pkh} = require('./hashes');
6
7
  const {hashForP2wpkh} = require('./hashes');
7
8
  const {idForBlock} = require('./hashes');
8
9
  const {idForTransaction} = require('./transactions');
@@ -28,6 +29,7 @@ module.exports = {
28
29
  decodeBase58Address,
29
30
  decodeBech32Address,
30
31
  encodeBech32Address,
32
+ hashForP2pkh,
31
33
  hashForP2wpkh,
32
34
  idForBlock,
33
35
  idForTransaction,
package/package.json CHANGED
@@ -19,5 +19,5 @@
19
19
  "scripts": {
20
20
  "test": "node --experimental-test-coverage --test test/5bit/*.js test/addresses/*.js test/hashes/*.js test/numbers/*.js test/script/*.js test/transactions/*.js"
21
21
  },
22
- "version": "4.2.0"
22
+ "version": "4.3.0"
23
23
  }
@@ -0,0 +1,54 @@
1
+ const {deepStrictEqual} = require('node:assert').strict;
2
+ const {throws} = require('node:assert').strict;
3
+ const test = require('node:test');
4
+
5
+ const {hashForP2pkh} = require('./../../');
6
+
7
+ const bufferFromHex = hex => Buffer.from(hex, 'hex');
8
+
9
+ const tests = [
10
+ {
11
+ args: {},
12
+ description: 'A public key is required',
13
+ error: 'ExpectedPublicKeyToDerivePublicKeyHash160',
14
+ },
15
+ {
16
+ args: {
17
+ key: bufferFromHex('0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8'),
18
+ },
19
+ description: 'A compressed public key is required',
20
+ error: 'ExpectedCompressedPublicKeyToDerivePublicKeyHash160',
21
+ },
22
+ {
23
+ args: {
24
+ key: bufferFromHex('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'),
25
+ },
26
+ description: 'A hash is derived for the BIP 173 example public key',
27
+ expected: {
28
+ hash: bufferFromHex('751e76e8199196d454941c45d1b3a323f1433bd6'),
29
+ },
30
+ },
31
+ {
32
+ args: {
33
+ key: bufferFromHex('025476c2e83188368da1ff3e292e7acafcdb3566bb0ad253f62fc70f07aeee6357'),
34
+ },
35
+ description: 'A hash is derived for the BIP 143 example public key',
36
+ expected: {
37
+ hash: bufferFromHex('1d0f172a0ecb48aee1be1f2687d2963ae33f71a1'),
38
+ },
39
+ },
40
+ ];
41
+
42
+ tests.forEach(({args, description, error, expected}) => {
43
+ return test(description, (t, end) => {
44
+ if (!!error) {
45
+ throws(() => hashForP2pkh(args), new Error(error), 'Got err');
46
+ } else {
47
+ const res = hashForP2pkh(args);
48
+
49
+ deepStrictEqual(res, expected, 'Got expected result');
50
+ }
51
+
52
+ return end();
53
+ });
54
+ });
@@ -10,14 +10,14 @@ const tests = [
10
10
  {
11
11
  args: {},
12
12
  description: 'A public key is required',
13
- error: 'ExpectedPublicKeyToDeriveHashForP2wpkh',
13
+ error: 'ExpectedPublicKeyToDerivePublicKeyHash160',
14
14
  },
15
15
  {
16
16
  args: {
17
17
  key: bufferFromHex('0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8'),
18
18
  },
19
19
  description: 'A compressed public key is required',
20
- error: 'ExpectedCompressedPublicKeyToDeriveHashForP2wpkh',
20
+ error: 'ExpectedCompressedPublicKeyToDerivePublicKeyHash160',
21
21
  },
22
22
  {
23
23
  args: {