-
Notifications
You must be signed in to change notification settings - Fork 5
/
compat.ts
159 lines (144 loc) · 3.72 KB
/
compat.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { BIP32Interface, fromPrivateKey, fromPublicKey } from 'bip32';
import { Message, sha256 } from 'js-sha256';
import RIPEMD160 from 'ripemd160';
import { encode as b32Encode } from 'buf-b32';
import { randomBytes } from 'crypto';
import secp256k1 from 'secp256k1';
import { tou8 } from './utils';
/**
* @param {any} args
* @returns any
*/
function ripemd160(args: any): any {
return new RIPEMD160().update(args).digest();
}
/**
* @param {any} args
* @returns any
*/
function hash160(args: any): any {
return ripemd160(Buffer.from(sha256s(args)));
}
/**
* @param {Message} msg
* @returns number[]
*/
function sha256s(msg: Message): number[] {
const hash = sha256.create();
const msg_digest = hash.update(msg).digest();
return msg_digest;
}
/**
* @param {Buffer} buff
* @returns Buffer (base-32 encoded)
*/
function base32Encode(buff: Buffer): Buffer {
return Buffer.from(b32Encode(tou8(buff) as ArrayBufferView));
}
/**
* @param {number} header
* @returns number
*/
function rec_id_from_header(header: number): number {
let header_num = header & 0xff;
if (header_num >= 39) header_num -= 12;
else if (header_num >= 35) header_num -= 8;
else if (header_num >= 31) header_num -= 4;
const rec_id = header_num - 27;
return rec_id;
}
/**
* @returns {priv: Buffer, pub: Buffer}
*/
function CT_pick_keypair(): { priv: Buffer; pub: Buffer } {
let priv;
const compressed = true;
do {
priv = randomBytes(32);
} while (!secp256k1.privateKeyVerify(priv));
const pub = secp256k1.publicKeyCreate(priv, compressed);
return { priv, pub };
}
/**
* @param {Uint8Array|Buffer} priv
* @returns Buffer (compressed pubkey)
*/
function CT_priv_to_pubkey(priv: Uint8Array | Buffer): Buffer {
const compressed = true;
return secp256k1.publicKeyCreate(priv, compressed);
}
/**
* @param {Uint8Array} sig
* @param {Uint8Array} msg_digest
* @param {Uint8Array} pub
* @returns boolean
*/
function CT_sig_verify(
sig: Uint8Array,
msg_digest: Uint8Array,
pub: Uint8Array
): boolean {
return secp256k1.ecdsaVerify(sig, msg_digest, pub);
}
/**
* @param {Uint8Array} msg_digest
* @param {Uint8Array} sig
* @returns Buffer (pubkey 33 bytes)
*/
function CT_sig_to_pubkey(msg_digest: Uint8Array, sig: Uint8Array): Buffer {
const header = sig.slice(0, 1)[0];
const compact_sig = sig.slice(1);
const rec_id = rec_id_from_header(header);
return secp256k1.ecdsaRecover(compact_sig, rec_id, msg_digest);
}
/**
* @param {Uint8Array} his_pubkey
* @param {Uint8Array} my_privkey
* @returns Buffer (32-byte session key, w/ is sha256s i.e. compressed point)
*/
function CT_ecdh(his_pubkey: Uint8Array, my_privkey: Uint8Array): Buffer {
return secp256k1.ecdh(his_pubkey, my_privkey);
}
/**
* @param {Uint8Array} privkey
* @param {Uint8Array} msg_digest
* @returns Buffer (64-byte signature)
*/
function CT_sign(privkey: Uint8Array, msg_digest: Uint8Array): Buffer {
return secp256k1.ecdsaSign(msg_digest, privkey);
}
/**
* @param {Buffer} chain_code
* @param {Buffer} master_priv_pub
* @param {any[]} subkey_path
* @returns Buffer (pubkey 33 bytes)
*/
function CT_bip32_derive(
chain_code: Buffer,
master_priv_pub: Buffer,
subkey_path: any[]
): Buffer {
let master: BIP32Interface;
if (master_priv_pub.length === 32) {
master = fromPrivateKey(master_priv_pub, chain_code); // master_priv_pub :: private_key
} else {
master = fromPublicKey(master_priv_pub, chain_code); // master_priv_pub :: public_key
}
let node = master;
subkey_path.forEach((i: number) => {
node = node.derive(i);
});
return node.publicKey;
}
export {
sha256s,
hash160,
base32Encode,
CT_pick_keypair,
CT_priv_to_pubkey,
CT_sig_verify,
CT_sig_to_pubkey,
CT_ecdh,
CT_sign,
CT_bip32_derive,
};