/
index.ts
26 lines (23 loc) · 1.01 KB
/
index.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
import rippleKeypairs from 'ripple-keypairs';
import { IDeriver } from '..';
import BitcoreLib from 'bitcore-lib';
export class XrpDeriver implements IDeriver {
deriveAddress(network, xpubkey, addressIndex, isChange) {
const xpub = new BitcoreLib.HDPublicKey(xpubkey, network);
const changeNum = isChange ? 1 : 0;
const path = `m/${changeNum}/${addressIndex}`;
const pubKey = xpub.derive(path).toObject().publicKey;
const address = rippleKeypairs.deriveAddress(pubKey);
return address;
}
derivePrivateKey(network, xPriv, addressIndex, isChange) {
const xpriv = new BitcoreLib.HDPrivateKey(xPriv, network);
const changeNum = isChange ? 1 : 0;
const path = `m/${changeNum}/${addressIndex}`;
const derivedXPriv = xpriv.derive(path);
const privKey = derivedXPriv.toObject().privateKey.toUpperCase();
const pubKey = derivedXPriv.hdPublicKey.toObject().publicKey.toUpperCase();
const address = rippleKeypairs.deriveAddress(pubKey);
return { address, privKey, pubKey };
}
}