Skip to content

Commit c462e1d

Browse files
committed
refactor: hardcoded prefixes
1 parent 6c886dd commit c462e1d

12 files changed

+152
-133
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"gitdiff:ci": "npm run build && git diff --exit-code",
3030
"integration": "npm run build && npm run nobuild:integration",
3131
"lint": "eslint ts_src/** src/**/*.js",
32-
"lint:fix": "eslint --fix ts_src/** src/**/*.js",
3332
"lint:tests": "eslint test/**/*.spec.ts",
3433
"mocha:ts": "mocha --recursive --require test/ts-node-register",
3534
"nobuild:coverage-report": "nyc report --reporter=lcov",
@@ -40,8 +39,7 @@
4039
"prettier": "prettier \"ts_src/**/*.ts\" \"test/**/*.ts\" --ignore-path ./.prettierignore",
4140
"prettierjs": "prettier \"src/**/*.js\" --ignore-path ./.prettierignore",
4241
"test": "npm run build && npm run format:ci && npm run lint && npm run nobuild:coverage",
43-
"unit": "npm run build && npm run nobuild:unit",
44-
"generate:prefixes": "node scripts/generate-tagged-hash-prefixes.js && yarn prettierjs && yarn lint:fix"
42+
"unit": "npm run build && npm run nobuild:unit"
4543
},
4644
"repository": {
4745
"type": "git",

scripts/generate-tagged-hash-prefixes.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/crypto.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
/// <reference types="node" />
2-
import { TAGS } from './tags';
32
export declare function ripemd160(buffer: Buffer): Buffer;
43
export declare function sha1(buffer: Buffer): Buffer;
54
export declare function sha256(buffer: Buffer): Buffer;
65
export declare function hash160(buffer: Buffer): Buffer;
76
export declare function hash256(buffer: Buffer): Buffer;
7+
export declare const TAGS: readonly ["BIP0340/challenge", "BIP0340/aux", "BIP0340/nonce", "TapLeaf", "TapBranch", "TapSighash", "TapTweak", "KeyAgg list", "KeyAgg coefficient"];
88
export type TaggedHashPrefix = typeof TAGS[number];
9+
export type TaggedHashPrefixes = {
10+
[key in TaggedHashPrefix]: Buffer;
11+
};
12+
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
13+
export declare const TAGGED_HASH_PREFIXES: TaggedHashPrefixes;
914
export declare function taggedHash(prefix: TaggedHashPrefix, data: Buffer): Buffer;

src/crypto.js

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22
Object.defineProperty(exports, '__esModule', { value: true });
33
exports.taggedHash =
4+
exports.TAGGED_HASH_PREFIXES =
5+
exports.TAGS =
46
exports.hash256 =
57
exports.hash160 =
68
exports.sha256 =
@@ -9,7 +11,6 @@ exports.taggedHash =
911
void 0;
1012
const createHash = require('create-hash');
1113
const RipeMd160 = require('ripemd160');
12-
const tagged_hash_prefixes_1 = require('./tagged-hash-prefixes');
1314
function ripemd160(buffer) {
1415
try {
1516
return createHash('rmd160').update(buffer).digest();
@@ -38,14 +39,75 @@ function hash256(buffer) {
3839
return sha256(sha256(buffer));
3940
}
4041
exports.hash256 = hash256;
42+
exports.TAGS = [
43+
'BIP0340/challenge',
44+
'BIP0340/aux',
45+
'BIP0340/nonce',
46+
'TapLeaf',
47+
'TapBranch',
48+
'TapSighash',
49+
'TapTweak',
50+
'KeyAgg list',
51+
'KeyAgg coefficient',
52+
];
4153
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
42-
const TAGGED_HASH_PREFIXES = Object.fromEntries(
43-
Object.keys(tagged_hash_prefixes_1.TAGGED_HASH_PREFIXES_HEX).map(tag => [
44-
tag,
45-
Buffer.from(tagged_hash_prefixes_1.TAGGED_HASH_PREFIXES_HEX[tag], 'hex'),
54+
exports.TAGGED_HASH_PREFIXES = {
55+
'BIP0340/challenge': Buffer.from([
56+
123, 181, 45, 122, 159, 239, 88, 50, 62, 177, 191, 122, 64, 125, 179, 130,
57+
210, 243, 242, 216, 27, 177, 34, 79, 73, 254, 81, 143, 109, 72, 211, 124,
58+
123, 181, 45, 122, 159, 239, 88, 50, 62, 177, 191, 122, 64, 125, 179, 130,
59+
210, 243, 242, 216, 27, 177, 34, 79, 73, 254, 81, 143, 109, 72, 211, 124,
4660
]),
47-
);
61+
'BIP0340/aux': Buffer.from([
62+
241, 239, 78, 94, 192, 99, 202, 218, 109, 148, 202, 250, 157, 152, 126, 160,
63+
105, 38, 88, 57, 236, 193, 31, 151, 45, 119, 165, 46, 216, 193, 204, 144,
64+
241, 239, 78, 94, 192, 99, 202, 218, 109, 148, 202, 250, 157, 152, 126, 160,
65+
105, 38, 88, 57, 236, 193, 31, 151, 45, 119, 165, 46, 216, 193, 204, 144,
66+
]),
67+
'BIP0340/nonce': Buffer.from([
68+
7, 73, 119, 52, 167, 155, 203, 53, 91, 155, 140, 125, 3, 79, 18, 28, 244,
69+
52, 215, 62, 247, 45, 218, 25, 135, 0, 97, 251, 82, 191, 235, 47, 7, 73,
70+
119, 52, 167, 155, 203, 53, 91, 155, 140, 125, 3, 79, 18, 28, 244, 52, 215,
71+
62, 247, 45, 218, 25, 135, 0, 97, 251, 82, 191, 235, 47,
72+
]),
73+
TapLeaf: Buffer.from([
74+
174, 234, 143, 220, 66, 8, 152, 49, 5, 115, 75, 88, 8, 29, 30, 38, 56, 211,
75+
95, 28, 181, 64, 8, 212, 211, 87, 202, 3, 190, 120, 233, 238, 174, 234, 143,
76+
220, 66, 8, 152, 49, 5, 115, 75, 88, 8, 29, 30, 38, 56, 211, 95, 28, 181,
77+
64, 8, 212, 211, 87, 202, 3, 190, 120, 233, 238,
78+
]),
79+
TapBranch: Buffer.from([
80+
25, 65, 161, 242, 229, 110, 185, 95, 162, 169, 241, 148, 190, 92, 1, 247,
81+
33, 111, 51, 237, 130, 176, 145, 70, 52, 144, 208, 91, 245, 22, 160, 21, 25,
82+
65, 161, 242, 229, 110, 185, 95, 162, 169, 241, 148, 190, 92, 1, 247, 33,
83+
111, 51, 237, 130, 176, 145, 70, 52, 144, 208, 91, 245, 22, 160, 21,
84+
]),
85+
TapSighash: Buffer.from([
86+
244, 10, 72, 223, 75, 42, 112, 200, 180, 146, 75, 242, 101, 70, 97, 237, 61,
87+
149, 253, 102, 163, 19, 235, 135, 35, 117, 151, 198, 40, 228, 160, 49, 244,
88+
10, 72, 223, 75, 42, 112, 200, 180, 146, 75, 242, 101, 70, 97, 237, 61, 149,
89+
253, 102, 163, 19, 235, 135, 35, 117, 151, 198, 40, 228, 160, 49,
90+
]),
91+
TapTweak: Buffer.from([
92+
232, 15, 225, 99, 156, 156, 160, 80, 227, 175, 27, 57, 193, 67, 198, 62, 66,
93+
156, 188, 235, 21, 217, 64, 251, 181, 197, 161, 244, 175, 87, 197, 233, 232,
94+
15, 225, 99, 156, 156, 160, 80, 227, 175, 27, 57, 193, 67, 198, 62, 66, 156,
95+
188, 235, 21, 217, 64, 251, 181, 197, 161, 244, 175, 87, 197, 233,
96+
]),
97+
'KeyAgg list': Buffer.from([
98+
72, 28, 151, 28, 60, 11, 70, 215, 240, 178, 117, 174, 89, 141, 78, 44, 126,
99+
215, 49, 156, 89, 74, 92, 110, 199, 158, 160, 212, 153, 2, 148, 240, 72, 28,
100+
151, 28, 60, 11, 70, 215, 240, 178, 117, 174, 89, 141, 78, 44, 126, 215, 49,
101+
156, 89, 74, 92, 110, 199, 158, 160, 212, 153, 2, 148, 240,
102+
]),
103+
'KeyAgg coefficient': Buffer.from([
104+
191, 201, 4, 3, 77, 28, 136, 232, 200, 14, 34, 229, 61, 36, 86, 109, 100,
105+
130, 78, 214, 66, 114, 129, 192, 145, 0, 249, 77, 205, 82, 201, 129, 191,
106+
201, 4, 3, 77, 28, 136, 232, 200, 14, 34, 229, 61, 36, 86, 109, 100, 130,
107+
78, 214, 66, 114, 129, 192, 145, 0, 249, 77, 205, 82, 201, 129,
108+
]),
109+
};
48110
function taggedHash(prefix, data) {
49-
return sha256(Buffer.concat([TAGGED_HASH_PREFIXES[prefix], data]));
111+
return sha256(Buffer.concat([exports.TAGGED_HASH_PREFIXES[prefix], data]));
50112
}
51113
exports.taggedHash = taggedHash;

src/tagged-hash-prefixes.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/tagged-hash-prefixes.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/tags.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/tags.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/crypto.spec.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import * as assert from 'assert';
22
import { describe, it } from 'mocha';
33
import { crypto as bcrypto, TaggedHashPrefix } from '..';
44
import * as fixtures from './fixtures/crypto.json';
5-
import { sha256 } from '../src/crypto';
6-
import { TAGS } from '../src/tags';
7-
import { TAGGED_HASH_PREFIXES_HEX } from '../src/tagged-hash-prefixes';
5+
import { sha256, TAGS, TAGGED_HASH_PREFIXES } from '../src/crypto';
86

97
describe('crypto', () => {
108
['hash160', 'hash256', 'ripemd160', 'sha1', 'sha256'].forEach(algorithm => {
@@ -36,18 +34,16 @@ describe('crypto', () => {
3634

3735
describe('TAGGED_HASH_PREFIXES', () => {
3836
const taggedHashPrefixes = Object.fromEntries(
39-
TAGS.map(tag => {
37+
TAGS.map((tag: TaggedHashPrefix) => {
4038
const tagHash = sha256(Buffer.from(tag));
4139
return [tag, Buffer.concat([tagHash, tagHash])];
4240
}),
4341
);
4442
it('stored the result of operation', () => {
45-
Object.keys(taggedHashPrefixes).forEach(tag => {
46-
assert.strictEqual(
47-
TAGGED_HASH_PREFIXES_HEX[tag],
48-
taggedHashPrefixes[tag].toString('hex'),
49-
);
50-
});
43+
assert.strictEqual(
44+
JSON.stringify(TAGGED_HASH_PREFIXES),
45+
JSON.stringify(taggedHashPrefixes),
46+
);
5147
});
5248
});
5349
});

ts_src/crypto.ts

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as createHash from 'create-hash';
22
import * as RipeMd160 from 'ripemd160';
3-
import { TAGGED_HASH_PREFIXES_HEX } from './tagged-hash-prefixes';
4-
import { TAGS } from './tags';
3+
54
export function ripemd160(buffer: Buffer): Buffer {
65
try {
76
return createHash('rmd160').update(buffer).digest();
@@ -30,16 +29,78 @@ export function hash256(buffer: Buffer): Buffer {
3029
return sha256(sha256(buffer));
3130
}
3231

32+
export const TAGS = [
33+
'BIP0340/challenge',
34+
'BIP0340/aux',
35+
'BIP0340/nonce',
36+
'TapLeaf',
37+
'TapBranch',
38+
'TapSighash',
39+
'TapTweak',
40+
'KeyAgg list',
41+
'KeyAgg coefficient',
42+
] as const;
3343
export type TaggedHashPrefix = typeof TAGS[number];
34-
44+
type TaggedHashPrefixes = {
45+
[key in TaggedHashPrefix]: Buffer;
46+
};
3547
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
36-
37-
const TAGGED_HASH_PREFIXES = Object.fromEntries(
38-
Object.keys(TAGGED_HASH_PREFIXES_HEX).map((tag: string) => [
39-
tag,
40-
Buffer.from(TAGGED_HASH_PREFIXES_HEX[tag], 'hex'),
48+
export const TAGGED_HASH_PREFIXES: TaggedHashPrefixes = {
49+
'BIP0340/challenge': Buffer.from([
50+
123, 181, 45, 122, 159, 239, 88, 50, 62, 177, 191, 122, 64, 125, 179, 130,
51+
210, 243, 242, 216, 27, 177, 34, 79, 73, 254, 81, 143, 109, 72, 211, 124,
52+
123, 181, 45, 122, 159, 239, 88, 50, 62, 177, 191, 122, 64, 125, 179, 130,
53+
210, 243, 242, 216, 27, 177, 34, 79, 73, 254, 81, 143, 109, 72, 211, 124,
54+
]),
55+
'BIP0340/aux': Buffer.from([
56+
241, 239, 78, 94, 192, 99, 202, 218, 109, 148, 202, 250, 157, 152, 126, 160,
57+
105, 38, 88, 57, 236, 193, 31, 151, 45, 119, 165, 46, 216, 193, 204, 144,
58+
241, 239, 78, 94, 192, 99, 202, 218, 109, 148, 202, 250, 157, 152, 126, 160,
59+
105, 38, 88, 57, 236, 193, 31, 151, 45, 119, 165, 46, 216, 193, 204, 144,
60+
]),
61+
'BIP0340/nonce': Buffer.from([
62+
7, 73, 119, 52, 167, 155, 203, 53, 91, 155, 140, 125, 3, 79, 18, 28, 244,
63+
52, 215, 62, 247, 45, 218, 25, 135, 0, 97, 251, 82, 191, 235, 47, 7, 73,
64+
119, 52, 167, 155, 203, 53, 91, 155, 140, 125, 3, 79, 18, 28, 244, 52, 215,
65+
62, 247, 45, 218, 25, 135, 0, 97, 251, 82, 191, 235, 47,
66+
]),
67+
TapLeaf: Buffer.from([
68+
174, 234, 143, 220, 66, 8, 152, 49, 5, 115, 75, 88, 8, 29, 30, 38, 56, 211,
69+
95, 28, 181, 64, 8, 212, 211, 87, 202, 3, 190, 120, 233, 238, 174, 234, 143,
70+
220, 66, 8, 152, 49, 5, 115, 75, 88, 8, 29, 30, 38, 56, 211, 95, 28, 181,
71+
64, 8, 212, 211, 87, 202, 3, 190, 120, 233, 238,
72+
]),
73+
TapBranch: Buffer.from([
74+
25, 65, 161, 242, 229, 110, 185, 95, 162, 169, 241, 148, 190, 92, 1, 247,
75+
33, 111, 51, 237, 130, 176, 145, 70, 52, 144, 208, 91, 245, 22, 160, 21, 25,
76+
65, 161, 242, 229, 110, 185, 95, 162, 169, 241, 148, 190, 92, 1, 247, 33,
77+
111, 51, 237, 130, 176, 145, 70, 52, 144, 208, 91, 245, 22, 160, 21,
78+
]),
79+
TapSighash: Buffer.from([
80+
244, 10, 72, 223, 75, 42, 112, 200, 180, 146, 75, 242, 101, 70, 97, 237, 61,
81+
149, 253, 102, 163, 19, 235, 135, 35, 117, 151, 198, 40, 228, 160, 49, 244,
82+
10, 72, 223, 75, 42, 112, 200, 180, 146, 75, 242, 101, 70, 97, 237, 61, 149,
83+
253, 102, 163, 19, 235, 135, 35, 117, 151, 198, 40, 228, 160, 49,
84+
]),
85+
TapTweak: Buffer.from([
86+
232, 15, 225, 99, 156, 156, 160, 80, 227, 175, 27, 57, 193, 67, 198, 62, 66,
87+
156, 188, 235, 21, 217, 64, 251, 181, 197, 161, 244, 175, 87, 197, 233, 232,
88+
15, 225, 99, 156, 156, 160, 80, 227, 175, 27, 57, 193, 67, 198, 62, 66, 156,
89+
188, 235, 21, 217, 64, 251, 181, 197, 161, 244, 175, 87, 197, 233,
90+
]),
91+
'KeyAgg list': Buffer.from([
92+
72, 28, 151, 28, 60, 11, 70, 215, 240, 178, 117, 174, 89, 141, 78, 44, 126,
93+
215, 49, 156, 89, 74, 92, 110, 199, 158, 160, 212, 153, 2, 148, 240, 72, 28,
94+
151, 28, 60, 11, 70, 215, 240, 178, 117, 174, 89, 141, 78, 44, 126, 215, 49,
95+
156, 89, 74, 92, 110, 199, 158, 160, 212, 153, 2, 148, 240,
96+
]),
97+
'KeyAgg coefficient': Buffer.from([
98+
191, 201, 4, 3, 77, 28, 136, 232, 200, 14, 34, 229, 61, 36, 86, 109, 100,
99+
130, 78, 214, 66, 114, 129, 192, 145, 0, 249, 77, 205, 82, 201, 129, 191,
100+
201, 4, 3, 77, 28, 136, 232, 200, 14, 34, 229, 61, 36, 86, 109, 100, 130,
101+
78, 214, 66, 114, 129, 192, 145, 0, 249, 77, 205, 82, 201, 129,
41102
]),
42-
) as { [k in TaggedHashPrefix]: Buffer };
103+
};
43104

44105
export function taggedHash(prefix: TaggedHashPrefix, data: Buffer): Buffer {
45106
return sha256(Buffer.concat([TAGGED_HASH_PREFIXES[prefix], data]));

ts_src/tagged-hash-prefixes.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

ts_src/tags.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)