@neuraiproject/neurai-key 2.8.4 → 2.8.6

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
  }
@@ -25,8 +23,10 @@ $parcel$export(module.exports, "getAddressByPath", () => $80bd448eb6ea085b$expor
25
23
  $parcel$export(module.exports, "generateMnemonic", () => $80bd448eb6ea085b$export$9f993213e5806bf0);
26
24
  $parcel$export(module.exports, "isMnemonicValid", () => $80bd448eb6ea085b$export$2b99b9ff149202f3);
27
25
  $parcel$export(module.exports, "getAddressByWIF", () => $80bd448eb6ea085b$export$f43d70cb4ddd5664);
26
+ $parcel$export(module.exports, "getPubkeyByWIF", () => $80bd448eb6ea085b$export$6ad4fb7ac7584525);
28
27
  $parcel$export(module.exports, "entropyToMnemonic", () => $80bd448eb6ea085b$export$4becd65eb23312e6);
29
28
  $parcel$export(module.exports, "generateAddressObject", () => $80bd448eb6ea085b$export$de190b37be25f71b);
29
+ $parcel$export(module.exports, "publicKeyToAddress", () => $80bd448eb6ea085b$export$462669520a9d12d1);
30
30
  $parcel$export(module.exports, "generateAddress", () => $80bd448eb6ea085b$export$e2e336010351d8a8);
31
31
  $parcel$export(module.exports, "default", () => $80bd448eb6ea085b$export$2e2bcd8739ae039);
32
32
  //Gives us meta data about coins/chains
@@ -34,11 +34,14 @@ $parcel$export(module.exports, "default", () => $80bd448eb6ea085b$export$2e2bcd8
34
34
 
35
35
 
36
36
 
37
+
38
+
37
39
  function $80bd448eb6ea085b$var$getNetwork(name) {
38
40
  const c = name.toLowerCase(); //Just to be sure
41
+ const chainData = (0, $g5Y9E$hyperbitjschains.chains);
39
42
  const map = {
40
- xna: (0, $g5Y9E$hyperbitjschains.chains).xna.mainnet.versions,
41
- "xna-test": (0, $g5Y9E$hyperbitjschains.chains).xna.testnet?.versions
43
+ xna: chainData.xna.mainnet.versions,
44
+ "xna-test": chainData.xna.testnet?.versions
42
45
  };
43
46
  const network = map[c];
44
47
  if (!network) throw new Error("network must be of value " + Object.keys(map).toString());
@@ -69,7 +72,7 @@ function $80bd448eb6ea085b$export$6c78ccde21ad48f6(network, mnemonic, passphrase
69
72
  const chain = $80bd448eb6ea085b$var$getNetwork(network);
70
73
  const seed = $g5Y9E$bip39.mnemonicToSeedSync(mnemonic, passphrase).toString("hex");
71
74
  //From the seed, get a hdKey, can we use CoinKey instead?
72
- const hdKey = (0, ($parcel$interopDefault($g5Y9E$hdkey))).fromMasterSeed(Buffer.from(seed, "hex"), chain.bip32);
75
+ const hdKey = $g5Y9E$hdkey.fromMasterSeed(Buffer.from(seed, "hex"), chain.bip32);
73
76
  return hdKey;
74
77
  }
75
78
  function $80bd448eb6ea085b$export$6fc951b76952b95e(network, hdKey, path) {
@@ -79,6 +82,7 @@ function $80bd448eb6ea085b$export$6fc951b76952b95e(network, hdKey, path) {
79
82
  return {
80
83
  address: ck2.publicAddress,
81
84
  path: path,
85
+ publicKey: ck2.publicKey.toString("hex"),
82
86
  privateKey: ck2.privateKey.toString("hex"),
83
87
  WIF: ck2.privateWif
84
88
  };
@@ -105,6 +109,11 @@ function $80bd448eb6ea085b$export$f43d70cb4ddd5664(network, privateKeyWIF) {
105
109
  WIF: coinKey.privateWif
106
110
  };
107
111
  }
112
+ function $80bd448eb6ea085b$export$6ad4fb7ac7584525(network, privateKeyWIF) {
113
+ const coinKey = $g5Y9E$coinkey.fromWif(privateKeyWIF);
114
+ coinKey.versions = $80bd448eb6ea085b$var$getNetwork(network);
115
+ return coinKey.publicKey.toString("hex");
116
+ }
108
117
  const $80bd448eb6ea085b$export$4becd65eb23312e6 = $g5Y9E$bip39.entropyToMnemonic;
109
118
  function $80bd448eb6ea085b$export$de190b37be25f71b(network = "xna", passphrase = "") {
110
119
  const mnemonic = $80bd448eb6ea085b$export$9f993213e5806bf0();
@@ -119,6 +128,20 @@ function $80bd448eb6ea085b$export$de190b37be25f71b(network = "xna", passphrase =
119
128
  };
120
129
  return result;
121
130
  }
131
+ function $80bd448eb6ea085b$export$462669520a9d12d1(network, publicKey) {
132
+ const chain = $80bd448eb6ea085b$var$getNetwork(network);
133
+ const keyBuffer = Buffer.isBuffer(publicKey) ? publicKey : Buffer.from(publicKey, "hex");
134
+ if (keyBuffer.length !== 33 && keyBuffer.length !== 65) throw new Error("Public key must be 33 or 65 bytes");
135
+ const sha256Hash = (0, $g5Y9E$crypto.createHash)("sha256").update(keyBuffer).digest();
136
+ const ripemd160Hash = (0, $g5Y9E$crypto.createHash)("ripemd160").update(sha256Hash).digest();
137
+ const payload = Buffer.concat([
138
+ Buffer.from([
139
+ chain.public
140
+ ]),
141
+ ripemd160Hash
142
+ ]);
143
+ return $g5Y9E$bs58check.encode(payload);
144
+ }
122
145
  function $80bd448eb6ea085b$export$e2e336010351d8a8(network = "xna") {
123
146
  return $80bd448eb6ea085b$export$de190b37be25f71b(network);
124
147
  }
@@ -128,10 +151,12 @@ var $80bd448eb6ea085b$export$2e2bcd8739ae039 = {
128
151
  generateMnemonic: $80bd448eb6ea085b$export$9f993213e5806bf0,
129
152
  getAddressByPath: $80bd448eb6ea085b$export$6fc951b76952b95e,
130
153
  getAddressByWIF: $80bd448eb6ea085b$export$f43d70cb4ddd5664,
154
+ getPubkeyByWIF: $80bd448eb6ea085b$export$6ad4fb7ac7584525,
131
155
  getAddressPair: $80bd448eb6ea085b$export$6e3ac79f8c0a2892,
132
156
  getCoinType: $80bd448eb6ea085b$export$23109f16a8a07245,
133
157
  getHDKey: $80bd448eb6ea085b$export$6c78ccde21ad48f6,
134
- isMnemonicValid: $80bd448eb6ea085b$export$2b99b9ff149202f3
158
+ isMnemonicValid: $80bd448eb6ea085b$export$2b99b9ff149202f3,
159
+ publicKeyToAddress: $80bd448eb6ea085b$export$462669520a9d12d1
135
160
  };
136
161
 
137
162
 
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;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;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,0CAAiB,SAAS,OAAO;IAEzD,gBAAgB;IAChB,MAAM,eAAe,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE,QAAQ,IAAI,EAAE,UAAU;IACpE,MAAM,kBAAkB,0CAAiB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c,0CAAe,SAAS,UAAU,SAAS,UAAU;IACzE,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\";\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 };\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 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\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,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,0CAAiB,SAAS,OAAO;IAEzD,gBAAgB;IAChB,MAAM,eAAe,CAAC,MAAM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,UAAU,eAAQ,OAAO,CAAC;IAChC,QAAQ,QAAQ,GAAG,iCAAW;IAE9B,OAAO,QAAQ,SAAS,CAAC,QAAQ,CAAC;AACpC;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,wBAAS,EAAE,UAAU,MAAM,CAAC,WAAW,MAAM;IAChE,MAAM,gBAAgB,CAAA,GAAA,wBAAS,EAAE,aAAa,MAAM,CAAC,YAAY,MAAM;IACvE,MAAM,UAAU,OAAO,MAAM,CAAC;QAC5B,OAAO,IAAI,CAAC;YAAC,MAAM,MAAM;SAAC;QAC1B;KACD;IAED,OAAO,wBAAiB;AAC1B;AASO,SAAS,0CAAgB,UAAmB,KAAK;IACtD,OAAO,0CAAsB;AAC/B;IACA,2CAAe;uBACb;qBACA;sBACA;sBACA;qBACA;oBACA;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\n/**\n * @param privateKeyWIF\n * @param network should be \"xna\" or \"xna-test\"\n * @returns the compressed public key as a hex string\n */\nexport function getPubkeyByWIF(network: Network, privateKeyWIF: string): string {\n const coinKey = CoinKey.fromWif(privateKeyWIF);\n coinKey.versions = getNetwork(network);\n\n return coinKey.publicKey.toString(\"hex\");\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 getPubkeyByWIF,\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,18 +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
19
+ xna: chainData.xna.mainnet.versions,
20
+ "xna-test": chainData.xna.testnet?.versions
16
21
  };
17
22
  const network = map[c];
18
23
  if (!network) throw new Error("network must be of value " + Object.keys(map).toString());
@@ -43,7 +48,7 @@ function $c3f6c693698dc7cd$export$6c78ccde21ad48f6(network, mnemonic, passphrase
43
48
  const chain = $c3f6c693698dc7cd$var$getNetwork(network);
44
49
  const seed = $hCgyA$mnemonicToSeedSync(mnemonic, passphrase).toString("hex");
45
50
  //From the seed, get a hdKey, can we use CoinKey instead?
46
- const hdKey = (0, $hCgyA$hdkey).fromMasterSeed(Buffer.from(seed, "hex"), chain.bip32);
51
+ const hdKey = $hCgyA$fromMasterSeed(Buffer.from(seed, "hex"), chain.bip32);
47
52
  return hdKey;
48
53
  }
49
54
  function $c3f6c693698dc7cd$export$6fc951b76952b95e(network, hdKey, path) {
@@ -53,6 +58,7 @@ function $c3f6c693698dc7cd$export$6fc951b76952b95e(network, hdKey, path) {
53
58
  return {
54
59
  address: ck2.publicAddress,
55
60
  path: path,
61
+ publicKey: ck2.publicKey.toString("hex"),
56
62
  privateKey: ck2.privateKey.toString("hex"),
57
63
  WIF: ck2.privateWif
58
64
  };
@@ -79,6 +85,11 @@ function $c3f6c693698dc7cd$export$f43d70cb4ddd5664(network, privateKeyWIF) {
79
85
  WIF: coinKey.privateWif
80
86
  };
81
87
  }
88
+ function $c3f6c693698dc7cd$export$6ad4fb7ac7584525(network, privateKeyWIF) {
89
+ const coinKey = $hCgyA$coinkey.fromWif(privateKeyWIF);
90
+ coinKey.versions = $c3f6c693698dc7cd$var$getNetwork(network);
91
+ return coinKey.publicKey.toString("hex");
92
+ }
82
93
  const $c3f6c693698dc7cd$export$4becd65eb23312e6 = $hCgyA$entropyToMnemonic;
83
94
  function $c3f6c693698dc7cd$export$de190b37be25f71b(network = "xna", passphrase = "") {
84
95
  const mnemonic = $c3f6c693698dc7cd$export$9f993213e5806bf0();
@@ -93,6 +104,20 @@ function $c3f6c693698dc7cd$export$de190b37be25f71b(network = "xna", passphrase =
93
104
  };
94
105
  return result;
95
106
  }
107
+ function $c3f6c693698dc7cd$export$462669520a9d12d1(network, publicKey) {
108
+ const chain = $c3f6c693698dc7cd$var$getNetwork(network);
109
+ const keyBuffer = Buffer.isBuffer(publicKey) ? publicKey : Buffer.from(publicKey, "hex");
110
+ if (keyBuffer.length !== 33 && keyBuffer.length !== 65) throw new Error("Public key must be 33 or 65 bytes");
111
+ const sha256Hash = (0, $hCgyA$createHash)("sha256").update(keyBuffer).digest();
112
+ const ripemd160Hash = (0, $hCgyA$createHash)("ripemd160").update(sha256Hash).digest();
113
+ const payload = Buffer.concat([
114
+ Buffer.from([
115
+ chain.public
116
+ ]),
117
+ ripemd160Hash
118
+ ]);
119
+ return $hCgyA$encode(payload);
120
+ }
96
121
  function $c3f6c693698dc7cd$export$e2e336010351d8a8(network = "xna") {
97
122
  return $c3f6c693698dc7cd$export$de190b37be25f71b(network);
98
123
  }
@@ -102,12 +127,14 @@ var $c3f6c693698dc7cd$export$2e2bcd8739ae039 = {
102
127
  generateMnemonic: $c3f6c693698dc7cd$export$9f993213e5806bf0,
103
128
  getAddressByPath: $c3f6c693698dc7cd$export$6fc951b76952b95e,
104
129
  getAddressByWIF: $c3f6c693698dc7cd$export$f43d70cb4ddd5664,
130
+ getPubkeyByWIF: $c3f6c693698dc7cd$export$6ad4fb7ac7584525,
105
131
  getAddressPair: $c3f6c693698dc7cd$export$6e3ac79f8c0a2892,
106
132
  getCoinType: $c3f6c693698dc7cd$export$23109f16a8a07245,
107
133
  getHDKey: $c3f6c693698dc7cd$export$6c78ccde21ad48f6,
108
- isMnemonicValid: $c3f6c693698dc7cd$export$2b99b9ff149202f3
134
+ isMnemonicValid: $c3f6c693698dc7cd$export$2b99b9ff149202f3,
135
+ publicKeyToAddress: $c3f6c693698dc7cd$export$462669520a9d12d1
109
136
  };
110
137
 
111
138
 
112
- 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};
139
+ 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$6ad4fb7ac7584525 as getPubkeyByWIF, $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};
113
140
  //# 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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OAAO;QACL,SAAS,IAAI,aAAa;QAC1B,MAAM;QACN,YAAY,IAAI,UAAU,CAAC,QAAQ,CAAC;QACpC,KAAK,IAAI,UAAU;IACrB;AACF;AAEO,SAAS;IACd,OAAO;AACT;AAEO,SAAS,0CAAgB,QAAgB;IAC9C,qBAAqB;IACrB,MAAM,YAAY,OAAO,MAAM,CAAC;IAEhC,oEAAoE;IACpE,KAAK,MAAM,YAAY,UAAW;QAChC,MAAM,IAAI,wBAAuB,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,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;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\";\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 };\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 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\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,a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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;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\n/**\n * @param privateKeyWIF\n * @param network should be \"xna\" or \"xna-test\"\n * @returns the compressed public key as a hex string\n */\nexport function getPubkeyByWIF(network: Network, privateKeyWIF: string): string {\n const coinKey = CoinKey.fromWif(privateKeyWIF);\n coinKey.versions = getNetwork(network);\n\n return coinKey.publicKey.toString(\"hex\");\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 getPubkeyByWIF,\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,6 +3,7 @@ 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
  }
@@ -40,8 +41,15 @@ export function getAddressByWIF(network: Network, privateKeyWIF: string): {
40
41
  privateKey: any;
41
42
  WIF: any;
42
43
  };
44
+ /**
45
+ * @param privateKeyWIF
46
+ * @param network should be "xna" or "xna-test"
47
+ * @returns the compressed public key as a hex string
48
+ */
49
+ export function getPubkeyByWIF(network: Network, privateKeyWIF: string): string;
43
50
  export const entropyToMnemonic: typeof bip39.entropyToMnemonic;
44
51
  export function generateAddressObject(network?: Network, passphrase?: string): IAddressObject;
52
+ export function publicKeyToAddress(network: Network, publicKey: Buffer | string): string;
45
53
  /**
46
54
  * Generates a random Address Object
47
55
  *
@@ -56,10 +64,12 @@ declare const _default: {
56
64
  generateMnemonic: typeof generateMnemonic;
57
65
  getAddressByPath: typeof getAddressByPath;
58
66
  getAddressByWIF: typeof getAddressByWIF;
67
+ getPubkeyByWIF: typeof getPubkeyByWIF;
59
68
  getAddressPair: typeof getAddressPair;
60
69
  getCoinType: typeof getCoinType;
61
70
  getHDKey: typeof getHDKey;
62
71
  isMnemonicValid: typeof isMnemonicValid;
72
+ publicKeyToAddress: typeof publicKeyToAddress;
63
73
  };
64
74
  export default _default;
65
75
 
@@ -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,CAAC;AAezC;;;;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,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,EACxB,UAAU,GAAE,MAAW,GACtB,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\";\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 };\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 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\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;;;;GAIG;AACH,+BAA+B,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,GAAG,MAAM,CAK9E;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,wBAYE","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\n/**\n * @param privateKeyWIF\n * @param network should be \"xna\" or \"xna-test\"\n * @returns the compressed public key as a hex string\n */\nexport function getPubkeyByWIF(network: Network, privateKeyWIF: string): string {\n const coinKey = CoinKey.fromWif(privateKeyWIF);\n coinKey.versions = getNetwork(network);\n\n return coinKey.publicKey.toString(\"hex\");\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 getPubkeyByWIF,\n getAddressPair,\n getCoinType,\n getHDKey,\n isMnemonicValid,\n publicKeyToAddress,\n};\n"],"names":[],"version":3,"file":"types.d.ts.map"}
package/index.ts CHANGED
@@ -4,21 +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
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,
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,
22
24
  };
