@neuraiproject/neurai-key 2.5.1 → 2.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +1 -0
- package/index.ts +67 -14
- package/package.json +6 -6
- package/test.js +56 -1
- package/types.ts +7 -0
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
Generate Neurai addresses from a mnemonic phrase following the standards BIP32, BIP39, BIP44.
|
|
4
4
|
|
|
5
5
|
That is, use your 12 words to get addresses for Neurai mainnet and testnet.
|
|
6
|
+
The library also support Evrmorecoin EVR, both mainnet and testnet.
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
## Example get external and internal (change) addresses by path
|
package/index.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
//Gives us meta data about coins/chains
|
|
2
|
-
|
|
3
|
-
import { chains } from "@neuraiproject/chains";
|
|
2
|
+
import { chains } from "@hyperbitjs/chains";
|
|
4
3
|
|
|
5
4
|
//bip39 from mnemonic to seed
|
|
6
5
|
import * as bip39 from "bip39";
|
|
@@ -8,16 +7,20 @@ import * as bip39 from "bip39";
|
|
|
8
7
|
const CoinKey = require("coinkey");
|
|
9
8
|
|
|
10
9
|
//From seed to key
|
|
11
|
-
const HDKey = require("hdkey");
|
|
10
|
+
//const HDKey = require("hdkey");
|
|
11
|
+
import HDKey from "hdkey";
|
|
12
|
+
import { IAddressObject } from "./types";
|
|
12
13
|
|
|
13
14
|
//Could not declare Network as enum, something wrong with parcel bundler
|
|
14
|
-
export type Network = "xna" | "xna-test";
|
|
15
|
+
export type Network = "xna" | "xna-test" | "evr" | "evr-test";
|
|
15
16
|
|
|
16
17
|
function getNetwork(name: Network) {
|
|
17
18
|
const c = name.toLowerCase(); //Just to be sure
|
|
18
19
|
const map = {
|
|
19
|
-
xna: chains.xna.
|
|
20
|
-
"xna-test": chains.xna.
|
|
20
|
+
xna: chains.xna.mainnet.versions,
|
|
21
|
+
"xna-test": chains.xna.testnet?.versions,
|
|
22
|
+
evr: chains.evr.mainnet.versions,
|
|
23
|
+
"evr-test": chains.evr.testnet?.versions,
|
|
21
24
|
};
|
|
22
25
|
|
|
23
26
|
const network = map[c];
|
|
@@ -26,9 +29,17 @@ function getNetwork(name: Network) {
|
|
|
26
29
|
}
|
|
27
30
|
return network;
|
|
28
31
|
}
|
|
29
|
-
|
|
30
32
|
/**
|
|
31
|
-
*
|
|
33
|
+
*
|
|
34
|
+
* @param network
|
|
35
|
+
* @returns the coin type for the network (blockchain), for example Neurai has coin type 175
|
|
36
|
+
*/
|
|
37
|
+
export function getCoinType(network: Network) {
|
|
38
|
+
const chain = getNetwork(network);
|
|
39
|
+
return chain.bip44;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @param network - should have value "xna", "xna-test", "evr" or "evr-test"
|
|
32
43
|
* @param mnemonic - your mnemonic
|
|
33
44
|
* @param account - accounts in BIP44 starts from 0, 0 is the default account
|
|
34
45
|
* @param position - starts from 0
|
|
@@ -40,9 +51,8 @@ export function getAddressPair(
|
|
|
40
51
|
position: number
|
|
41
52
|
) {
|
|
42
53
|
const hdKey = getHDKey(network, mnemonic);
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
//coint_type should always be 1 according to SLIP-0044
|
|
54
|
+
const coin_type = getCoinType(network);
|
|
55
|
+
|
|
46
56
|
//https://github.com/satoshilabs/slips/blob/master/slip-0044.md
|
|
47
57
|
|
|
48
58
|
//Syntax of BIP44
|
|
@@ -68,7 +78,11 @@ export function getHDKey(network: Network, mnemonic: string): any {
|
|
|
68
78
|
return hdKey;
|
|
69
79
|
}
|
|
70
80
|
|
|
71
|
-
export function getAddressByPath(
|
|
81
|
+
export function getAddressByPath(
|
|
82
|
+
network: Network,
|
|
83
|
+
hdKey: any,
|
|
84
|
+
path: string
|
|
85
|
+
): IAddressObject {
|
|
72
86
|
const chain = getNetwork(network);
|
|
73
87
|
const derived = hdKey.derive(path);
|
|
74
88
|
var ck2 = new CoinKey(derived.privateKey, chain);
|
|
@@ -86,7 +100,17 @@ export function generateMnemonic() {
|
|
|
86
100
|
}
|
|
87
101
|
|
|
88
102
|
export function isMnemonicValid(mnemonic: string) {
|
|
89
|
-
|
|
103
|
+
//Check all languages
|
|
104
|
+
const wordlists = Object.values(bip39.wordlists);
|
|
105
|
+
|
|
106
|
+
//If mnemonic is valid in any language, return true, otherwise false
|
|
107
|
+
for (const wordlist of wordlists) {
|
|
108
|
+
const v = bip39.validateMnemonic(mnemonic, wordlist);
|
|
109
|
+
if (v === true) {
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
90
114
|
}
|
|
91
115
|
/**
|
|
92
116
|
*
|
|
@@ -108,12 +132,41 @@ export function getAddressByWIF(network: Network, privateKeyWIF: string) {
|
|
|
108
132
|
|
|
109
133
|
export const entropyToMnemonic = bip39.entropyToMnemonic;
|
|
110
134
|
|
|
135
|
+
export function generateAddressObject(
|
|
136
|
+
network: Network = "xna"
|
|
137
|
+
): IAddressObject {
|
|
138
|
+
const mnemonic = generateMnemonic();
|
|
139
|
+
const account = 0;
|
|
140
|
+
const position = 0;
|
|
141
|
+
const addressPair = getAddressPair(network, mnemonic, account, position);
|
|
142
|
+
const addressObject = addressPair.external;
|
|
143
|
+
|
|
144
|
+
const result = {
|
|
145
|
+
...addressObject,
|
|
146
|
+
mnemonic,
|
|
147
|
+
network,
|
|
148
|
+
};
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Generates a random Address Object
|
|
154
|
+
*
|
|
155
|
+
* @deprecated use generateAddressObject
|
|
156
|
+
* @param network
|
|
157
|
+
* @returns
|
|
158
|
+
*/
|
|
159
|
+
export function generateAddress(network: Network = "xna") {
|
|
160
|
+
return generateAddressObject(network);
|
|
161
|
+
}
|
|
111
162
|
export default {
|
|
112
163
|
entropyToMnemonic,
|
|
164
|
+
generateAddress,
|
|
165
|
+
generateMnemonic,
|
|
113
166
|
getAddressByPath,
|
|
114
167
|
getAddressByWIF,
|
|
115
168
|
getAddressPair,
|
|
169
|
+
getCoinType,
|
|
116
170
|
getHDKey,
|
|
117
|
-
generateMnemonic,
|
|
118
171
|
isMnemonicValid,
|
|
119
172
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@neuraiproject/neurai-key",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.8.1",
|
|
4
4
|
"description": "Generate Neurai addresses from mnemonic code. BIP32, BIP39, BIP44",
|
|
5
5
|
"source": "index.ts",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -27,19 +27,19 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/neuraiproject/neurai-key#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@hyperbitjs/chains": "^
|
|
30
|
+
"@hyperbitjs/chains": "^1.2.0",
|
|
31
31
|
"bip39": "^3.0.4",
|
|
32
32
|
"coinkey": "^3.0.0",
|
|
33
33
|
"hdkey": "^2.0.1"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@parcel/packager-ts": "^2.
|
|
37
|
-
"@parcel/transformer-typescript-types": "^2.
|
|
38
|
-
"@types/hdkey": "^2.0.
|
|
36
|
+
"@parcel/packager-ts": "^2.10.3",
|
|
37
|
+
"@parcel/transformer-typescript-types": "^2.10.3",
|
|
38
|
+
"@types/hdkey": "^2.0.3",
|
|
39
39
|
"@types/node": "^18.14.0",
|
|
40
40
|
"browserify": "^17.0.0",
|
|
41
41
|
"jest": "^29.4.0",
|
|
42
|
-
"parcel": "^2.
|
|
42
|
+
"parcel": "^2.10.3",
|
|
43
43
|
"typescript": "^4.9.4"
|
|
44
44
|
}
|
|
45
45
|
}
|
package/test.js
CHANGED
|
@@ -44,7 +44,7 @@ test("Validate Wallet Import Format (WIF) test-net ", () => {
|
|
|
44
44
|
);
|
|
45
45
|
});
|
|
46
46
|
|
|
47
|
-
test("Validate get public address from Wallet Import Format (WIF) main-
|
|
47
|
+
test("Validate get public address from Wallet Import Format (WIF) main-net ", () => {
|
|
48
48
|
const network = "xna";
|
|
49
49
|
const WIF = "KyWuYcev1hJ7YJZTjWx8coXNRm4jRbMEhgVVVC8vDcTaKRCMASUE";
|
|
50
50
|
const addressObject = NeuraiKey.getAddressByWIF(network, WIF);
|
|
@@ -69,3 +69,58 @@ test("Non valid bytes to mnemonic should fail", () => {
|
|
|
69
69
|
"patient feed learn prison angle convince first napkin uncover track open theory"
|
|
70
70
|
);
|
|
71
71
|
});
|
|
72
|
+
|
|
73
|
+
describe("Validate diff languages", () => {
|
|
74
|
+
it("Should accept spanish mnemonic", () => {
|
|
75
|
+
const m =
|
|
76
|
+
"velero nuera pepino reír barro reforma negar rumbo atento separar pesa puma";
|
|
77
|
+
const valid = NeuraiKey.isMnemonicValid(m);
|
|
78
|
+
expect(valid).toBe(true);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("Should accept French mnemonic", () => {
|
|
82
|
+
const m =
|
|
83
|
+
"vaseux mixte ozone quiétude besogne punaise membre réussir avarice samedi pantalon poney";
|
|
84
|
+
const valid = NeuraiKey.isMnemonicValid(m);
|
|
85
|
+
expect(valid).toBe(true);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("Should accept Italian mnemonic", () => {
|
|
90
|
+
const m =
|
|
91
|
+
"veloce perforare recinto sciroppo bici scelto parabola sguardo avanzato sonnifero remoto rustico";
|
|
92
|
+
const valid = NeuraiKey.isMnemonicValid(m);
|
|
93
|
+
expect(valid).toBe(true);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe("generateAddress", () => {
|
|
97
|
+
it("should generate an address with a mnemonic", () => {
|
|
98
|
+
// Call the function
|
|
99
|
+
const result = NeuraiKey.generateAddressObject();
|
|
100
|
+
|
|
101
|
+
// Assertions
|
|
102
|
+
expect(result).toHaveProperty("mnemonic");
|
|
103
|
+
expect(result.mnemonic).toBeDefined();
|
|
104
|
+
expect(result.network).toBe("xna"); //Test default
|
|
105
|
+
expect(result).toHaveProperty("address"); // replace 'key' with the actual property you expect in addressObject
|
|
106
|
+
// ... you can add more assertions based on the expected structure of the result
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("default network should be xna for Neurai", () => {
|
|
110
|
+
const network = "xna-test";
|
|
111
|
+
// Call the function
|
|
112
|
+
const result = NeuraiKey.generateAddressObject(network);
|
|
113
|
+
// Assertions
|
|
114
|
+
expect(result.network).toBe(network); //Test default
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("Should handle xna-test", () => {
|
|
118
|
+
const network = "xna-test";
|
|
119
|
+
// Call the function
|
|
120
|
+
const result = NeuraiKey.generateAddressObject(network);
|
|
121
|
+
// Assertions
|
|
122
|
+
expect(result.network).toBe(network); //Test default
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// Add more tests if needed to cover different scenarios
|
|
126
|
+
});
|