@neuraiproject/neurai-key 2.5.1 → 2.8.0

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.
Files changed (4) hide show
  1. package/LICENSE +1 -1
  2. package/index.ts +61 -10
  3. package/package.json +6 -6
  4. package/types.ts +7 -0
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 Raven Rebels
3
+ Copyright (c) 2025 Neuraiproject
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/index.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  //Gives us meta data about coins/chains
2
- //import { chains } from "@hyperbitjs/chains";
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,7 +7,9 @@ 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
15
  export type Network = "xna" | "xna-test";
@@ -26,7 +27,15 @@ function getNetwork(name: Network) {
26
27
  }
27
28
  return network;
28
29
  }
29
-
30
+ /**
31
+ *
32
+ * @param network
33
+ * @returns the coin type for the network (blockchain), for example Neurai has coin type 175
34
+ */
35
+ export function getCoinType(network: Network) {
36
+ const chain = getNetwork(network);
37
+ return chain.bip44;
38
+ }
30
39
  /**
31
40
  * @param network - should have value "xna", "xna-test"
32
41
  * @param mnemonic - your mnemonic
@@ -40,9 +49,8 @@ export function getAddressPair(
40
49
  position: number
41
50
  ) {
42
51
  const hdKey = getHDKey(network, mnemonic);
43
- const chain = getNetwork(network);
44
- const coin_type = chain.bip44;
45
- //coint_type should always be 1 according to SLIP-0044
52
+ const coin_type = getCoinType(network);
53
+
46
54
  //https://github.com/satoshilabs/slips/blob/master/slip-0044.md
47
55
 
48
56
  //Syntax of BIP44
@@ -68,7 +76,11 @@ export function getHDKey(network: Network, mnemonic: string): any {
68
76
  return hdKey;
69
77
  }
70
78
 
71
- export function getAddressByPath(network: Network, hdKey: any, path: string) {
79
+ export function getAddressByPath(
80
+ network: Network,
81
+ hdKey: any,
82
+ path: string
83
+ ): IAddressObject {
72
84
  const chain = getNetwork(network);
73
85
  const derived = hdKey.derive(path);
74
86
  var ck2 = new CoinKey(derived.privateKey, chain);
@@ -86,7 +98,17 @@ export function generateMnemonic() {
86
98
  }
87
99
 
88
100
  export function isMnemonicValid(mnemonic: string) {
89
- return bip39.validateMnemonic(mnemonic);
101
+ //Check all languages
102
+ const wordlists = Object.values(bip39.wordlists);
103
+
104
+ //If mnemonic is valid in any language, return true, otherwise false
105
+ for (const wordlist of wordlists) {
106
+ const v = bip39.validateMnemonic(mnemonic, wordlist);
107
+ if (v === true) {
108
+ return true;
109
+ }
110
+ }
111
+ return false;
90
112
  }
91
113
  /**
92
114
  *
@@ -108,12 +130,41 @@ export function getAddressByWIF(network: Network, privateKeyWIF: string) {
108
130
 
109
131
  export const entropyToMnemonic = bip39.entropyToMnemonic;
110
132
 
133
+ export function generateAddressObject(
134
+ network: Network = "xna"
135
+ ): IAddressObject {
136
+ const mnemonic = generateMnemonic();
137
+ const account = 0;
138
+ const position = 0;
139
+ const addressPair = getAddressPair(network, mnemonic, account, position);
140
+ const addressObject = addressPair.external;
141
+
142
+ const result = {
143
+ ...addressObject,
144
+ mnemonic,
145
+ network,
146
+ };
147
+ return result;
148
+ }
149
+
150
+ /**
151
+ * Generates a random Address Object
152
+ *
153
+ * @deprecated use generateAddressObject
154
+ * @param network
155
+ * @returns
156
+ */
157
+ export function generateAddress(network: Network = "xna") {
158
+ return generateAddressObject(network);
159
+ }
111
160
  export default {
112
161
  entropyToMnemonic,
162
+ generateAddress,
163
+ generateMnemonic,
113
164
  getAddressByPath,
114
165
  getAddressByWIF,
115
166
  getAddressPair,
167
+ getCoinType,
116
168
  getHDKey,
117
- generateMnemonic,
118
169
  isMnemonicValid,
119
170
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neuraiproject/neurai-key",
3
- "version": "2.5.1",
3
+ "version": "2.8.0",
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": "^0.2.0",
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.8.3",
37
- "@parcel/transformer-typescript-types": "^2.8.3",
38
- "@types/hdkey": "^2.0.1",
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.8.3",
42
+ "parcel": "^2.10.3",
43
43
  "typescript": "^4.9.4"
44
44
  }
45
45
  }
package/types.ts ADDED
@@ -0,0 +1,7 @@
1
+ export interface IAddressObject {
2
+ address: string;
3
+ mnemonic?: string;
4
+ path: string;
5
+ privateKey: string;
6
+ WIF: string;
7
+ }