23
25
 
24
26
  const network = map[c];
@@ -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
  };
@@ -130,6 +133,18 @@ export function getAddressByWIF(network: Network, privateKeyWIF: string) {
130
133
  };
131
134
  }
132
135
 
136
+ /**
137
+ * @param privateKeyWIF
138
+ * @param network should be "xna" or "xna-test"
139
+ * @returns the compressed public key as a hex string
140
+ */
141
+ export function getPubkeyByWIF(network: Network, privateKeyWIF: string): string {
142
+ const coinKey = CoinKey.fromWif(privateKeyWIF);
143
+ coinKey.versions = getNetwork(network);
144
+
145
+ return coinKey.publicKey.toString("hex");
146
+ }
147
+
133
148
  export const entropyToMnemonic = bip39.entropyToMnemonic;
134
149
 
135
150
  export function generateAddressObject(
@@ -150,6 +165,29 @@ export function generateAddressObject(
150
165
  return result;
151
166
  }
152
167
 
168
+ export function publicKeyToAddress(
169
+ network: Network,
170
+ publicKey: Buffer | string
171
+ ): string {
172
+ const chain = getNetwork(network);
173
+ const keyBuffer = Buffer.isBuffer(publicKey)
174
+ ? publicKey
175
+ : Buffer.from(publicKey, "hex");
176
+
177
+ if (keyBuffer.length !== 33 && keyBuffer.length !== 65) {
178
+ throw new Error("Public key must be 33 or 65 bytes");
179
+ }
180
+
181
+ const sha256Hash = createHash("sha256").update(keyBuffer).digest();
182
+ const ripemd160Hash = createHash("ripemd160").update(sha256Hash).digest();
183
+ const payload = Buffer.concat([
184
+ Buffer.from([chain.public]),
185
+ ripemd160Hash,
186
+ ]);
187
+
188
+ return bs58check.encode(payload);
189
+ }
190
+
153
191
  /**
154
192
  * Generates a random Address Object
155
193
  *
@@ -166,8 +204,10 @@ export default {
166
204
  generateMnemonic,
167
205
  getAddressByPath,
168
206
  getAddressByWIF,
207
+ getPubkeyByWIF,
169
208
  getAddressPair,
170
209
  getCoinType,
171
210
  getHDKey,
172
211
  isMnemonicValid,
212
+ publicKeyToAddress,
173
213
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neuraiproject/neurai-key",
3
- "version": "2.8.4",
3
+ "version": "2.8.6",
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.16.0",
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",