-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutils.ts
82 lines (70 loc) · 2.3 KB
/
utils.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
import { NetworkCoin, TinySecp256k1Interface } from './interfaces';
import { BIP32API, BIP32Interface } from 'bip32';
import { mainnetData } from './networks';
import * as bip39 from 'bip39';
import * as bitcoin from 'bitcoinjs-lib';
const bs58check = require('bs58check');
export default function getUtils(ecc: TinySecp256k1Interface, bip32: BIP32API) {
const getPublicPaymentCodeNodeFromBase58 = (
paymentCode: string,
network: NetworkCoin,
): BIP32Interface => {
const rawPaymentCode = bs58check.decode(paymentCode);
return bip32.fromPublicKey(
rawPaymentCode.slice(3, 36),
rawPaymentCode.slice(36, 68),
network.network,
);
};
const getRootPaymentCodeNodeFromSeedHex = (
seedHex: string | Buffer,
network: NetworkCoin = mainnetData,
) => {
if (typeof seedHex === 'string') seedHex = Buffer.from(seedHex, 'hex');
const node: BIP32Interface = bip32.fromSeed(seedHex, network.network);
return node.derivePath(`m/47'/${network.coin}'/0'`);
};
const getRootPaymentCodeNodeFromBIP39Seed = (
bip39Seed: string,
network: NetworkCoin = mainnetData,
password?: string,
) => {
const seed: Buffer = bip39.mnemonicToSeedSync(bip39Seed, password);
return getRootPaymentCodeNodeFromSeedHex(seed);
};
const uintArrayToBuffer = (array: Uint8Array): Buffer => {
const b: Buffer = Buffer.alloc(array.length);
for (let i = 0; i < array.length; i++) b[i] = array[i];
return b;
};
const getSharedSecret = (B: Buffer, a: Buffer): Buffer => {
const S: Buffer = uintArrayToBuffer(
(ecc.pointMultiply(B, a, true) as Buffer).slice(1, 33),
);
let s: Buffer = bitcoin.crypto.sha256(S);
if (!ecc.isPrivate(s))
throw new Error('Shared secret is not a valid private key');
return s;
};
const toInternalByteOrder = (data: Buffer): Buffer => {
let start = 0;
let length = data.length;
while (length - start >= 1) {
const tmp = data[start];
const lastIndex = length - 1;
data[start] = data[lastIndex];
data[lastIndex] = tmp;
length--;
start++;
}
return data;
};
return {
getPublicPaymentCodeNodeFromBase58,
getRootPaymentCodeNodeFromSeedHex,
getRootPaymentCodeNodeFromBIP39Seed,
uintArrayToBuffer,
getSharedSecret,
toInternalByteOrder,
};
}