-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.ts
172 lines (151 loc) · 4.42 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
//Gives us meta data about coins/chains
import { chains } from "@hyperbitjs/chains";
//bip39 from mnemonic to seed
import * as bip39 from "bip39";
const CoinKey = require("coinkey");
//From seed to key
//const HDKey = require("hdkey");
import HDKey from "hdkey";
import { IAddressObject } from "./types";
//Could not declare Network as enum, something wrong with parcel bundler
export type Network = "rvn" | "rvn-test" | "evr" | "evr-test";
function getNetwork(name: Network) {
const c = name.toLowerCase(); //Just to be sure
const map = {
rvn: chains.rvn.mainnet.versions,
"rvn-test": chains.rvn.testnet?.versions,
evr: chains.evr.mainnet.versions,
"evr-test": chains.evr.testnet?.versions,
};
const network = map[c];
if (!network) {
throw new Error("network must be of value " + Object.keys(map).toString());
}
return network;
}
/**
*
* @param network
* @returns the coin type for the network (blockchain), for example Ravencoin has coin type 175
*/
export function getCoinType(network: Network) {
const chain = getNetwork(network);
return chain.bip44;
}
/**
* @param network - should have value "rvn", "rvn-test", "evr" or "evr-test"
* @param mnemonic - your mnemonic
* @param account - accounts in BIP44 starts from 0, 0 is the default account
* @param position - starts from 0
*/
export function getAddressPair(
network: Network,
mnemonic: string,
account: number,
position: number
) {
const hdKey = getHDKey(network, mnemonic);
const coin_type = getCoinType(network);
//https://github.com/satoshilabs/slips/blob/master/slip-0044.md
//Syntax of BIP44
//m / purpose' / coin_type' / account' / change / address_index
const externalPath = `m/44'/${coin_type}'/${account}'/0/${position}`;
const externalAddress = getAddressByPath(network, hdKey, externalPath);
//change address
const internalPath = `m/44'/${coin_type}'/${account}'/1/${position}`;
const internalAddress = getAddressByPath(network, hdKey, internalPath);
return {
internal: internalAddress,
external: externalAddress,
position,
};
}
export function getHDKey(network: Network, mnemonic: string): any {
const chain = getNetwork(network);
const seed = bip39.mnemonicToSeedSync(mnemonic).toString("hex");
//From the seed, get a hdKey, can we use CoinKey instead?
const hdKey = HDKey.fromMasterSeed(Buffer.from(seed, "hex"), chain.bip32);
return hdKey;
}
export function getAddressByPath(
network: Network,
hdKey: any,
path: string
): IAddressObject {
const chain = getNetwork(network);
const derived = hdKey.derive(path);
var ck2 = new CoinKey(derived.privateKey, chain);
return {
address: ck2.publicAddress,
path: path,
privateKey: ck2.privateKey.toString("hex"),
WIF: ck2.privateWif,
};
}
export function generateMnemonic() {
return bip39.generateMnemonic();
}
export function isMnemonicValid(mnemonic: string) {
//Check all languages
const wordlists = Object.values(bip39.wordlists);
//If mnemonic is valid in any language, return true, otherwise false
for (const wordlist of wordlists) {
const v = bip39.validateMnemonic(mnemonic, wordlist);
if (v === true) {
return true;
}
}
return false;
}
/**
*
* @param privateKeyWIF
* @param network should be "rvn" or "rvn-test"
* @returns object {address, privateKey (hex), WIF}
*/
export function getAddressByWIF(network: Network, privateKeyWIF: string) {
const coinKey = CoinKey.fromWif(privateKeyWIF);
coinKey.versions = getNetwork(network);
return {
address: coinKey.publicAddress,
privateKey: coinKey.privateKey.toString("hex"),
WIF: coinKey.privateWif,
};
}
export const entropyToMnemonic = bip39.entropyToMnemonic;
export function generateAddressObject(
network: Network = "rvn"
): IAddressObject {
const mnemonic = generateMnemonic();
const account = 0;
const position = 0;
const addressPair = getAddressPair(network, mnemonic, account, position);
const addressObject = addressPair.external;
const result = {
...addressObject,
mnemonic,
network,
};
return result;
}
/**
* Generates a random Address Object
*
* @deprecated use generateAddressObject
* @param network
* @returns
*/
export function generateAddress(network: Network = "rvn") {
return generateAddressObject(network);
}
export default {
entropyToMnemonic,
generateAddress,
generateMnemonic,
getAddressByPath,
getAddressByWIF,
getAddressPair,
getCoinType,
getHDKey,
isMnemonicValid,
};