@neuraiproject/neurai-key 2.8.3 → 2.8.5

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/dist/main.js CHANGED
@@ -1,13 +1,11 @@
1
1
  var $g5Y9E$hyperbitjschains = require("@hyperbitjs/chains");
2
2
  var $g5Y9E$bip39 = require("bip39");
3
+ var $g5Y9E$crypto = require("crypto");
3
4
  var $g5Y9E$coinkey = require("coinkey");
4
5
  var $g5Y9E$hdkey = require("hdkey");
6
+ var $g5Y9E$bs58check = require("bs58check");
5
7
 
6
8
 
7
- function $parcel$interopDefault(a) {
8
- return a && a.__esModule ? a.default : a;
9
- }
10
-
11
9
  function $parcel$defineInteropFlag(a) {
12
10
  Object.defineProperty(a, '__esModule', {value: true, configurable: true});
13
11
  }
@@ -27,6 +25,7 @@ $parcel$export(module.exports, "isMnemonicValid", () => $80bd448eb6ea085b$export
27
25
  $parcel$export(module.exports, "getAddressByWIF", () => $80bd448eb6ea085b$export$f43d70cb4ddd5664);
28
26
  $parcel$export(module.exports, "entropyToMnemonic", () => $80bd448eb6ea085b$export$4becd65eb23312e6);
29
27
  $parcel$export(module.exports, "generateAddressObject", () => $80bd448eb6ea085b$export$de190b37be25f71b);
28
+ $parcel$export(module.exports, "publicKeyToAddress", () => $80bd448eb6ea085b$export$462669520a9d12d1);
30
29
  $parcel$export(module.exports, "generateAddress", () => $80bd448eb6ea085b$export$e2e336010351d8a8);
31
30
  $parcel$export(module.exports, "default", () => $80bd448eb6ea085b$export$2e2bcd8739ae039);
32
31
  //Gives us meta data about coins/chains
@@ -34,13 +33,14 @@ $parcel$export(module.exports, "default", () => $80bd448eb6ea085b$export$2e2bcd8
34
33
 
35
34
 
36
35
 
36
+
37
+
37
38
  function $80bd448eb6ea085b$var$getNetwork(name) {
38
39
  const c = name.toLowerCase(); //Just to be sure
40
+ const chainData = (0, $g5Y9E$hyperbitjschains.chains);
39
41
  const map = {
40
- xna: (0, $g5Y9E$hyperbitjschains.chains).xna.mainnet.versions,
41
- "xna-test": (0, $g5Y9E$hyperbitjschains.chains).xna.testnet?.versions,
42
- evr: (0, $g5Y9E$hyperbitjschains.chains).evr.mainnet.versions,
43
- "evr-test": (0, $g5Y9E$hyperbitjschains.chains).evr.testnet?.versions
42
+ xna: chainData.xna.mainnet.versions,
43
+ "xna-test": chainData.xna.testnet?.versions
44
44
  };
45
45
  const network = map[c];
46
46
  if (!network) throw new Error("network must be of value " + Object.keys(map).toString());
@@ -50,8 +50,8 @@ function $80bd448eb6ea085b$export$23109f16a8a07245(network) {
50
50
  const chain = $80bd448eb6ea085b$var$getNetwork(network);
51
51
  return chain.bip44;
52
52
  }
53
- function $80bd448eb6ea085b$export$6e3ac79f8c0a2892(network, mnemonic, account, position) {
54
- const hdKey = $80bd448eb6ea085b$export$6c78ccde21ad48f6(network, mnemonic);
53
+ function $80bd448eb6ea085b$export$6e3ac79f8c0a2892(network, mnemonic, account, position, passphrase = "") {
54
+ const hdKey = $80bd448eb6ea085b$export$6c78ccde21ad48f6(network, mnemonic, passphrase);
55
55
  const coin_type = $80bd448eb6ea085b$export$23109f16a8a07245(network);
56
56
  //https://github.com/satoshilabs/slips/blob/master/slip-0044.md
57
57
  //Syntax of BIP44
@@ -67,11 +67,11 @@ function $80bd448eb6ea085b$export$6e3ac79f8c0a2892(network, mnemonic, account, p
67
67
  position: position
68
68
  };
69
69
  }
70
- function $80bd448eb6ea085b$export$6c78ccde21ad48f6(network, mnemonic) {
70
+ function $80bd448eb6ea085b$export$6c78ccde21ad48f6(network, mnemonic, passphrase = "") {
71
71
  const chain = $80bd448eb6ea085b$var$getNetwork(network);
72
- const seed = $g5Y9E$bip39.mnemonicToSeedSync(mnemonic).toString("hex");
72
+ const seed = $g5Y9E$bip39.mnemonicToSeedSync(mnemonic, passphrase).toString("hex");
73
73
  //From the seed, get a hdKey, can we use CoinKey instead?
74
- const hdKey = (0, ($parcel$interopDefault($g5Y9E$hdkey))).fromMasterSeed(Buffer.from(seed, "hex"), chain.bip32);
74
+ const hdKey = $g5Y9E$hdkey.fromMasterSeed(Buffer.from(seed, "hex"), chain.bip32);
75
75
  return hdKey;
76
76
  }
77
77
  function $80bd448eb6ea085b$export$6fc951b76952b95e(network, hdKey, path) {
@@ -81,6 +81,7 @@ function $80bd448eb6ea085b$export$6fc951b76952b95e(network, hdKey, path) {
81
81
  return {
82
82
  address: ck2.publicAddress,
83
83
  path: path,
84
+ publicKey: ck2.publicKey.toString("hex"),
84
85
  privateKey: ck2.privateKey.toString("hex"),
85
86
  WIF: ck2.privateWif
86
87
  };
@@ -108,11 +109,11 @@ function $80bd448eb6ea085b$export$f43d70cb4ddd5664(network, privateKeyWIF) {
108
109
  };
109
110
  }
110
111
  const $80bd448eb6ea085b$export$4becd65eb23312e6 = $g5Y9E$bip39.entropyToMnemonic;
111
- function $80bd448eb6ea085b$export$de190b37be25f71b(network = "xna") {
112
+ function $80bd448eb6ea085b$export$de190b37be25f71b(network = "xna", passphrase = "") {
112
113
  const mnemonic = $80bd448eb6ea085b$export$9f993213e5806bf0();
113
114
  const account = 0;
114
115
  const position = 0;
115
- const addressPair = $80bd448eb6ea085b$export$6e3ac79f8c0a2892(network, mnemonic, account, position);
116
+ const addressPair = $80bd448eb6ea085b$export$6e3ac79f8c0a2892(network, mnemonic, account, position, passphrase);
116
117
  const addressObject = addressPair.external;
117
118
  const result = {
118
119
  ...addressObject,
@@ -121,6 +122,20 @@ function $80bd448eb6ea085b$export$de190b37be25f71b(network = "xna") {
121
122
  };
122
123
  return result;
123
124
  }
125
+ function $80bd448eb6ea085b$export$462669520a9d12d1(network, publicKey) {
126
+ const chain = $80bd448eb6ea085b$var$getNetwork(network);
127
+ const keyBuffer = Buffer.isBuffer(publicKey) ? publicKey : Buffer.from(publicKey, "hex");
128
+ if (keyBuffer.length !== 33 && keyBuffer.length !== 65) throw new Error("Public key must be 33 or 65 bytes");
129
+ const sha256Hash = (0, $g5Y9E$crypto.createHash)("sha256").update(keyBuffer).digest();
130
+ const ripemd160Hash = (0, $g5Y9E$crypto.createHash)("ripemd160").update(sha256Hash).digest();
131
+ const payload = Buffer.concat([
132
+ Buffer.from([
133
+ chain.public
134
+ ]),
135
+ ripemd160Hash
136
+ ]);
137
+ return $g5Y9E$bs58check.encode(payload);
138
+ }
124
139
  function $80bd448eb6ea085b$export$e2e336010351d8a8(network = "xna") {
125
140
  return $80bd448eb6ea085b$export$de190b37be25f71b(network);
126
141
  }
@@ -133,7 +148,8 @@ var $80bd448eb6ea085b$export$2e2bcd8739ae039 = {
133
148
  getAddressPair: $80bd448eb6ea085b$export$6e3ac79f8c0a2892,
134
149
  getCoinType: $80bd448eb6ea085b$export$23109f16a8a07245,
135
150
  getHDKey: $80bd448eb6ea085b$export$6c78ccde21ad48f6,
136
- isMnemonicValid: $80bd448eb6ea085b$export$2b99b9ff149202f3
151
+ isMnemonicValid: $80bd448eb6ea085b$export$2b99b9ff149202f3,
152
+ publicKeyToAddress: $80bd448eb6ea085b$export$462669520a9d12d1
137
153
  };
138
154
 
139
155
 
package/dist/main.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uCAAuC;;;;;AAgBvC,SAAS,iCAAW,IAAa;IAC/B,MAAM,IAAI,KAAK,WAAW,IAAI,iBAAiB;IAC/C,MAAM,MAAM;QACV,KAAK,CAAA,GAAA,8BAAK,EAAE,GAAG,CAAC,OAAO,CAAC,QAAQ;QAChC,YAAY,CAAA,GAAA,8BAAK,EAAE,GAAG,CAAC,OAAO,EAAE;QAChC,KAAK,CAAA,GAAA,8BAAK,EAAE,GAAG,CAAC,OAAO,CAAC,QAAQ;QAChC,YAAY,CAAA,GAAA,8BAAK,EAAE,GAAG,CAAC,OAAO,EAAE;IAClC;IAEA,MAAM,UAAU,GAAG,CAAC,EAAE;IACtB,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,8BAA8B,OAAO,IAAI,CAAC,KAAK,QAAQ;IAEzE,O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AK,MAAM,YAAY,UAAW;QAChC,MAAM,IAAI,8BAAuB,UAAU;QAC3C,IAAI,MAAM,MACR,OAAO;IAEX;IACA,OAAO;AACT;AAQO,SAAS,0CAAgB,OAAgB,EAAE,aAAqB;IACrE,MAAM,UAAU,eAAQ,OAAO,CAAC;IAChC,QAAQ,QAAQ,GAAG,iCAAW;IAE9B,OAAO;QACL,SAAS,QAAQ,aAAa;QAC9B,YAAY,QAAQ,UAAU,CAAC,QAAQ,CAAC;QACxC,KAAK,QAAQ,UAAU;IACzB;AACF;AAEO,MAAM,4CAAoB;AAE1B,SAAS,0CACd,UAAmB,KAAK;IAExB,MAAM,WAAW;IACjB,MAAM,UAAU;IAChB,MAAM,WAAW;IACjB,MAAM,cAAc,0CAAe,SAAS,UAAU,SAAS;IAC/D,MAAM,gBAAgB,YAAY,QAAQ;IAE1C,MAAM,SAAS;QACb,GAAG,aAAa;kBAChB;iBACA;IACF;IACA,OAAO;AACT;AASO,SAAS,0CAAgB,UAAmB,KAAK;IACtD,OAAO,0CAAsB;AAC/B;IACA,2CAAe;uBACb;qBACA;sBACA;sBACA;qBACA;oBACA;iBACA;cACA;qBACA;AACF","sources":["index.ts"],"sourcesContent":["//Gives us meta data about coins/chains\nimport { chains } from \"@hyperbitjs/chains\";\n\n//bip39 from mnemonic to seed\nimport * as bip39 from \"bip39\";\n\nconst CoinKey = require(\"coinkey\");\n\n//From seed to key\n//const HDKey = require(\"hdkey\");\nimport HDKey from \"hdkey\";\nimport { IAddressObject } from \"./types\";\n\n//Could not declare Network as enum, something wrong with parcel bundler\nexport type Network = \"xna\" | \"xna-test\" | \"evr\" | \"evr-test\";\n\nfunction getNetwork(name: Network) {\n const c = name.toLowerCase(); //Just to be sure\n const map = {\n xna: chains.xna.mainnet.versions,\n \"xna-test\": chains.xna.testnet?.versions,\n evr: chains.evr.mainnet.versions,\n \"evr-test\": chains.evr.testnet?.versions,\n };\n\n const network = map[c];\n if (!network) {\n throw new Error(\"network must be of value \" + Object.keys(map).toString());\n }\n return network;\n}\n/**\n *\n * @param network\n * @returns the coin type for the network (blockchain), for example Neurai has coin type 175\n */\nexport function getCoinType(network: Network) {\n const chain = getNetwork(network);\n return chain.bip44;\n}\n/**\n * @param network - should have value \"xna\", \"xna-test\", \"evr\" or \"evr-test\"\n * @param mnemonic - your mnemonic\n * @param account - accounts in BIP44 starts from 0, 0 is the default account\n * @param position - starts from 0\n */\nexport function getAddressPair(\n network: Network,\n mnemonic: string,\n account: number,\n position: number\n) {\n const hdKey = getHDKey(network, mnemonic);\n const coin_type = getCoinType(network);\n\n //https://github.com/satoshilabs/slips/blob/master/slip-0044.md\n\n //Syntax of BIP44\n //m / purpose' / coin_type' / account' / change / address_index\n const externalPath = `m/44'/${coin_type}'/${account}'/0/${position}`;\n const externalAddress = getAddressByPath(network, hdKey, externalPath);\n\n //change address\n const internalPath = `m/44'/${coin_type}'/${account}'/1/${position}`;\n const internalAddress = getAddressByPath(network, hdKey, internalPath);\n return {\n internal: internalAddress,\n external: externalAddress,\n position,\n };\n}\n\nexport function getHDKey(network: Network, mnemonic: string): any {\n const chain = getNetwork(network);\n const seed = bip39.mnemonicToSeedSync(mnemonic).toString(\"hex\");\n //From the seed, get a hdKey, can we use CoinKey instead?\n const hdKey = HDKey.fromMasterSeed(Buffer.from(seed, \"hex\"), chain.bip32);\n return hdKey;\n}\n\nexport function getAddressByPath(\n network: Network,\n hdKey: any,\n path: string\n): IAddressObject {\n const chain = getNetwork(network);\n const derived = hdKey.derive(path);\n var ck2 = new CoinKey(derived.privateKey, chain);\n\n return {\n address: ck2.publicAddress,\n path: path,\n privateKey: ck2.privateKey.toString(\"hex\"),\n WIF: ck2.privateWif,\n };\n}\n\nexport function generateMnemonic() {\n return bip39.generateMnemonic();\n}\n\nexport function isMnemonicValid(mnemonic: string) {\n //Check all languages\n const wordlists = Object.values(bip39.wordlists);\n\n //If mnemonic is valid in any language, return true, otherwise false\n for (const wordlist of wordlists) {\n const v = bip39.validateMnemonic(mnemonic, wordlist);\n if (v === true) {\n return true;\n }\n }\n return false;\n}\n/**\n *\n * @param privateKeyWIF\n * @param network should be \"xna\" or \"xna-test\"\n * @returns object {address, privateKey (hex), WIF}\n */\n\nexport function getAddressByWIF(network: Network, privateKeyWIF: string) {\n const coinKey = CoinKey.fromWif(privateKeyWIF);\n coinKey.versions = getNetwork(network);\n\n return {\n address: coinKey.publicAddress,\n privateKey: coinKey.privateKey.toString(\"hex\"),\n WIF: coinKey.privateWif,\n };\n}\n\nexport const entropyToMnemonic = bip39.entropyToMnemonic;\n\nexport function generateAddressObject(\n network: Network = \"xna\"\n): IAddressObject {\n const mnemonic = generateMnemonic();\n const account = 0;\n const position = 0;\n const addressPair = getAddressPair(network, mnemonic, account, position);\n const addressObject = addressPair.external;\n\n const result = {\n ...addressObject,\n mnemonic,\n network,\n };\n return result;\n}\n\n/**\n * Generates a random Address Object\n *\n * @deprecated use generateAddressObject\n * @param network\n * @returns\n */\nexport function generateAddress(network: Network = \"xna\") {\n return generateAddressObject(network);\n}\nexport default {\n entropyToMnemonic,\n generateAddress,\n generateMnemonic,\n getAddressByPath,\n getAddressByWIF,\n getAddressPair,\n getCoinType,\n getHDKey,\n isMnemonicValid,\n};\n"],"names":[],"version":3,"file":"main.js.map"}
1
+ {"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uCAAuC;;;;;;;AAiBvC,SAAS,iCAAW,IAAa;IAC/B,MAAM,IAAI,KAAK,WAAW,IAAe,iBAAiB;IAC1D,MAAM,YAAY,CAAA,GAAA,8BAAK;IACvB,MAAM,MAA4B;QAChC,KAAK,UAAU,GAAG,CAAC,OAAO,CAAC,QAAQ;QACnC,YAAY,UAAU,GAAG,CAAC,OAAO,EAAE;IACrC;IAEA,MAAM,UAAU,GAAG,CAAC,EAAE;IACtB,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,8BAA8B,OAAO,IAAI,CAAC,KAAK,QAAQ;IAEzE,OAAO;AACT;AAMO,SAAS,0CAAY,O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BACA;sBACA;qBACA;oBACA;iBACA;cACA;qBACA;wBACA;AACF","sources":["index.ts"],"sourcesContent":["//Gives us meta data about coins/chains\nimport { chains } from \"@hyperbitjs/chains\";\n\n//bip39 from mnemonic to seed\nimport * as bip39 from \"bip39\";\n\nimport { createHash } from \"crypto\";\nconst CoinKey = require(\"coinkey\");\n\n//From seed to key\nconst HDKey = require(\"hdkey\");\nimport { IAddressObject } from \"./types\";\nconst bs58check = require(\"bs58check\");\n\n//Could not declare Network as enum, something wrong with parcel bundler\nexport type Network = \"xna\" | \"xna-test\";\n\nfunction getNetwork(name: Network) {\n const c = name.toLowerCase() as Network; //Just to be sure\n const chainData = chains as any;\n const map: Record<Network, any> = {\n xna: chainData.xna.mainnet.versions,\n \"xna-test\": chainData.xna.testnet?.versions,\n };\n\n const network = map[c];\n if (!network) {\n throw new Error(\"network must be of value \" + Object.keys(map).toString());\n }\n return network;\n}\n/**\n *\n * @param network\n * @returns the coin type for the network (blockchain), for example Neurai has coin type 175\n */\nexport function getCoinType(network: Network) {\n const chain = getNetwork(network);\n return chain.bip44;\n}\n/**\n * @param network - should have value \"xna\", \"xna-test\", \"evr\" or \"evr-test\"\n * @param mnemonic - your mnemonic\n * @param account - accounts in BIP44 starts from 0, 0 is the default account\n * @param position - starts from 0\n * @param passphrase - optional BIP39 passphrase (25th word) for additional security\n */\nexport function getAddressPair(\n network: Network,\n mnemonic: string,\n account: number,\n position: number,\n passphrase: string = \"\"\n) {\n const hdKey = getHDKey(network, mnemonic, passphrase);\n const coin_type = getCoinType(network);\n\n //https://github.com/satoshilabs/slips/blob/master/slip-0044.md\n\n //Syntax of BIP44\n //m / purpose' / coin_type' / account' / change / address_index\n const externalPath = `m/44'/${coin_type}'/${account}'/0/${position}`;\n const externalAddress = getAddressByPath(network, hdKey, externalPath);\n\n //change address\n const internalPath = `m/44'/${coin_type}'/${account}'/1/${position}`;\n const internalAddress = getAddressByPath(network, hdKey, internalPath);\n return {\n internal: internalAddress,\n external: externalAddress,\n position,\n };\n}\n\nexport function getHDKey(network: Network, mnemonic: string, passphrase: string = \"\"): any {\n const chain = getNetwork(network);\n const seed = bip39.mnemonicToSeedSync(mnemonic, passphrase).toString(\"hex\");\n //From the seed, get a hdKey, can we use CoinKey instead?\n const hdKey = HDKey.fromMasterSeed(Buffer.from(seed, \"hex\"), chain.bip32);\n return hdKey;\n}\n\nexport function getAddressByPath(\n network: Network,\n hdKey: any,\n path: string\n): IAddressObject {\n const chain = getNetwork(network);\n const derived = hdKey.derive(path);\n var ck2 = new CoinKey(derived.privateKey, chain);\n\n return {\n address: ck2.publicAddress,\n path: path,\n publicKey: ck2.publicKey.toString(\"hex\"),\n privateKey: ck2.privateKey.toString(\"hex\"),\n WIF: ck2.privateWif,\n };\n}\n\nexport function generateMnemonic() {\n return bip39.generateMnemonic();\n}\n\nexport function isMnemonicValid(mnemonic: string) {\n //Check all languages\n const wordlists = Object.values(bip39.wordlists);\n\n //If mnemonic is valid in any language, return true, otherwise false\n for (const wordlist of wordlists) {\n const v = bip39.validateMnemonic(mnemonic, wordlist);\n if (v === true) {\n return true;\n }\n }\n return false;\n}\n/**\n *\n * @param privateKeyWIF\n * @param network should be \"xna\" or \"xna-test\"\n * @returns object {address, privateKey (hex), WIF}\n */\n\nexport function getAddressByWIF(network: Network, privateKeyWIF: string) {\n const coinKey = CoinKey.fromWif(privateKeyWIF);\n coinKey.versions = getNetwork(network);\n\n return {\n address: coinKey.publicAddress,\n privateKey: coinKey.privateKey.toString(\"hex\"),\n WIF: coinKey.privateWif,\n };\n}\n\nexport const entropyToMnemonic = bip39.entropyToMnemonic;\n\nexport function generateAddressObject(\n network: Network = \"xna\",\n passphrase: string = \"\"\n): IAddressObject {\n const mnemonic = generateMnemonic();\n const account = 0;\n const position = 0;\n const addressPair = getAddressPair(network, mnemonic, account, position, passphrase);\n const addressObject = addressPair.external;\n\n const result = {\n ...addressObject,\n mnemonic,\n network,\n };\n return result;\n}\n\nexport function publicKeyToAddress(\n network: Network,\n publicKey: Buffer | string\n): string {\n const chain = getNetwork(network);\n const keyBuffer = Buffer.isBuffer(publicKey)\n ? publicKey\n : Buffer.from(publicKey, \"hex\");\n\n if (keyBuffer.length !== 33 && keyBuffer.length !== 65) {\n throw new Error(\"Public key must be 33 or 65 bytes\");\n }\n\n const sha256Hash = createHash(\"sha256\").update(keyBuffer).digest();\n const ripemd160Hash = createHash(\"ripemd160\").update(sha256Hash).digest();\n const payload = Buffer.concat([\n Buffer.from([chain.public]),\n ripemd160Hash,\n ]);\n\n return bs58check.encode(payload);\n}\n\n/**\n * Generates a random Address Object\n *\n * @deprecated use generateAddressObject\n * @param network\n * @returns\n */\nexport function generateAddress(network: Network = \"xna\") {\n return generateAddressObject(network);\n}\nexport default {\n entropyToMnemonic,\n generateAddress,\n generateMnemonic,\n getAddressByPath,\n getAddressByWIF,\n getAddressPair,\n getCoinType,\n getHDKey,\n isMnemonicValid,\n publicKeyToAddress,\n};\n"],"names":[],"version":3,"file":"main.js.map"}
package/dist/module.js CHANGED
@@ -1,20 +1,23 @@
1
1
  import {chains as $hCgyA$chains} from "@hyperbitjs/chains";
2
2
  import {mnemonicToSeedSync as $hCgyA$mnemonicToSeedSync, generateMnemonic as $hCgyA$generateMnemonic, wordlists as $hCgyA$wordlists, validateMnemonic as $hCgyA$validateMnemonic, entropyToMnemonic as $hCgyA$entropyToMnemonic} from "bip39";
3
+ import {createHash as $hCgyA$createHash} from "crypto";
3
4
  import * as $hCgyA$coinkey from "coinkey";
4
- import $hCgyA$hdkey from "hdkey";
5
+ import {fromMasterSeed as $hCgyA$fromMasterSeed} from "hdkey";
6
+ import {encode as $hCgyA$encode} from "bs58check";
5
7
 
6
8
  //Gives us meta data about coins/chains
7
9
 
8
10
 
9
11
 
10
12
 
13
+
14
+
11
15
  function $c3f6c693698dc7cd$var$getNetwork(name) {
12
16
  const c = name.toLowerCase(); //Just to be sure
17
+ const chainData = (0, $hCgyA$chains);
13
18
  const map = {
14
- xna: (0, $hCgyA$chains).xna.mainnet.versions,
15
- "xna-test": (0, $hCgyA$chains).xna.testnet?.versions,
16
- evr: (0, $hCgyA$chains).evr.mainnet.versions,
17
- "evr-test": (0, $hCgyA$chains).evr.testnet?.versions
19
+ xna: chainData.xna.mainnet.versions,
20
+ "xna-test": chainData.xna.testnet?.versions
18
21
  };
19
22
  const network = map[c];
20
23
  if (!network) throw new Error("network must be of value " + Object.keys(map).toString());
@@ -24,8 +27,8 @@ function $c3f6c693698dc7cd$export$23109f16a8a07245(network) {
24
27
  const chain = $c3f6c693698dc7cd$var$getNetwork(network);
25
28
  return chain.bip44;
26
29
  }
27
- function $c3f6c693698dc7cd$export$6e3ac79f8c0a2892(network, mnemonic, account, position) {
28
- const hdKey = $c3f6c693698dc7cd$export$6c78ccde21ad48f6(network, mnemonic);
30
+ function $c3f6c693698dc7cd$export$6e3ac79f8c0a2892(network, mnemonic, account, position, passphrase = "") {
31
+ const hdKey = $c3f6c693698dc7cd$export$6c78ccde21ad48f6(network, mnemonic, passphrase);
29
32
  const coin_type = $c3f6c693698dc7cd$export$23109f16a8a07245(network);
30
33
  //https://github.com/satoshilabs/slips/blob/master/slip-0044.md
31
34
  //Syntax of BIP44
@@ -41,11 +44,11 @@ function $c3f6c693698dc7cd$export$6e3ac79f8c0a2892(network, mnemonic, account, p
41
44
  position: position
42
45
  };
43
46
  }
44
- function $c3f6c693698dc7cd$export$6c78ccde21ad48f6(network, mnemonic) {
47
+ function $c3f6c693698dc7cd$export$6c78ccde21ad48f6(network, mnemonic, passphrase = "") {
45
48
  const chain = $c3f6c693698dc7cd$var$getNetwork(network);
46
- const seed = $hCgyA$mnemonicToSeedSync(mnemonic).toString("hex");
49
+ const seed = $hCgyA$mnemonicToSeedSync(mnemonic, passphrase).toString("hex");
47
50
  //From the seed, get a hdKey, can we use CoinKey instead?
48
- const hdKey = (0, $hCgyA$hdkey).fromMasterSeed(Buffer.from(seed, "hex"), chain.bip32);
51
+ const hdKey = $hCgyA$fromMasterSeed(Buffer.from(seed, "hex"), chain.bip32);
49
52
  return hdKey;
50
53
  }
51
54
  function $c3f6c693698dc7cd$export$6fc951b76952b95e(network, hdKey, path) {
@@ -55,6 +58,7 @@ function $c3f6c693698dc7cd$export$6fc951b76952b95e(network, hdKey, path) {
55
58
  return {
56
59
  address: ck2.publicAddress,
57
60
  path: path,
61
+ publicKey: ck2.publicKey.toString("hex"),
58
62
  privateKey: ck2.privateKey.toString("hex"),
59
63
  WIF: ck2.privateWif
60
64
  };
@@ -82,11 +86,11 @@ function $c3f6c693698dc7cd$export$f43d70cb4ddd5664(network, privateKeyWIF) {
82
86
  };
83
87
  }
84
88
  const $c3f6c693698dc7cd$export$4becd65eb23312e6 = $hCgyA$entropyToMnemonic;
85
- function $c3f6c693698dc7cd$export$de190b37be25f71b(network = "xna") {
89
+ function $c3f6c693698dc7cd$export$de190b37be25f71b(network = "xna", passphrase = "") {
86
90
  const mnemonic = $c3f6c693698dc7cd$export$9f993213e5806bf0();
87
91
  const account = 0;
88
92
  const position = 0;
89
- const addressPair = $c3f6c693698dc7cd$export$6e3ac79f8c0a2892(network, mnemonic, account, position);
93
+ const addressPair = $c3f6c693698dc7cd$export$6e3ac79f8c0a2892(network, mnemonic, account, position, passphrase);
90
94
  const addressObject = addressPair.external;
91
95
  const result = {
92
96
  ...addressObject,
@@ -95,6 +99,20 @@ function $c3f6c693698dc7cd$export$de190b37be25f71b(network = "xna") {
95
99
  };
96
100
  return result;
97
101
  }
102
+ function $c3f6c693698dc7cd$export$462669520a9d12d1(network, publicKey) {
103
+ const chain = $c3f6c693698dc7cd$var$getNetwork(network);
104
+ const keyBuffer = Buffer.isBuffer(publicKey) ? publicKey : Buffer.from(publicKey, "hex");
105
+ if (keyBuffer.length !== 33 && keyBuffer.length !== 65) throw new Error("Public key must be 33 or 65 bytes");
106
+ const sha256Hash = (0, $hCgyA$createHash)("sha256").update(keyBuffer).digest();
107
+ const ripemd160Hash = (0, $hCgyA$createHash)("ripemd160").update(sha256Hash).digest();
108
+ const payload = Buffer.concat([
109
+ Buffer.from([
110
+ chain.public
111
+ ]),
112
+ ripemd160Hash
113
+ ]);
114
+ return $hCgyA$encode(payload);
115
+ }
98
116
  function $c3f6c693698dc7cd$export$e2e336010351d8a8(network = "xna") {
99
117
  return $c3f6c693698dc7cd$export$de190b37be25f71b(network);
100
118
  }
@@ -107,9 +125,10 @@ var $c3f6c693698dc7cd$export$2e2bcd8739ae039 = {
107
125
  getAddressPair: $c3f6c693698dc7cd$export$6e3ac79f8c0a2892,
108
126
  getCoinType: $c3f6c693698dc7cd$export$23109f16a8a07245,
109
127
  getHDKey: $c3f6c693698dc7cd$export$6c78ccde21ad48f6,
110
- isMnemonicValid: $c3f6c693698dc7cd$export$2b99b9ff149202f3
128
+ isMnemonicValid: $c3f6c693698dc7cd$export$2b99b9ff149202f3,
129
+ publicKeyToAddress: $c3f6c693698dc7cd$export$462669520a9d12d1
111
130
  };
112
131
 
113
132
 
114
- export {$c3f6c693698dc7cd$export$23109f16a8a07245 as getCoinType, $c3f6c693698dc7cd$export$6e3ac79f8c0a2892 as getAddressPair, $c3f6c693698dc7cd$export$6c78ccde21ad48f6 as getHDKey, $c3f6c693698dc7cd$export$6fc951b76952b95e as getAddressByPath, $c3f6c693698dc7cd$export$9f993213e5806bf0 as generateMnemonic, $c3f6c693698dc7cd$export$2b99b9ff149202f3 as isMnemonicValid, $c3f6c693698dc7cd$export$f43d70cb4ddd5664 as getAddressByWIF, $c3f6c693698dc7cd$export$4becd65eb23312e6 as entropyToMnemonic, $c3f6c693698dc7cd$export$de190b37be25f71b as generateAddressObject, $c3f6c693698dc7cd$export$e2e336010351d8a8 as generateAddress, $c3f6c693698dc7cd$export$2e2bcd8739ae039 as default};
133
+ export {$c3f6c693698dc7cd$export$23109f16a8a07245 as getCoinType, $c3f6c693698dc7cd$export$6e3ac79f8c0a2892 as getAddressPair, $c3f6c693698dc7cd$export$6c78ccde21ad48f6 as getHDKey, $c3f6c693698dc7cd$export$6fc951b76952b95e as getAddressByPath, $c3f6c693698dc7cd$export$9f993213e5806bf0 as generateMnemonic, $c3f6c693698dc7cd$export$2b99b9ff149202f3 as isMnemonicValid, $c3f6c693698dc7cd$export$f43d70cb4ddd5664 as getAddressByWIF, $c3f6c693698dc7cd$export$4becd65eb23312e6 as entropyToMnemonic, $c3f6c693698dc7cd$export$de190b37be25f71b as generateAddressObject, $c3f6c693698dc7cd$export$462669520a9d12d1 as publicKeyToAddress, $c3f6c693698dc7cd$export$e2e336010351d8a8 as generateAddress, $c3f6c693698dc7cd$export$2e2bcd8739ae039 as default};
115
134
  //# sourceMappingURL=module.js.map
@@ -1 +1 @@
1
- {"mappings":";;;;;AAAA,uCAAuC;;;;;AAgBvC,SAAS,iCAAW,IAAa;IAC/B,MAAM,IAAI,KAAK,WAAW,IAAI,iBAAiB;IAC/C,MAAM,MAAM;QACV,KAAK,CAAA,GAAA,aAAK,EAAE,GAAG,CAAC,OAAO,CAAC,QAAQ;QAChC,YAAY,CAAA,GAAA,aAAK,EAAE,GAAG,CAAC,OAAO,EAAE;QAChC,KAAK,CAAA,GAAA,aAAK,EAAE,GAAG,CAAC,OAAO,CAAC,QAAQ;QAChC,YAAY,CAAA,GAAA,aAAK,EAAE,GAAG,CAAC,OAAO,EAAE;IAClC;IAEA,MAAM,UAAU,GAAG,CAAC,EAAE;IACtB,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,8BAA8B,OAAO,IAAI,CAAC,KAAK,QAAQ;IAEzE,OAAO;AACT;AAMO,SAAS,0CAAY,OAAgB;IAC1C,MAAM,QAAQ,iCAAW;IACzB,OAAO,MAAM,KAAK;AACpB;AAOO,SAAS,0CACd,OAAgB,EAChB,QAAgB,EAChB,OAAe,EACf,QAAgB;IAEhB,MAAM,QAAQ,0CAAS,SAAS;IAChC,MAAM,YAAY,0CAAY;IAE9B,+DAA+D;IAE/D,iBAAiB;IACjB,+DAA+D;IAC/D,MAAM,eAAe,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE,QAAQ,IAAI,EA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ACF;AAEO,MAAM,4CAAoB;AAE1B,SAAS,0CACd,UAAmB,KAAK;IAExB,MAAM,WAAW;IACjB,MAAM,UAAU;IAChB,MAAM,WAAW;IACjB,MAAM,cAAc,0CAAe,SAAS,UAAU,SAAS;IAC/D,MAAM,gBAAgB,YAAY,QAAQ;IAE1C,MAAM,SAAS;QACb,GAAG,aAAa;kBAChB;iBACA;IACF;IACA,OAAO;AACT;AASO,SAAS,0CAAgB,UAAmB,KAAK;IACtD,OAAO,0CAAsB;AAC/B;IACA,2CAAe;uBACb;qBACA;sBACA;sBACA;qBACA;oBACA;iBACA;cACA;qBACA;AACF","sources":["index.ts"],"sourcesContent":["//Gives us meta data about coins/chains\nimport { chains } from \"@hyperbitjs/chains\";\n\n//bip39 from mnemonic to seed\nimport * as bip39 from \"bip39\";\n\nconst CoinKey = require(\"coinkey\");\n\n//From seed to key\n//const HDKey = require(\"hdkey\");\nimport HDKey from \"hdkey\";\nimport { IAddressObject } from \"./types\";\n\n//Could not declare Network as enum, something wrong with parcel bundler\nexport type Network = \"xna\" | \"xna-test\" | \"evr\" | \"evr-test\";\n\nfunction getNetwork(name: Network) {\n const c = name.toLowerCase(); //Just to be sure\n const map = {\n xna: chains.xna.mainnet.versions,\n \"xna-test\": chains.xna.testnet?.versions,\n evr: chains.evr.mainnet.versions,\n \"evr-test\": chains.evr.testnet?.versions,\n };\n\n const network = map[c];\n if (!network) {\n throw new Error(\"network must be of value \" + Object.keys(map).toString());\n }\n return network;\n}\n/**\n *\n * @param network\n * @returns the coin type for the network (blockchain), for example Neurai has coin type 175\n */\nexport function getCoinType(network: Network) {\n const chain = getNetwork(network);\n return chain.bip44;\n}\n/**\n * @param network - should have value \"xna\", \"xna-test\", \"evr\" or \"evr-test\"\n * @param mnemonic - your mnemonic\n * @param account - accounts in BIP44 starts from 0, 0 is the default account\n * @param position - starts from 0\n */\nexport function getAddressPair(\n network: Network,\n mnemonic: string,\n account: number,\n position: number\n) {\n const hdKey = getHDKey(network, mnemonic);\n const coin_type = getCoinType(network);\n\n //https://github.com/satoshilabs/slips/blob/master/slip-0044.md\n\n //Syntax of BIP44\n //m / purpose' / coin_type' / account' / change / address_index\n const externalPath = `m/44'/${coin_type}'/${account}'/0/${position}`;\n const externalAddress = getAddressByPath(network, hdKey, externalPath);\n\n //change address\n const internalPath = `m/44'/${coin_type}'/${account}'/1/${position}`;\n const internalAddress = getAddressByPath(network, hdKey, internalPath);\n return {\n internal: internalAddress,\n external: externalAddress,\n position,\n };\n}\n\nexport function getHDKey(network: Network, mnemonic: string): any {\n const chain = getNetwork(network);\n const seed = bip39.mnemonicToSeedSync(mnemonic).toString(\"hex\");\n //From the seed, get a hdKey, can we use CoinKey instead?\n const hdKey = HDKey.fromMasterSeed(Buffer.from(seed, \"hex\"), chain.bip32);\n return hdKey;\n}\n\nexport function getAddressByPath(\n network: Network,\n hdKey: any,\n path: string\n): IAddressObject {\n const chain = getNetwork(network);\n const derived = hdKey.derive(path);\n var ck2 = new CoinKey(derived.privateKey, chain);\n\n return {\n address: ck2.publicAddress,\n path: path,\n privateKey: ck2.privateKey.toString(\"hex\"),\n WIF: ck2.privateWif,\n };\n}\n\nexport function generateMnemonic() {\n return bip39.generateMnemonic();\n}\n\nexport function isMnemonicValid(mnemonic: string) {\n //Check all languages\n const wordlists = Object.values(bip39.wordlists);\n\n //If mnemonic is valid in any language, return true, otherwise false\n for (const wordlist of wordlists) {\n const v = bip39.validateMnemonic(mnemonic, wordlist);\n if (v === true) {\n return true;\n }\n }\n return false;\n}\n/**\n *\n * @param privateKeyWIF\n * @param network should be \"xna\" or \"xna-test\"\n * @returns object {address, privateKey (hex), WIF}\n */\n\nexport function getAddressByWIF(network: Network, privateKeyWIF: string) {\n const coinKey = CoinKey.fromWif(privateKeyWIF);\n coinKey.versions = getNetwork(network);\n\n return {\n address: coinKey.publicAddress,\n privateKey: coinKey.privateKey.toString(\"hex\"),\n WIF: coinKey.privateWif,\n };\n}\n\nexport const entropyToMnemonic = bip39.entropyToMnemonic;\n\nexport function generateAddressObject(\n network: Network = \"xna\"\n): IAddressObject {\n const mnemonic = generateMnemonic();\n const account = 0;\n const position = 0;\n const addressPair = getAddressPair(network, mnemonic, account, position);\n const addressObject = addressPair.external;\n\n const result = {\n ...addressObject,\n mnemonic,\n network,\n };\n return result;\n}\n\n/**\n * Generates a random Address Object\n *\n * @deprecated use generateAddressObject\n * @param network\n * @returns\n */\nexport function generateAddress(network: Network = \"xna\") {\n return generateAddressObject(network);\n}\nexport default {\n entropyToMnemonic,\n generateAddress,\n generateMnemonic,\n getAddressByPath,\n getAddressByWIF,\n getAddressPair,\n getCoinType,\n getHDKey,\n isMnemonicValid,\n};\n"],"names":[],"version":3,"file":"module.js.map"}
1
+ {"mappings":";;;;;;;AAAA,uCAAuC;;;;;;;AAiBvC,SAAS,iCAAW,IAAa;IAC/B,MAAM,IAAI,KAAK,WAAW,IAAe,iBAAiB;IAC1D,MAAM,YAAY,CAAA,GAAA,aAAK;IACvB,MAAM,MAA4B;QAChC,KAAK,UAAU,GAAG,CAAC,OAAO,CAAC,QAAQ;QACnC,YAAY,UAAU,GAAG,CAAC,OAAO,EAAE;IACrC;IAEA,MAAM,UAAU,GAAG,CAAC,EAAE;IACtB,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,8BAA8B,OAAO,IAAI,CAAC,KAAK,QAAQ;IAEzE,OAAO;AACT;AAMO,SAAS,0CAAY,OAAgB;IAC1C,MAAM,QAAQ,iCAAW;IACzB,OAAO,MAAM,KAAK;AACpB;AAQO,SAAS,0CACd,OAAgB,EAChB,QAAgB,EAChB,OAAe,EACf,QAAgB,EAChB,aAAqB,EAAE;IAEvB,MAAM,QAAQ,0CAAS,SAAS,UAAU;IAC1C,MAAM,YAAY,0CAAY;IAE9B,+DAA+D;IAE/D,iBAAiB;IACjB,+DAA+D;IAC/D,MAAM,eAAe,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE,QAAQ,IAAI,EAAE,UAAU;IACpE,MAAM,kBAAkB,0CA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IACzB;AACF;AAEO,MAAM,4CAAoB;AAE1B,SAAS,0CACd,UAAmB,KAAK,EACxB,aAAqB,EAAE;IAEvB,MAAM,WAAW;IACjB,MAAM,UAAU;IAChB,MAAM,WAAW;IACjB,MAAM,cAAc,0CAAe,SAAS,UAAU,SAAS,UAAU;IACzE,MAAM,gBAAgB,YAAY,QAAQ;IAE1C,MAAM,SAAS;QACb,GAAG,aAAa;kBAChB;iBACA;IACF;IACA,OAAO;AACT;AAEO,SAAS,0CACd,OAAgB,EAChB,SAA0B;IAE1B,MAAM,QAAQ,iCAAW;IACzB,MAAM,YAAY,OAAO,QAAQ,CAAC,aAC9B,YACA,OAAO,IAAI,CAAC,WAAW;IAE3B,IAAI,UAAU,MAAM,KAAK,MAAM,UAAU,MAAM,KAAK,IAClD,MAAM,IAAI,MAAM;IAGlB,MAAM,aAAa,CAAA,GAAA,iBAAS,EAAE,UAAU,MAAM,CAAC,WAAW,MAAM;IAChE,MAAM,gBAAgB,CAAA,GAAA,iBAAS,EAAE,aAAa,MAAM,CAAC,YAAY,MAAM;IACvE,MAAM,UAAU,OAAO,MAAM,CAAC;QAC5B,OAAO,IAAI,CAAC;YAAC,MAAM,MAAM;SAAC;QAC1B;KACD;IAED,OAAO,cAAiB;AAC1B;AASO,SAAS,0CAAgB,UAAmB,KAAK;IACtD,OAAO,0CAAsB;AAC/B;IACA,2CAAe;uBACb;qBACA;sBACA;sBACA;qBACA;oBACA;iBACA;cACA;qBACA;wBACA;AACF","sources":["index.ts"],"sourcesContent":["//Gives us meta data about coins/chains\nimport { chains } from \"@hyperbitjs/chains\";\n\n//bip39 from mnemonic to seed\nimport * as bip39 from \"bip39\";\n\nimport { createHash } from \"crypto\";\nconst CoinKey = require(\"coinkey\");\n\n//From seed to key\nconst HDKey = require(\"hdkey\");\nimport { IAddressObject } from \"./types\";\nconst bs58check = require(\"bs58check\");\n\n//Could not declare Network as enum, something wrong with parcel bundler\nexport type Network = \"xna\" | \"xna-test\";\n\nfunction getNetwork(name: Network) {\n const c = name.toLowerCase() as Network; //Just to be sure\n const chainData = chains as any;\n const map: Record<Network, any> = {\n xna: chainData.xna.mainnet.versions,\n \"xna-test\": chainData.xna.testnet?.versions,\n };\n\n const network = map[c];\n if (!network) {\n throw new Error(\"network must be of value \" + Object.keys(map).toString());\n }\n return network;\n}\n/**\n *\n * @param network\n * @returns the coin type for the network (blockchain), for example Neurai has coin type 175\n */\nexport function getCoinType(network: Network) {\n const chain = getNetwork(network);\n return chain.bip44;\n}\n/**\n * @param network - should have value \"xna\", \"xna-test\", \"evr\" or \"evr-test\"\n * @param mnemonic - your mnemonic\n * @param account - accounts in BIP44 starts from 0, 0 is the default account\n * @param position - starts from 0\n * @param passphrase - optional BIP39 passphrase (25th word) for additional security\n */\nexport function getAddressPair(\n network: Network,\n mnemonic: string,\n account: number,\n position: number,\n passphrase: string = \"\"\n) {\n const hdKey = getHDKey(network, mnemonic, passphrase);\n const coin_type = getCoinType(network);\n\n //https://github.com/satoshilabs/slips/blob/master/slip-0044.md\n\n //Syntax of BIP44\n //m / purpose' / coin_type' / account' / change / address_index\n const externalPath = `m/44'/${coin_type}'/${account}'/0/${position}`;\n const externalAddress = getAddressByPath(network, hdKey, externalPath);\n\n //change address\n const internalPath = `m/44'/${coin_type}'/${account}'/1/${position}`;\n const internalAddress = getAddressByPath(network, hdKey, internalPath);\n return {\n internal: internalAddress,\n external: externalAddress,\n position,\n };\n}\n\nexport function getHDKey(network: Network, mnemonic: string, passphrase: string = \"\"): any {\n const chain = getNetwork(network);\n const seed = bip39.mnemonicToSeedSync(mnemonic, passphrase).toString(\"hex\");\n //From the seed, get a hdKey, can we use CoinKey instead?\n const hdKey = HDKey.fromMasterSeed(Buffer.from(seed, \"hex\"), chain.bip32);\n return hdKey;\n}\n\nexport function getAddressByPath(\n network: Network,\n hdKey: any,\n path: string\n): IAddressObject {\n const chain = getNetwork(network);\n const derived = hdKey.derive(path);\n var ck2 = new CoinKey(derived.privateKey, chain);\n\n return {\n address: ck2.publicAddress,\n path: path,\n publicKey: ck2.publicKey.toString(\"hex\"),\n privateKey: ck2.privateKey.toString(\"hex\"),\n WIF: ck2.privateWif,\n };\n}\n\nexport function generateMnemonic() {\n return bip39.generateMnemonic();\n}\n\nexport function isMnemonicValid(mnemonic: string) {\n //Check all languages\n const wordlists = Object.values(bip39.wordlists);\n\n //If mnemonic is valid in any language, return true, otherwise false\n for (const wordlist of wordlists) {\n const v = bip39.validateMnemonic(mnemonic, wordlist);\n if (v === true) {\n return true;\n }\n }\n return false;\n}\n/**\n *\n * @param privateKeyWIF\n * @param network should be \"xna\" or \"xna-test\"\n * @returns object {address, privateKey (hex), WIF}\n */\n\nexport function getAddressByWIF(network: Network, privateKeyWIF: string) {\n const coinKey = CoinKey.fromWif(privateKeyWIF);\n coinKey.versions = getNetwork(network);\n\n return {\n address: coinKey.publicAddress,\n privateKey: coinKey.privateKey.toString(\"hex\"),\n WIF: coinKey.privateWif,\n };\n}\n\nexport const entropyToMnemonic = bip39.entropyToMnemonic;\n\nexport function generateAddressObject(\n network: Network = \"xna\",\n passphrase: string = \"\"\n): IAddressObject {\n const mnemonic = generateMnemonic();\n const account = 0;\n const position = 0;\n const addressPair = getAddressPair(network, mnemonic, account, position, passphrase);\n const addressObject = addressPair.external;\n\n const result = {\n ...addressObject,\n mnemonic,\n network,\n };\n return result;\n}\n\nexport function publicKeyToAddress(\n network: Network,\n publicKey: Buffer | string\n): string {\n const chain = getNetwork(network);\n const keyBuffer = Buffer.isBuffer(publicKey)\n ? publicKey\n : Buffer.from(publicKey, \"hex\");\n\n if (keyBuffer.length !== 33 && keyBuffer.length !== 65) {\n throw new Error(\"Public key must be 33 or 65 bytes\");\n }\n\n const sha256Hash = createHash(\"sha256\").update(keyBuffer).digest();\n const ripemd160Hash = createHash(\"ripemd160\").update(sha256Hash).digest();\n const payload = Buffer.concat([\n Buffer.from([chain.public]),\n ripemd160Hash,\n ]);\n\n return bs58check.encode(payload);\n}\n\n/**\n * Generates a random Address Object\n *\n * @deprecated use generateAddressObject\n * @param network\n * @returns\n */\nexport function generateAddress(network: Network = \"xna\") {\n return generateAddressObject(network);\n}\nexport default {\n entropyToMnemonic,\n generateAddress,\n generateMnemonic,\n getAddressByPath,\n getAddressByWIF,\n getAddressPair,\n getCoinType,\n getHDKey,\n isMnemonicValid,\n publicKeyToAddress,\n};\n"],"names":[],"version":3,"file":"module.js.map"}
package/dist/types.d.ts CHANGED
@@ -3,10 +3,11 @@ interface IAddressObject {
3
3
  address: string;
4
4
  mnemonic?: string;
5
5
  path: string;
6
+ publicKey: string;
6
7
  privateKey: string;
7
8
  WIF: string;
8
9
  }
9
- export type Network = "xna" | "xna-test" | "evr" | "evr-test";
10
+ export type Network = "xna" | "xna-test";
10
11
  /**
11
12
  *
12
13
  * @param network
@@ -18,13 +19,14 @@ export function getCoinType(network: Network): any;
18
19
  * @param mnemonic - your mnemonic
19
20
  * @param account - accounts in BIP44 starts from 0, 0 is the default account
20
21
  * @param position - starts from 0
22
+ * @param passphrase - optional BIP39 passphrase (25th word) for additional security
21
23
  */
22
- export function getAddressPair(network: Network, mnemonic: string, account: number, position: number): {
24
+ export function getAddressPair(network: Network, mnemonic: string, account: number, position: number, passphrase?: string): {
23
25
  internal: IAddressObject;
24
26
  external: IAddressObject;
25
27
  position: number;
26
28
  };
27
- export function getHDKey(network: Network, mnemonic: string): any;
29
+ export function getHDKey(network: Network, mnemonic: string, passphrase?: string): any;
28
30
  export function getAddressByPath(network: Network, hdKey: any, path: string): IAddressObject;
29
31
  export function generateMnemonic(): string;
30
32
  export function isMnemonicValid(mnemonic: string): boolean;
@@ -40,7 +42,8 @@ export function getAddressByWIF(network: Network, privateKeyWIF: string): {
40
42
  WIF: any;
41
43
  };
42
44
  export const entropyToMnemonic: typeof bip39.entropyToMnemonic;
43
- export function generateAddressObject(network?: Network): IAddressObject;
45
+ export function generateAddressObject(network?: Network, passphrase?: string): IAddressObject;
46
+ export function publicKeyToAddress(network: Network, publicKey: Buffer | string): string;
44
47
  /**
45
48
  * Generates a random Address Object
46
49
  *
@@ -59,6 +62,7 @@ declare const _default: {
59
62
  getCoinType: typeof getCoinType;
60
63
  getHDKey: typeof getHDKey;
61
64
  isMnemonicValid: typeof isMnemonicValid;
65
+ publicKeyToAddress: typeof publicKeyToAddress;
62
66
  };
63
67
  export default _default;
64
68
 
@@ -1 +1 @@
1
- {"mappings":";AAAA;IACE,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,GAAG,EAAE,MAAM,CAAC;CACb;ACQD,sBAAsB,KAAK,GAAG,UAAU,GAAG,KAAK,GAAG,UAAU,CAAC;AAiB9D;;;;GAIG;AACH,4BAA4B,OAAO,EAAE,OAAO,OAG3C;AACD;;;;;GAKG;AACH,+BACE,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM;;;;EAoBjB;AAED,yBAAyB,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,GAAG,GAAG,CAMhE;AAED,iCACE,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,GAAG,EACV,IAAI,EAAE,MAAM,GACX,cAAc,CAWhB;AAED,2CAEC;AAED,gCAAgC,QAAQ,EAAE,MAAM,WAY/C;AACD;;;;;GAKG;AAEH,gCAAgC,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM;;;;EAStE;AAED,OAAO,MAAM,iDAA2C,CAAC;AAEzD,sCACE,OAAO,GAAE,OAAe,GACvB,cAAc,CAahB;AAED;;;;;;GAMG;AACH,gCAAgC,OAAO,GAAE,OAAe,kBAEvD;;;;;;;;;;;;AACD,wBAUE","sources":["types.ts","index.ts"],"sourcesContent":["export interface IAddressObject {\n address: string;\n mnemonic?: string;\n path: string;\n privateKey: string;\n WIF: string;\n}\n","//Gives us meta data about coins/chains\nimport { chains } from \"@hyperbitjs/chains\";\n\n//bip39 from mnemonic to seed\nimport * as bip39 from \"bip39\";\n\nconst CoinKey = require(\"coinkey\");\n\n//From seed to key\n//const HDKey = require(\"hdkey\");\nimport HDKey from \"hdkey\";\nimport { IAddressObject } from \"./types\";\n\n//Could not declare Network as enum, something wrong with parcel bundler\nexport type Network = \"xna\" | \"xna-test\" | \"evr\" | \"evr-test\";\n\nfunction getNetwork(name: Network) {\n const c = name.toLowerCase(); //Just to be sure\n const map = {\n xna: chains.xna.mainnet.versions,\n \"xna-test\": chains.xna.testnet?.versions,\n evr: chains.evr.mainnet.versions,\n \"evr-test\": chains.evr.testnet?.versions,\n };\n\n const network = map[c];\n if (!network) {\n throw new Error(\"network must be of value \" + Object.keys(map).toString());\n }\n return network;\n}\n/**\n *\n * @param network\n * @returns the coin type for the network (blockchain), for example Neurai has coin type 175\n */\nexport function getCoinType(network: Network) {\n const chain = getNetwork(network);\n return chain.bip44;\n}\n/**\n * @param network - should have value \"xna\", \"xna-test\", \"evr\" or \"evr-test\"\n * @param mnemonic - your mnemonic\n * @param account - accounts in BIP44 starts from 0, 0 is the default account\n * @param position - starts from 0\n */\nexport function getAddressPair(\n network: Network,\n mnemonic: string,\n account: number,\n position: number\n) {\n const hdKey = getHDKey(network, mnemonic);\n const coin_type = getCoinType(network);\n\n //https://github.com/satoshilabs/slips/blob/master/slip-0044.md\n\n //Syntax of BIP44\n //m / purpose' / coin_type' / account' / change / address_index\n const externalPath = `m/44'/${coin_type}'/${account}'/0/${position}`;\n const externalAddress = getAddressByPath(network, hdKey, externalPath);\n\n //change address\n const internalPath = `m/44'/${coin_type}'/${account}'/1/${position}`;\n const internalAddress = getAddressByPath(network, hdKey, internalPath);\n return {\n internal: internalAddress,\n external: externalAddress,\n position,\n };\n}\n\nexport function getHDKey(network: Network, mnemonic: string): any {\n const chain = getNetwork(network);\n const seed = bip39.mnemonicToSeedSync(mnemonic).toString(\"hex\");\n //From the seed, get a hdKey, can we use CoinKey instead?\n const hdKey = HDKey.fromMasterSeed(Buffer.from(seed, \"hex\"), chain.bip32);\n return hdKey;\n}\n\nexport function getAddressByPath(\n network: Network,\n hdKey: any,\n path: string\n): IAddressObject {\n const chain = getNetwork(network);\n const derived = hdKey.derive(path);\n var ck2 = new CoinKey(derived.privateKey, chain);\n\n return {\n address: ck2.publicAddress,\n path: path,\n privateKey: ck2.privateKey.toString(\"hex\"),\n WIF: ck2.privateWif,\n };\n}\n\nexport function generateMnemonic() {\n return bip39.generateMnemonic();\n}\n\nexport function isMnemonicValid(mnemonic: string) {\n //Check all languages\n const wordlists = Object.values(bip39.wordlists);\n\n //If mnemonic is valid in any language, return true, otherwise false\n for (const wordlist of wordlists) {\n const v = bip39.validateMnemonic(mnemonic, wordlist);\n if (v === true) {\n return true;\n }\n }\n return false;\n}\n/**\n *\n * @param privateKeyWIF\n * @param network should be \"xna\" or \"xna-test\"\n * @returns object {address, privateKey (hex), WIF}\n */\n\nexport function getAddressByWIF(network: Network, privateKeyWIF: string) {\n const coinKey = CoinKey.fromWif(privateKeyWIF);\n coinKey.versions = getNetwork(network);\n\n return {\n address: coinKey.publicAddress,\n privateKey: coinKey.privateKey.toString(\"hex\"),\n WIF: coinKey.privateWif,\n };\n}\n\nexport const entropyToMnemonic = bip39.entropyToMnemonic;\n\nexport function generateAddressObject(\n network: Network = \"xna\"\n): IAddressObject {\n const mnemonic = generateMnemonic();\n const account = 0;\n const position = 0;\n const addressPair = getAddressPair(network, mnemonic, account, position);\n const addressObject = addressPair.external;\n\n const result = {\n ...addressObject,\n mnemonic,\n network,\n };\n return result;\n}\n\n/**\n * Generates a random Address Object\n *\n * @deprecated use generateAddressObject\n * @param network\n * @returns\n */\nexport function generateAddress(network: Network = \"xna\") {\n return generateAddressObject(network);\n}\nexport default {\n entropyToMnemonic,\n generateAddress,\n generateMnemonic,\n getAddressByPath,\n getAddressByWIF,\n getAddressPair,\n getCoinType,\n getHDKey,\n isMnemonicValid,\n};\n"],"names":[],"version":3,"file":"types.d.ts.map"}
1
+ {"mappings":";AAAA;IACE,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,GAAG,EAAE,MAAM,CAAC;CACb;ACQD,sBAAsB,KAAK,GAAG,UAAU,CAAC;AAgBzC;;;;GAIG;AACH,4BAA4B,OAAO,EAAE,OAAO,OAG3C;AACD;;;;;;GAMG;AACH,+BACE,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,UAAU,GAAE,MAAW;;;;EAoBxB;AAED,yBAAyB,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,GAAE,MAAW,GAAG,GAAG,CAMzF;AAED,iCACE,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,GAAG,EACV,IAAI,EAAE,MAAM,GACX,cAAc,CAYhB;AAED,2CAEC;AAED,gCAAgC,QAAQ,EAAE,MAAM,WAY/C;AACD;;;;;GAKG;AAEH,gCAAgC,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM;;;;EAStE;AAED,OAAO,MAAM,iDAA2C,CAAC;AAEzD,sCACE,OAAO,GAAE,OAAe,EACxB,UAAU,GAAE,MAAW,GACtB,cAAc,CAahB;AAED,mCACE,OAAO,EAAE,OAAO,EAChB,SAAS,EAAE,MAAM,GAAG,MAAM,GACzB,MAAM,CAkBR;AAED;;;;;;GAMG;AACH,gCAAgC,OAAO,GAAE,OAAe,kBAEvD;;;;;;;;;;;;;AACD,wBAWE","sources":["types.ts","index.ts"],"sourcesContent":["export interface IAddressObject {\n address: string;\n mnemonic?: string;\n path: string;\n publicKey: string;\n privateKey: string;\n WIF: string;\n}\n","//Gives us meta data about coins/chains\nimport { chains } from \"@hyperbitjs/chains\";\n\n//bip39 from mnemonic to seed\nimport * as bip39 from \"bip39\";\n\nimport { createHash } from \"crypto\";\nconst CoinKey = require(\"coinkey\");\n\n//From seed to key\nconst HDKey = require(\"hdkey\");\nimport { IAddressObject } from \"./types\";\nconst bs58check = require(\"bs58check\");\n\n//Could not declare Network as enum, something wrong with parcel bundler\nexport type Network = \"xna\" | \"xna-test\";\n\nfunction getNetwork(name: Network) {\n const c = name.toLowerCase() as Network; //Just to be sure\n const chainData = chains as any;\n const map: Record<Network, any> = {\n xna: chainData.xna.mainnet.versions,\n \"xna-test\": chainData.xna.testnet?.versions,\n };\n\n const network = map[c];\n if (!network) {\n throw new Error(\"network must be of value \" + Object.keys(map).toString());\n }\n return network;\n}\n/**\n *\n * @param network\n * @returns the coin type for the network (blockchain), for example Neurai has coin type 175\n */\nexport function getCoinType(network: Network) {\n const chain = getNetwork(network);\n return chain.bip44;\n}\n/**\n * @param network - should have value \"xna\", \"xna-test\", \"evr\" or \"evr-test\"\n * @param mnemonic - your mnemonic\n * @param account - accounts in BIP44 starts from 0, 0 is the default account\n * @param position - starts from 0\n * @param passphrase - optional BIP39 passphrase (25th word) for additional security\n */\nexport function getAddressPair(\n network: Network,\n mnemonic: string,\n account: number,\n position: number,\n passphrase: string = \"\"\n) {\n const hdKey = getHDKey(network, mnemonic, passphrase);\n const coin_type = getCoinType(network);\n\n //https://github.com/satoshilabs/slips/blob/master/slip-0044.md\n\n //Syntax of BIP44\n //m / purpose' / coin_type' / account' / change / address_index\n const externalPath = `m/44'/${coin_type}'/${account}'/0/${position}`;\n const externalAddress = getAddressByPath(network, hdKey, externalPath);\n\n //change address\n const internalPath = `m/44'/${coin_type}'/${account}'/1/${position}`;\n const internalAddress = getAddressByPath(network, hdKey, internalPath);\n return {\n internal: internalAddress,\n external: externalAddress,\n position,\n };\n}\n\nexport function getHDKey(network: Network, mnemonic: string, passphrase: string = \"\"): any {\n const chain = getNetwork(network);\n const seed = bip39.mnemonicToSeedSync(mnemonic, passphrase).toString(\"hex\");\n //From the seed, get a hdKey, can we use CoinKey instead?\n const hdKey = HDKey.fromMasterSeed(Buffer.from(seed, \"hex\"), chain.bip32);\n return hdKey;\n}\n\nexport function getAddressByPath(\n network: Network,\n hdKey: any,\n path: string\n): IAddressObject {\n const chain = getNetwork(network);\n const derived = hdKey.derive(path);\n var ck2 = new CoinKey(derived.privateKey, chain);\n\n return {\n address: ck2.publicAddress,\n path: path,\n publicKey: ck2.publicKey.toString(\"hex\"),\n privateKey: ck2.privateKey.toString(\"hex\"),\n WIF: ck2.privateWif,\n };\n}\n\nexport function generateMnemonic() {\n return bip39.generateMnemonic();\n}\n\nexport function isMnemonicValid(mnemonic: string) {\n //Check all languages\n const wordlists = Object.values(bip39.wordlists);\n\n //If mnemonic is valid in any language, return true, otherwise false\n for (const wordlist of wordlists) {\n const v = bip39.validateMnemonic(mnemonic, wordlist);\n if (v === true) {\n return true;\n }\n }\n return false;\n}\n/**\n *\n * @param privateKeyWIF\n * @param network should be \"xna\" or \"xna-test\"\n * @returns object {address, privateKey (hex), WIF}\n */\n\nexport function getAddressByWIF(network: Network, privateKeyWIF: string) {\n const coinKey = CoinKey.fromWif(privateKeyWIF);\n coinKey.versions = getNetwork(network);\n\n return {\n address: coinKey.publicAddress,\n privateKey: coinKey.privateKey.toString(\"hex\"),\n WIF: coinKey.privateWif,\n };\n}\n\nexport const entropyToMnemonic = bip39.entropyToMnemonic;\n\nexport function generateAddressObject(\n network: Network = \"xna\",\n passphrase: string = \"\"\n): IAddressObject {\n const mnemonic = generateMnemonic();\n const account = 0;\n const position = 0;\n const addressPair = getAddressPair(network, mnemonic, account, position, passphrase);\n const addressObject = addressPair.external;\n\n const result = {\n ...addressObject,\n mnemonic,\n network,\n };\n return result;\n}\n\nexport function publicKeyToAddress(\n network: Network,\n publicKey: Buffer | string\n): string {\n const chain = getNetwork(network);\n const keyBuffer = Buffer.isBuffer(publicKey)\n ? publicKey\n : Buffer.from(publicKey, \"hex\");\n\n if (keyBuffer.length !== 33 && keyBuffer.length !== 65) {\n throw new Error(\"Public key must be 33 or 65 bytes\");\n }\n\n const sha256Hash = createHash(\"sha256\").update(keyBuffer).digest();\n const ripemd160Hash = createHash(\"ripemd160\").update(sha256Hash).digest();\n const payload = Buffer.concat([\n Buffer.from([chain.public]),\n ripemd160Hash,\n ]);\n\n return bs58check.encode(payload);\n}\n\n/**\n * Generates a random Address Object\n *\n * @deprecated use generateAddressObject\n * @param network\n * @returns\n */\nexport function generateAddress(network: Network = \"xna\") {\n return generateAddressObject(network);\n}\nexport default {\n entropyToMnemonic,\n generateAddress,\n generateMnemonic,\n getAddressByPath,\n getAddressByWIF,\n getAddressPair,\n getCoinType,\n getHDKey,\n isMnemonicValid,\n publicKeyToAddress,\n};\n"],"names":[],"version":3,"file":"types.d.ts.map"}
@@ -0,0 +1,34 @@
1
+ const NeuraiKey = require("./dist/main");
2
+
3
+ console.log("=== Neurai Key - Passphrase Example ===\n");
4
+
5
+ const mnemonic = "result pact model attract result puzzle final boss private educate luggage era";
6
+ const network = "xna";
7
+ const account = 0;
8
+ const position = 0;
9
+
10
+ console.log("Mnemonic:", mnemonic);
11
+ console.log("\n--- WITHOUT Passphrase ---");
12
+ const addressNoPass = NeuraiKey.getAddressPair(network, mnemonic, account, position);
13
+ console.log("External Address:", addressNoPass.external.address);
14
+ console.log("Internal Address:", addressNoPass.internal.address);
15
+ console.log("WIF:", addressNoPass.external.WIF);
16
+
17
+ console.log("\n--- WITH Passphrase: 'my secret passphrase' ---");
18
+ const passphrase1 = "my secret passphrase";
19
+ const addressWithPass1 = NeuraiKey.getAddressPair(network, mnemonic, account, position, passphrase1);
20
+ console.log("External Address:", addressWithPass1.external.address);
21
+ console.log("Internal Address:", addressWithPass1.internal.address);
22
+ console.log("WIF:", addressWithPass1.external.WIF);
23
+
24
+ console.log("\n--- WITH Different Passphrase: 'another passphrase' ---");
25
+ const passphrase2 = "another passphrase";
26
+ const addressWithPass2 = NeuraiKey.getAddressPair(network, mnemonic, account, position, passphrase2);
27
+ console.log("External Address:", addressWithPass2.external.address);
28
+ console.log("Internal Address:", addressWithPass2.internal.address);
29
+ console.log("WIF:", addressWithPass2.external.WIF);
30
+
31
+ console.log("\n=== Summary ===");
32
+ console.log("✅ Same mnemonic + different passphrases = DIFFERENT wallets");
33
+ console.log("✅ This provides an extra security layer");
34
+ console.log("⚠️ IMPORTANT: You need BOTH mnemonic AND passphrase to recover the wallet!");
package/index.ts CHANGED
@@ -4,23 +4,23 @@ import { chains } from "@hyperbitjs/chains";
4
4
  //bip39 from mnemonic to seed
5
5
  import * as bip39 from "bip39";
6
6
 
7
+ import { createHash } from "crypto";
7
8
  const CoinKey = require("coinkey");
8
9
 
9
10
  //From seed to key
10
- //const HDKey = require("hdkey");
11
- import HDKey from "hdkey";
11
+ const HDKey = require("hdkey");
12
12
  import { IAddressObject } from "./types";
13
+ const bs58check = require("bs58check");
13
14
 
14
15
  //Could not declare Network as enum, something wrong with parcel bundler
15
- export type Network = "xna" | "xna-test" | "evr" | "evr-test";
16
+ export type Network = "xna" | "xna-test";
16
17
 
17
18
  function getNetwork(name: Network) {
18
- const c = name.toLowerCase(); //Just to be sure
19
- const map = {
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,
19
+ const c = name.toLowerCase() as Network; //Just to be sure
20
+ const chainData = chains as any;
21
+ const map: Record<Network, any> = {
22
+ xna: chainData.xna.mainnet.versions,
23
+ "xna-test": chainData.xna.testnet?.versions,
24
24
  };
25
25
 
26
26
  const network = map[c];
@@ -43,14 +43,16 @@ export function getCoinType(network: Network) {
43
43
  * @param mnemonic - your mnemonic
44
44
  * @param account - accounts in BIP44 starts from 0, 0 is the default account
45
45
  * @param position - starts from 0
46
+ * @param passphrase - optional BIP39 passphrase (25th word) for additional security
46
47
  */
47
48
  export function getAddressPair(
48
49
  network: Network,
49
50
  mnemonic: string,
50
51
  account: number,
51
- position: number
52
+ position: number,
53
+ passphrase: string = ""
52
54
  ) {
53
- const hdKey = getHDKey(network, mnemonic);
55
+ const hdKey = getHDKey(network, mnemonic, passphrase);
54
56
  const coin_type = getCoinType(network);
55
57
 
56
58
  //https://github.com/satoshilabs/slips/blob/master/slip-0044.md
@@ -70,9 +72,9 @@ export function getAddressPair(
70
72
  };
71
73
  }
72
74
 
73
- export function getHDKey(network: Network, mnemonic: string): any {
75
+ export function getHDKey(network: Network, mnemonic: string, passphrase: string = ""): any {
74
76
  const chain = getNetwork(network);
75
- const seed = bip39.mnemonicToSeedSync(mnemonic).toString("hex");
77
+ const seed = bip39.mnemonicToSeedSync(mnemonic, passphrase).toString("hex");
76
78
  //From the seed, get a hdKey, can we use CoinKey instead?
77
79
  const hdKey = HDKey.fromMasterSeed(Buffer.from(seed, "hex"), chain.bip32);
78
80
  return hdKey;
@@ -90,6 +92,7 @@ export function getAddressByPath(
90
92
  return {
91
93
  address: ck2.publicAddress,
92
94
  path: path,
95
+ publicKey: ck2.publicKey.toString("hex"),
93
96
  privateKey: ck2.privateKey.toString("hex"),
94
97
  WIF: ck2.privateWif,
95
98
  };
@@ -133,12 +136,13 @@ export function getAddressByWIF(network: Network, privateKeyWIF: string) {
133
136
  export const entropyToMnemonic = bip39.entropyToMnemonic;
134
137
 
135
138
  export function generateAddressObject(
136
- network: Network = "xna"
139
+ network: Network = "xna",
140
+ passphrase: string = ""
137
141
  ): IAddressObject {
138
142
  const mnemonic = generateMnemonic();
139
143
  const account = 0;
140
144
  const position = 0;
141
- const addressPair = getAddressPair(network, mnemonic, account, position);
145
+ const addressPair = getAddressPair(network, mnemonic, account, position, passphrase);
142
146
  const addressObject = addressPair.external;
143
147
 
144
148
  const result = {
@@ -149,6 +153,29 @@ export function generateAddressObject(
149
153
  return result;
150
154
  }
151
155
 
156
+ export function publicKeyToAddress(
157
+ network: Network,
158
+ publicKey: Buffer | string
159
+ ): string {
160
+ const chain = getNetwork(network);
161
+ const keyBuffer = Buffer.isBuffer(publicKey)
162
+ ? publicKey
163
+ : Buffer.from(publicKey, "hex");
164
+
165
+ if (keyBuffer.length !== 33 && keyBuffer.length !== 65) {
166
+ throw new Error("Public key must be 33 or 65 bytes");
167
+ }
168
+
169
+ const sha256Hash = createHash("sha256").update(keyBuffer).digest();
170
+ const ripemd160Hash = createHash("ripemd160").update(sha256Hash).digest();
171
+ const payload = Buffer.concat([
172
+ Buffer.from([chain.public]),
173
+ ripemd160Hash,
174
+ ]);
175
+
176
+ return bs58check.encode(payload);
177
+ }
178
+
152
179
  /**
153
180
  * Generates a random Address Object
154
181
  *
@@ -169,4 +196,5 @@ export default {
169
196
  getCoinType,
170
197
  getHDKey,
171
198
  isMnemonicValid,
199
+ publicKeyToAddress,
172
200
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neuraiproject/neurai-key",
3
- "version": "2.8.3",
3
+ "version": "2.8.5",
4
4
  "description": "Generate Neurai addresses from mnemonic code. BIP32, BIP39, BIP44",
5
5
  "source": "index.ts",
6
6
  "main": "dist/main.js",
@@ -29,12 +29,13 @@
29
29
  "dependencies": {
30
30
  "@hyperbitjs/chains": "^1.2.0",
31
31
  "bip39": "^3.0.4",
32
+ "bs58check": "^2.1.2",
32
33
  "coinkey": "^3.0.0",
33
34
  "hdkey": "^2.0.1"
34
35
  },
35
36
  "devDependencies": {
36
37
  "@parcel/packager-ts": "^2.10.3",
37
- "@parcel/transformer-typescript-types": "^2.10.3",
38
+ "@parcel/transformer-typescript-types": "^2.16.1",
38
39
  "@types/hdkey": "^2.0.3",
39
40
  "@types/node": "^18.14.0",
40
41
  "browserify": "^17.0.0",