@aeternity/aepp-sdk 9.0.1 → 11.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -14
- package/dist/aepp-sdk.browser-script.js +1 -1
- package/dist/aepp-sdk.browser-script.js.LICENSE.txt +7 -36
- package/dist/aepp-sdk.browser-script.js.map +1 -1
- package/dist/aepp-sdk.browser.js +1 -1
- package/dist/aepp-sdk.browser.js.map +1 -1
- package/dist/aepp-sdk.js +1 -1
- package/dist/aepp-sdk.js.map +1 -1
- package/es/account/base.mjs +179 -0
- package/es/account/base.mjs.map +1 -0
- package/es/account/memory.mjs +99 -0
- package/es/account/memory.mjs.map +1 -0
- package/es/account/multiple.mjs +179 -0
- package/es/account/multiple.mjs.map +1 -0
- package/es/ae/aens.mjs +404 -0
- package/es/ae/aens.mjs.map +1 -0
- package/es/ae/{aepp.js → aepp.mjs} +9 -9
- package/es/ae/aepp.mjs.map +1 -0
- package/es/ae/contract.mjs +159 -0
- package/es/ae/contract.mjs.map +1 -0
- package/es/ae/index.mjs +207 -0
- package/es/ae/index.mjs.map +1 -0
- package/es/ae/oracle.mjs +339 -0
- package/es/ae/oracle.mjs.map +1 -0
- package/es/ae/{universal.js → universal.mjs} +10 -10
- package/es/ae/universal.mjs.map +1 -0
- package/es/ae/{wallet.js → wallet.mjs} +10 -10
- package/es/ae/wallet.mjs.map +1 -0
- package/es/aepp-wallet-communication/schema.d.ts +74 -0
- package/es/bignumber.d.ts +1 -7
- package/es/chain/{index.js → index.mjs} +29 -9
- package/es/chain/index.mjs.map +1 -0
- package/es/chain/node.mjs +442 -0
- package/es/chain/node.mjs.map +1 -0
- package/es/channel/handlers.mjs +1110 -0
- package/es/channel/handlers.mjs.map +1 -0
- package/es/channel/{index.js → index.mjs} +245 -472
- package/es/channel/index.mjs.map +1 -0
- package/es/channel/internal.mjs +283 -0
- package/es/channel/internal.mjs.map +1 -0
- package/es/contract/aci.mjs +477 -0
- package/es/contract/aci.mjs.map +1 -0
- package/es/contract/compiler.mjs +115 -0
- package/es/contract/compiler.mjs.map +1 -0
- package/es/contract/ga/helpers.mjs +30 -0
- package/es/contract/ga/helpers.mjs.map +1 -0
- package/es/contract/ga/index.mjs +178 -0
- package/es/contract/ga/index.mjs.map +1 -0
- package/es/errors.d.ts +284 -0
- package/es/index.mjs +57 -0
- package/es/index.mjs.map +1 -0
- package/es/node-pool/index.mjs +188 -0
- package/es/node-pool/index.mjs.map +1 -0
- package/es/node.mjs +133 -0
- package/es/node.mjs.map +1 -0
- package/es/other.d.ts +1 -1
- package/es/tx/builder/field-types.mjs +74 -0
- package/es/tx/builder/field-types.mjs.map +1 -0
- package/es/tx/builder/helpers.mjs +363 -0
- package/es/tx/builder/helpers.mjs.map +1 -0
- package/es/tx/builder/index.mjs +606 -0
- package/es/tx/builder/index.mjs.map +1 -0
- package/es/tx/builder/schema.mjs +760 -0
- package/es/tx/builder/schema.mjs.map +1 -0
- package/es/tx/{index.js → index.mjs} +107 -15
- package/es/tx/index.mjs.map +1 -0
- package/es/tx/tx-object.mjs +283 -0
- package/es/tx/tx-object.mjs.map +1 -0
- package/es/tx/tx.mjs +938 -0
- package/es/tx/tx.mjs.map +1 -0
- package/es/tx/validator.mjs +247 -0
- package/es/tx/validator.mjs.map +1 -0
- package/es/typings/blakejs/index.d.mjs +2 -0
- package/es/typings/blakejs/{index.d.js.map → index.d.mjs.map} +1 -1
- package/es/utils/aepp-wallet-communication/connection/{browser-runtime.js → browser-runtime.mjs} +27 -29
- package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs +183 -0
- package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/connection/{index.js → index.mjs} +7 -3
- package/es/utils/aepp-wallet-communication/connection/index.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/{content-script-bridge.js → content-script-bridge.mjs} +29 -23
- package/es/utils/aepp-wallet-communication/content-script-bridge.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/helpers.mjs +92 -0
- package/es/utils/aepp-wallet-communication/helpers.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs +375 -0
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs +275 -0
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs +541 -0
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/schema.mjs +102 -0
- package/es/utils/aepp-wallet-communication/schema.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/wallet-detector.mjs +110 -0
- package/es/utils/aepp-wallet-communication/wallet-detector.mjs.map +1 -0
- package/es/utils/amount-formatter.mjs +139 -0
- package/es/utils/amount-formatter.mjs.map +1 -0
- package/es/utils/async-init.mjs +64 -0
- package/es/utils/async-init.mjs.map +1 -0
- package/es/utils/bignumber.mjs +21 -0
- package/es/utils/bignumber.mjs.map +1 -0
- package/es/utils/{bytes.js → bytes.mjs} +16 -12
- package/es/utils/bytes.mjs.map +1 -0
- package/es/utils/{crypto-ts.js → crypto-ts.mjs} +11 -3
- package/es/utils/crypto-ts.mjs.map +1 -0
- package/es/utils/crypto.mjs +239 -0
- package/es/utils/crypto.mjs.map +1 -0
- package/es/utils/errors.mjs +689 -0
- package/es/utils/errors.mjs.map +1 -0
- package/es/utils/hd-wallet.mjs +111 -0
- package/es/utils/hd-wallet.mjs.map +1 -0
- package/es/utils/json-big.mjs +45 -0
- package/es/utils/json-big.mjs.map +1 -0
- package/es/utils/keystore.mjs +240 -0
- package/es/utils/keystore.mjs.map +1 -0
- package/es/utils/mptree.mjs +161 -0
- package/es/utils/mptree.mjs.map +1 -0
- package/es/utils/other.mjs +43 -0
- package/es/utils/other.mjs.map +1 -0
- package/es/utils/semver-satisfies.mjs +16 -0
- package/es/utils/semver-satisfies.mjs.map +1 -0
- package/es/utils/{string.js → string.mjs} +5 -14
- package/es/utils/string.mjs.map +1 -0
- package/es/utils/swagger.mjs +164 -0
- package/es/utils/swagger.mjs.map +1 -0
- package/package.json +55 -62
- package/src/account/base.js +3 -1
- package/src/account/memory.js +13 -8
- package/src/account/multiple.js +31 -22
- package/src/ae/aens.js +75 -77
- package/src/ae/aepp.js +2 -2
- package/src/ae/contract.js +27 -291
- package/src/ae/index.js +9 -7
- package/src/ae/oracle.js +51 -28
- package/src/ae/universal.js +4 -2
- package/src/ae/wallet.js +2 -2
- package/src/chain/index.js +29 -6
- package/src/chain/node.js +95 -67
- package/src/channel/handlers.js +30 -21
- package/src/channel/index.js +44 -20
- package/src/channel/internal.js +30 -24
- package/src/contract/aci.js +376 -0
- package/src/contract/compiler.js +41 -91
- package/src/contract/ga/helpers.js +10 -13
- package/src/contract/ga/index.js +42 -33
- package/src/index.js +9 -10
- package/src/node-pool/index.js +29 -17
- package/src/node.js +33 -36
- package/src/tx/builder/field-types.js +61 -0
- package/src/tx/builder/helpers.js +126 -84
- package/src/tx/builder/index.js +83 -41
- package/src/tx/builder/schema.js +50 -40
- package/src/tx/index.js +101 -12
- package/src/tx/tx-object.js +35 -15
- package/src/tx/tx.js +232 -153
- package/src/tx/validator.js +34 -12
- package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +11 -5
- package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +35 -13
- package/src/utils/aepp-wallet-communication/connection/index.js +2 -1
- package/src/utils/aepp-wallet-communication/content-script-bridge.js +14 -7
- package/src/utils/aepp-wallet-communication/helpers.js +6 -14
- package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +107 -60
- package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +24 -11
- package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +98 -56
- package/src/utils/aepp-wallet-communication/schema.ts +80 -0
- package/src/utils/aepp-wallet-communication/wallet-detector.js +6 -2
- package/src/utils/amount-formatter.ts +4 -3
- package/src/utils/async-init.js +10 -6
- package/src/utils/bignumber.ts +3 -10
- package/src/utils/bytes.ts +3 -2
- package/src/utils/crypto-ts.ts +2 -2
- package/src/utils/crypto.js +10 -144
- package/src/utils/errors.ts +683 -0
- package/src/utils/hd-wallet.js +11 -26
- package/src/utils/keystore.js +57 -53
- package/src/utils/mptree.ts +12 -6
- package/src/utils/other.ts +1 -1
- package/src/utils/semver-satisfies.ts +1 -1
- package/src/utils/swagger.js +75 -45
- package/es/account/base.js +0 -259
- package/es/account/base.js.map +0 -1
- package/es/account/memory.js +0 -86
- package/es/account/memory.js.map +0 -1
- package/es/account/multiple.js +0 -267
- package/es/account/multiple.js.map +0 -1
- package/es/ae/aens.js +0 -829
- package/es/ae/aens.js.map +0 -1
- package/es/ae/aepp.js.map +0 -1
- package/es/ae/contract.js +0 -985
- package/es/ae/contract.js.map +0 -1
- package/es/ae/index.js +0 -442
- package/es/ae/index.js.map +0 -1
- package/es/ae/oracle.js +0 -690
- package/es/ae/oracle.js.map +0 -1
- package/es/ae/universal.js.map +0 -1
- package/es/ae/wallet.js.map +0 -1
- package/es/chain/index.js.map +0 -1
- package/es/chain/node.js +0 -1119
- package/es/chain/node.js.map +0 -1
- package/es/channel/handlers.js +0 -1601
- package/es/channel/handlers.js.map +0 -1
- package/es/channel/index.js.map +0 -1
- package/es/channel/internal.js +0 -438
- package/es/channel/internal.js.map +0 -1
- package/es/contract/aci/helpers.js +0 -298
- package/es/contract/aci/helpers.js.map +0 -1
- package/es/contract/aci/index.js +0 -517
- package/es/contract/aci/index.js.map +0 -1
- package/es/contract/aci/transformation.js +0 -742
- package/es/contract/aci/transformation.js.map +0 -1
- package/es/contract/compiler.js +0 -316
- package/es/contract/compiler.js.map +0 -1
- package/es/contract/ga/helpers.js +0 -120
- package/es/contract/ga/helpers.js.map +0 -1
- package/es/contract/ga/index.js +0 -307
- package/es/contract/ga/index.js.map +0 -1
- package/es/contract/index.js +0 -158
- package/es/contract/index.js.map +0 -1
- package/es/index.js +0 -57
- package/es/index.js.map +0 -1
- package/es/node-pool/index.js +0 -204
- package/es/node-pool/index.js.map +0 -1
- package/es/node.js +0 -228
- package/es/node.js.map +0 -1
- package/es/tx/builder/helpers.js +0 -355
- package/es/tx/builder/helpers.js.map +0 -1
- package/es/tx/builder/index.js +0 -686
- package/es/tx/builder/index.js.map +0 -1
- package/es/tx/builder/schema.js +0 -695
- package/es/tx/builder/schema.js.map +0 -1
- package/es/tx/index.js.map +0 -1
- package/es/tx/tx-object.js +0 -297
- package/es/tx/tx-object.js.map +0 -1
- package/es/tx/tx.js +0 -1757
- package/es/tx/tx.js.map +0 -1
- package/es/tx/validator.js +0 -344
- package/es/tx/validator.js.map +0 -1
- package/es/typings/blakejs/index.d.js +0 -2
- package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +0 -1
- package/es/utils/aepp-wallet-communication/connection/browser-window-message.js +0 -211
- package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +0 -1
- package/es/utils/aepp-wallet-communication/connection/index.js.map +0 -1
- package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +0 -1
- package/es/utils/aepp-wallet-communication/helpers.js +0 -148
- package/es/utils/aepp-wallet-communication/helpers.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js +0 -652
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.js +0 -285
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js +0 -763
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +0 -1
- package/es/utils/aepp-wallet-communication/schema.js +0 -103
- package/es/utils/aepp-wallet-communication/schema.js.map +0 -1
- package/es/utils/aepp-wallet-communication/wallet-detector.js +0 -135
- package/es/utils/aepp-wallet-communication/wallet-detector.js.map +0 -1
- package/es/utils/amount-formatter.js +0 -152
- package/es/utils/amount-formatter.js.map +0 -1
- package/es/utils/async-init.js +0 -103
- package/es/utils/async-init.js.map +0 -1
- package/es/utils/bignumber.js +0 -36
- package/es/utils/bignumber.js.map +0 -1
- package/es/utils/bytes.js.map +0 -1
- package/es/utils/crypto-ts.js.map +0 -1
- package/es/utils/crypto.js +0 -358
- package/es/utils/crypto.js.map +0 -1
- package/es/utils/hd-wallet.js +0 -148
- package/es/utils/hd-wallet.js.map +0 -1
- package/es/utils/json-big.js +0 -78
- package/es/utils/json-big.js.map +0 -1
- package/es/utils/keystore.js +0 -393
- package/es/utils/keystore.js.map +0 -1
- package/es/utils/mptree.js +0 -198
- package/es/utils/mptree.js.map +0 -1
- package/es/utils/other.js +0 -93
- package/es/utils/other.js.map +0 -1
- package/es/utils/semver-satisfies.js +0 -35
- package/es/utils/semver-satisfies.js.map +0 -1
- package/es/utils/string.js.map +0 -1
- package/es/utils/swagger.js +0 -269
- package/es/utils/swagger.js.map +0 -1
- package/src/contract/aci/helpers.js +0 -140
- package/src/contract/aci/index.js +0 -191
- package/src/contract/aci/transformation.js +0 -464
- package/src/contract/index.js +0 -159
- package/src/utils/aepp-wallet-communication/schema.js +0 -110
package/es/utils/bignumber.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import _Number$isInteger from "@babel/runtime-corejs3/core-js-stable/number/is-integer";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Big Number Helpers
|
|
5
|
-
* @module @aeternity/aepp-sdk/es/utils/bignumber
|
|
6
|
-
* @example import { parseBigNumber, isBigNumber, ceil } from '@aeternity/aepp-sdk/es/utils/bignumber'
|
|
7
|
-
*/
|
|
8
|
-
import BigNumber from 'bignumber.js';
|
|
9
|
-
/**
|
|
10
|
-
* Convert number to string
|
|
11
|
-
* @param {String|Number|BigNumber} number number to convert
|
|
12
|
-
* @return {String}
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
export var parseBigNumber = function parseBigNumber(number) {
|
|
16
|
-
return new BigNumber(number.toString()).toString(10);
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* Check if value is BigNumber, Number or number string representation
|
|
20
|
-
* @param {String|Number|BigNumber} number number to convert
|
|
21
|
-
* @return {Boolean}
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
export var isBigNumber = function isBigNumber(number) {
|
|
25
|
-
return !isNaN(number) || _Number$isInteger(number) || BigNumber.isBigNumber(number);
|
|
26
|
-
};
|
|
27
|
-
/**
|
|
28
|
-
* BigNumber ceil operation
|
|
29
|
-
* @param {BigNumber} bigNumber
|
|
30
|
-
* @return {BigNumber}
|
|
31
|
-
*/
|
|
32
|
-
|
|
33
|
-
export var ceil = function ceil(bigNumber) {
|
|
34
|
-
return bigNumber.integerValue(BigNumber.ROUND_CEIL);
|
|
35
|
-
};
|
|
36
|
-
//# sourceMappingURL=bignumber.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/bignumber.ts"],"names":["BigNumber","parseBigNumber","number","toString","isBigNumber","isNaN","ceil","bigNumber","integerValue","ROUND_CEIL"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA,OAAOA,SAAP,MAAsB,cAAtB;AAEA;AACA;AACA;AACA;AACA;;AACA,OAAO,IAAMC,cAAc,GAAG,SAAjBA,cAAiB,CAACC,MAAD;AAAA,SAC5B,IAAIF,SAAJ,CAAcE,MAAM,CAACC,QAAP,EAAd,EAAiCA,QAAjC,CAA0C,EAA1C,CAD4B;AAAA,CAAvB;AAGP;AACA;AACA;AACA;AACA;;AACA,OAAO,IAAMC,WAAW,GAAG,SAAdA,WAAc,CAACF,MAAD;AAAA,SACzB,CAACG,KAAK,CAACH,MAAD,CAAN,IAA4B,kBAAiBA,MAAjB,CAA5B,IAAwDF,SAAS,CAACI,WAAV,CAAsBF,MAAtB,CAD/B;AAAA,CAApB;AAGP;AACA;AACA;AACA;AACA;;AACA,OAAO,IAAMI,IAAI,GAAG,SAAPA,IAAO,CAACC,SAAD;AAAA,SAAqCA,SAAS,CAACC,YAAV,CAAuBR,SAAS,CAACS,UAAjC,CAArC;AAAA,CAAb","sourcesContent":["/**\n * Big Number Helpers\n * @module @aeternity/aepp-sdk/es/utils/bignumber\n * @example import { parseBigNumber, isBigNumber, ceil } from '@aeternity/aepp-sdk/es/utils/bignumber'\n */\nimport BigNumber from 'bignumber.js'\n\n/**\n * Convert number to string\n * @param {String|Number|BigNumber} number number to convert\n * @return {String}\n */\nexport const parseBigNumber = (number: string | number | BigNumber): string =>\n new BigNumber(number.toString()).toString(10)\n\n/**\n * Check if value is BigNumber, Number or number string representation\n * @param {String|Number|BigNumber} number number to convert\n * @return {Boolean}\n */\nexport const isBigNumber = (number: string | number | BigNumber): boolean =>\n !isNaN(number as number) || Number.isInteger(number) || BigNumber.isBigNumber(number)\n\n/**\n * BigNumber ceil operation\n * @param {BigNumber} bigNumber\n * @return {BigNumber}\n */\nexport const ceil = (bigNumber: BigNumber): BigNumber => bigNumber.integerValue(BigNumber.ROUND_CEIL)\n"],"file":"bignumber.js"}
|
package/es/utils/bytes.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/bytes.ts"],"names":["BigNumber","isBase64","isHex","bigNumberToByteArray","x","isInteger","Error","toFixed","hexString","toString","length","Buffer","from","toBytes","val","big","undefined","isBigNumber","str2buf","str","enc"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAOA,SAAP,MAAsB,cAAtB;AACA,SAASC,QAAT,EAAmBC,KAAnB,QAAgC,UAAhC;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,oBAAT,CAA+BC,CAA/B,EAAqD;AAC1D,MAAI,CAACA,CAAC,CAACC,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ACkB,GAAD,CAAR,GAAgB,QAAhB,GAA2BH,SAFlE,CAAP;AAID","sourcesContent":["/*\n * ISC License (ISC)\n * Copyright 2018 aeternity developers\n *\n * Permission to use, copy, modify, and/or distribute this software for any\n * purpose with or without fee is hereby granted, provided that the above\n * copyright notice and this permission notice appear in all copies.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\n * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\n * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\n * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\n * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\n * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\n * PERFORMANCE OF THIS SOFTWARE.\n */\nimport BigNumber from 'bignumber.js'\nimport { isBase64, isHex } from './string'\n\n/**\n * Bytes module\n * @module @aeternity/aepp-sdk/es/utils/bytes\n * @example import { Crypto } from '@aeternity/aepp-sdk'\n */\n\n/**\n * Convert bignumber to byte array\n * @param {BigNumber} x bignumber instance\n * @return Buffer\n */\nexport function bigNumberToByteArray (x: BigNumber): Buffer {\n if (!x.isInteger()) throw new Error(`Unexpected not integer value: ${x.toFixed()}`)\n let hexString = x.toString(16)\n if (hexString.length % 2 === 1) hexString = '0' + hexString\n return Buffer.from(hexString, 'hex')\n}\n\n/**\n * Convert string, number, or BigNumber to byte array\n * @param {null|string|number|BigNumber} val\n * @param {boolean} big enables force conversion to BigNumber\n * @return Buffer\n */\nexport function toBytes (val?: null | string | number | BigNumber, big: boolean = false): Buffer {\n // Encode a value to bytes.\n // If the value is an int it will be encoded as bytes big endian\n // Raises ValueError if the input is not an int or string\n\n if (val === undefined || val === null) return Buffer.from([])\n if (Number.isInteger(val) || BigNumber.isBigNumber(val) || big) {\n if (!BigNumber.isBigNumber(val)) val = new BigNumber(val)\n return bigNumberToByteArray(val)\n }\n if (typeof val === 'string') {\n return Buffer.from(val)\n }\n throw new Error('Byte serialization not supported')\n}\n\n/**\n * Convert a string to a Buffer. If encoding is not specified, hex-encoding\n * will be used if the input is valid hex. If the input is valid base64 but\n * not valid hex, base64 will be used. Otherwise, utf8 will be used.\n * @param {string} str String to be converted.\n * @param {string} [enc] Encoding of the input string.\n * @return {buffer} Buffer containing the input data.\n */\nexport function str2buf (str: string, enc?: BufferEncoding): Buffer {\n return Buffer.from(\n str,\n enc ?? (isHex(str) ? 'hex' : undefined) ?? (isBase64(str) ? 'base64' : undefined)\n )\n}\n"],"file":"bytes.js"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/crypto-ts.ts"],"names":["blake2b","hash","input","Buffer","from"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA,SAASA,OAAT,QAAwB,SAAxB;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,IAAT,CAAeC,KAAf,EAAoC;AACzC,SAAOC,MAAM,CAACC,IAAP,CAAYJ,OAAO,CAACE,KAAD,EAAQ,IAAR,EAAc,EAAd,CAAnB,CAAP,CADyC,CACI;AAC9C","sourcesContent":["/*\n * ISC License (ISC)\n * Copyright 2021 aeternity developers\n *\n * Permission to use, copy, modify, and/or distribute this software for any\n * purpose with or without fee is hereby granted, provided that the above\n * copyright notice and this permission notice appear in all copies.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\n * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\n * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\n * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\n * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\n * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\n * PERFORMANCE OF THIS SOFTWARE.\n */\n\nimport { blake2b } from 'blakejs'\nimport type { Data } from 'blakejs'\n\n/**\n * Calculate 256bits Blake2b hash of `input`\n * @rtype (input: String) => hash: String\n * @param {Data} input - Data to hash\n * @return {Buffer} Hash\n */\nexport function hash (input: Data): Buffer {\n return Buffer.from(blake2b(input, null, 32)) // 256 bits\n}\n"],"file":"crypto-ts.js"}
|
package/es/utils/crypto.js
DELETED
|
@@ -1,358 +0,0 @@
|
|
|
1
|
-
import _Number$MAX_SAFE_INTEGER from "@babel/runtime-corejs3/core-js-stable/number/max-safe-integer";
|
|
2
|
-
import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
|
|
3
|
-
import _sliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/slice";
|
|
4
|
-
import _findIndexInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/find-index";
|
|
5
|
-
|
|
6
|
-
/*
|
|
7
|
-
* ISC License (ISC)
|
|
8
|
-
* Copyright 2018 aeternity developers
|
|
9
|
-
*
|
|
10
|
-
* Permission to use, copy, modify, and/or distribute this software for any
|
|
11
|
-
* purpose with or without fee is hereby granted, provided that the above
|
|
12
|
-
* copyright notice and this permission notice appear in all copies.
|
|
13
|
-
*
|
|
14
|
-
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
15
|
-
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
16
|
-
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
17
|
-
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
18
|
-
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
19
|
-
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
20
|
-
* PERFORMANCE OF THIS SOFTWARE.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Crypto module
|
|
25
|
-
* @module @aeternity/aepp-sdk/es/utils/crypto
|
|
26
|
-
* @example import { Crypto } from '@aeternity/aepp-sdk'
|
|
27
|
-
*/
|
|
28
|
-
import bs58check from 'bs58check';
|
|
29
|
-
import ed2curve from 'ed2curve';
|
|
30
|
-
import nacl from 'tweetnacl';
|
|
31
|
-
import aesjs from 'aes-js';
|
|
32
|
-
import shajs from 'sha.js';
|
|
33
|
-
import { str2buf, toBytes } from './bytes';
|
|
34
|
-
import { decode } from '../tx/builder/helpers';
|
|
35
|
-
import { hash } from './crypto-ts';
|
|
36
|
-
export * from './crypto-ts';
|
|
37
|
-
var Ecb = aesjs.ModeOfOperation.ecb;
|
|
38
|
-
/**
|
|
39
|
-
* Generate address from secret key
|
|
40
|
-
* @rtype (secret: String) => tx: Promise[String]
|
|
41
|
-
* @param {String} secret - Private key
|
|
42
|
-
* @return {String} Public key
|
|
43
|
-
*/
|
|
44
|
-
|
|
45
|
-
export function getAddressFromPriv(secret) {
|
|
46
|
-
var keys = nacl.sign.keyPair.fromSecretKey(str2buf(secret));
|
|
47
|
-
var publicBuffer = Buffer.from(keys.publicKey);
|
|
48
|
-
return "ak_".concat(encodeBase58Check(publicBuffer));
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Check if address is valid
|
|
52
|
-
* @rtype (input: String) => valid: Boolean
|
|
53
|
-
* @param {String} address - Address
|
|
54
|
-
* @param {String} prefix Transaction prefix. Default: 'ak'
|
|
55
|
-
* @return {Boolean} valid
|
|
56
|
-
*/
|
|
57
|
-
|
|
58
|
-
export function isAddressValid(address) {
|
|
59
|
-
var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'ak';
|
|
60
|
-
if (typeof address !== 'string') return false;
|
|
61
|
-
|
|
62
|
-
try {
|
|
63
|
-
return decode(address, prefix).length === 32;
|
|
64
|
-
} catch (e) {
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Parse decimal address and return base58Check encoded address with prefix 'ak'
|
|
70
|
-
* @rtype (input: String) => address: String
|
|
71
|
-
* @param {String} decimalAddress - Address
|
|
72
|
-
* @return {String} address
|
|
73
|
-
*/
|
|
74
|
-
|
|
75
|
-
export function addressFromDecimal(decimalAddress) {
|
|
76
|
-
return aeEncodeKey(toBytes(decimalAddress, true));
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Calculate SHA256 hash of `input`
|
|
80
|
-
* @rtype (input: String) => hash: String
|
|
81
|
-
* @param {String} input - Data to hash
|
|
82
|
-
* @return {String} Hash
|
|
83
|
-
*/
|
|
84
|
-
|
|
85
|
-
export function sha256hash(input) {
|
|
86
|
-
return shajs('sha256').update(input).digest();
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Generate a random salt (positive integer)
|
|
90
|
-
* @rtype () => salt: Number
|
|
91
|
-
* @return {Number} random salt
|
|
92
|
-
*/
|
|
93
|
-
|
|
94
|
-
export function salt() {
|
|
95
|
-
return Math.floor(Math.random() * Math.floor(_Number$MAX_SAFE_INTEGER));
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Base64check encode given `input`
|
|
99
|
-
* @rtype (input: String|buffer) => Buffer
|
|
100
|
-
* @param {String} input - Data to encode
|
|
101
|
-
* @return {Buffer} Base64check encoded data
|
|
102
|
-
*/
|
|
103
|
-
|
|
104
|
-
export function encodeBase64Check(input) {
|
|
105
|
-
var buffer = Buffer.from(input);
|
|
106
|
-
var checksum = checkSumFn(input);
|
|
107
|
-
|
|
108
|
-
var payloadWithChecksum = _concatInstanceProperty(Buffer).call(Buffer, [buffer, checksum], buffer.length + 4);
|
|
109
|
-
|
|
110
|
-
return payloadWithChecksum.toString('base64');
|
|
111
|
-
}
|
|
112
|
-
export function checkSumFn(payload) {
|
|
113
|
-
var _context;
|
|
114
|
-
|
|
115
|
-
return _sliceInstanceProperty(_context = sha256hash(sha256hash(payload))).call(_context, 0, 4);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function decodeRaw(buffer) {
|
|
119
|
-
var payload = _sliceInstanceProperty(buffer).call(buffer, 0, -4);
|
|
120
|
-
|
|
121
|
-
var checksum = _sliceInstanceProperty(buffer).call(buffer, -4);
|
|
122
|
-
|
|
123
|
-
var newChecksum = checkSumFn(payload);
|
|
124
|
-
if (!checksum.equals(newChecksum)) return;
|
|
125
|
-
return payload;
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Base64check decode given `str`
|
|
129
|
-
* @rtype (str: String) => Buffer
|
|
130
|
-
* @param {String} str - Data to decode
|
|
131
|
-
* @return {Buffer} Base64check decoded data
|
|
132
|
-
*/
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
export function decodeBase64Check(str) {
|
|
136
|
-
var buffer = Buffer.from(str, 'base64');
|
|
137
|
-
var payload = decodeRaw(buffer);
|
|
138
|
-
if (!payload) throw new Error('Invalid checksum');
|
|
139
|
-
return Buffer.from(payload);
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Base58 encode given `input`
|
|
143
|
-
* @rtype (input: String) => String
|
|
144
|
-
* @param {String|Buffer} input - Data to encode
|
|
145
|
-
* @return {String} Base58 encoded data
|
|
146
|
-
*/
|
|
147
|
-
|
|
148
|
-
export function encodeBase58Check(input) {
|
|
149
|
-
return bs58check.encode(Buffer.from(input));
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Base58 decode given `str`
|
|
153
|
-
* @rtype (str: String) => Buffer
|
|
154
|
-
* @param {String} str - Data to decode
|
|
155
|
-
* @return {Buffer} Base58 decoded data
|
|
156
|
-
*/
|
|
157
|
-
|
|
158
|
-
export function decodeBase58Check(str) {
|
|
159
|
-
return bs58check.decode(str);
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Converts a positive integer to the smallest possible
|
|
163
|
-
* representation in a binary digit representation
|
|
164
|
-
* @rtype (value: Number) => Buffer
|
|
165
|
-
* @param {Number} value - Value to encode
|
|
166
|
-
* @return {Buffer} - Encoded data
|
|
167
|
-
*/
|
|
168
|
-
|
|
169
|
-
export function encodeUnsigned(value) {
|
|
170
|
-
var binary = Buffer.allocUnsafe(4);
|
|
171
|
-
binary.writeUInt32BE(value);
|
|
172
|
-
return _sliceInstanceProperty(binary).call(binary, _findIndexInstanceProperty(binary).call(binary, function (i) {
|
|
173
|
-
return i !== 0;
|
|
174
|
-
}));
|
|
175
|
-
} // Todo Duplicated in tx builder. remove
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Compute contract address
|
|
179
|
-
* @rtype (owner: String, nonce: Number) => String
|
|
180
|
-
* @param {String} owner - Address of contract owner
|
|
181
|
-
* @param {Number} nonce - Round when contract was created
|
|
182
|
-
* @return {String} - Contract address
|
|
183
|
-
*/
|
|
184
|
-
|
|
185
|
-
export function encodeContractAddress(owner, nonce) {
|
|
186
|
-
var publicKey = decode(owner, 'ak');
|
|
187
|
-
|
|
188
|
-
var binary = _concatInstanceProperty(Buffer).call(Buffer, [publicKey, encodeUnsigned(nonce)]);
|
|
189
|
-
|
|
190
|
-
return "ct_".concat(encodeBase58Check(hash(binary)));
|
|
191
|
-
} // KEY-PAIR HELPERS
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Generate keyPair from secret key
|
|
195
|
-
* @rtype (secret: Uint8Array) => KeyPair
|
|
196
|
-
* @param {Uint8Array} secret - secret key
|
|
197
|
-
* @return {Object} - Object with Private(privateKey) and Public(publicKey) keys
|
|
198
|
-
*/
|
|
199
|
-
|
|
200
|
-
export function generateKeyPairFromSecret(secret) {
|
|
201
|
-
return nacl.sign.keyPair.fromSecretKey(secret);
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Generate a random ED25519 keypair
|
|
205
|
-
* @rtype (raw: Boolean) => {publicKey: String, secretKey: String} | {publicKey: Buffer, secretKey: Buffer}
|
|
206
|
-
* @param {Boolean} raw - Whether to return raw (binary) keys
|
|
207
|
-
* @return {Object} Key pair
|
|
208
|
-
*/
|
|
209
|
-
|
|
210
|
-
export function generateKeyPair() {
|
|
211
|
-
var raw = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
212
|
-
// <node>/apps/aens/test/aens_test_utils.erl
|
|
213
|
-
var keyPair = nacl.sign.keyPair();
|
|
214
|
-
var publicBuffer = Buffer.from(keyPair.publicKey);
|
|
215
|
-
var secretBuffer = Buffer.from(keyPair.secretKey);
|
|
216
|
-
|
|
217
|
-
if (raw) {
|
|
218
|
-
return {
|
|
219
|
-
publicKey: publicBuffer,
|
|
220
|
-
secretKey: secretBuffer
|
|
221
|
-
};
|
|
222
|
-
} else {
|
|
223
|
-
return {
|
|
224
|
-
publicKey: "ak_".concat(encodeBase58Check(publicBuffer)),
|
|
225
|
-
secretKey: secretBuffer.toString('hex')
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Encrypt given data using `password`
|
|
231
|
-
* @rtype (password: String, binaryData: Buffer) => Uint8Array
|
|
232
|
-
* @param {String} password - Password to encrypt with
|
|
233
|
-
* @param {Buffer} binaryData - Data to encrypt
|
|
234
|
-
* @return {Uint8Array} Encrypted data
|
|
235
|
-
*/
|
|
236
|
-
|
|
237
|
-
export function encryptKey(password, binaryData) {
|
|
238
|
-
var hashedPasswordBytes = sha256hash(password);
|
|
239
|
-
var aesEcb = new Ecb(hashedPasswordBytes);
|
|
240
|
-
return aesEcb.encrypt(binaryData);
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Decrypt given data using `password`
|
|
244
|
-
* @rtype (password: String, encrypted: String) => Uint8Array
|
|
245
|
-
* @param {String} password - Password to decrypt with
|
|
246
|
-
* @param {String} encrypted - Data to decrypt
|
|
247
|
-
* @return {Buffer} Decrypted data
|
|
248
|
-
*/
|
|
249
|
-
|
|
250
|
-
export function decryptKey(password, encrypted) {
|
|
251
|
-
var encryptedBytes = Buffer.from(encrypted);
|
|
252
|
-
var hashedPasswordBytes = sha256hash(password);
|
|
253
|
-
var aesEcb = new Ecb(hashedPasswordBytes);
|
|
254
|
-
return Buffer.from(aesEcb.decrypt(encryptedBytes));
|
|
255
|
-
} // SIGNATURES
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* Generate signature
|
|
259
|
-
* @rtype (data: String|Buffer, privateKey: Buffer) => Buffer
|
|
260
|
-
* @param {String|Buffer} data - Data to sign
|
|
261
|
-
* @param {String|Buffer} privateKey - Key to sign with
|
|
262
|
-
* @return {Buffer|Uint8Array} Signature
|
|
263
|
-
*/
|
|
264
|
-
|
|
265
|
-
export function sign(data, privateKey) {
|
|
266
|
-
return nacl.sign.detached(Buffer.from(data), Buffer.from(privateKey));
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* Verify that signature was signed by public key
|
|
270
|
-
* @rtype (str: String, signature: Buffer, publicKey: Buffer) => Boolean
|
|
271
|
-
* @param {String|Buffer} str - Data to verify
|
|
272
|
-
* @param {Buffer} signature - Signature to verify
|
|
273
|
-
* @param {Buffer} publicKey - Key to verify against
|
|
274
|
-
* @return {Boolean} Valid?
|
|
275
|
-
*/
|
|
276
|
-
|
|
277
|
-
export function verify(str, signature, publicKey) {
|
|
278
|
-
return nacl.sign.detached.verify(new Uint8Array(str), signature, publicKey);
|
|
279
|
-
}
|
|
280
|
-
export function messageToHash(message) {
|
|
281
|
-
var p = Buffer.from('aeternity Signed Message:\n', 'utf8');
|
|
282
|
-
var msg = Buffer.from(message, 'utf8');
|
|
283
|
-
if (msg.length >= 0xFD) throw new Error('message too long');
|
|
284
|
-
return hash(_concatInstanceProperty(Buffer).call(Buffer, [Buffer.from([p.length]), p, Buffer.from([msg.length]), msg]));
|
|
285
|
-
}
|
|
286
|
-
export function signMessage(message, privateKey) {
|
|
287
|
-
return sign(messageToHash(message), privateKey);
|
|
288
|
-
}
|
|
289
|
-
export function verifyMessage(str, signature, publicKey) {
|
|
290
|
-
return verify(messageToHash(str), signature, publicKey);
|
|
291
|
-
}
|
|
292
|
-
/**
|
|
293
|
-
* æternity readable public keys are the base58-encoded public key, prepended
|
|
294
|
-
* with 'ak_'
|
|
295
|
-
* @rtype (binaryKey: Buffer) => String
|
|
296
|
-
* @param {Buffer} binaryKey - Key to encode
|
|
297
|
-
* @return {String} Encoded key
|
|
298
|
-
*/
|
|
299
|
-
|
|
300
|
-
export function aeEncodeKey(binaryKey) {
|
|
301
|
-
var publicKeyBuffer = Buffer.from(binaryKey, 'hex');
|
|
302
|
-
var pubKeyAddress = encodeBase58Check(publicKeyBuffer);
|
|
303
|
-
return "ak_".concat(pubKeyAddress);
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Check key pair for validity
|
|
307
|
-
*
|
|
308
|
-
* Sign a message, and then verifying that signature
|
|
309
|
-
* @rtype (privateKey: Buffer, publicKey: Buffer) => Boolean
|
|
310
|
-
* @param {Buffer} privateKey - Private key to verify
|
|
311
|
-
* @param {Buffer} publicKey - Public key to verify
|
|
312
|
-
* @return {Boolean} Valid?
|
|
313
|
-
*/
|
|
314
|
-
|
|
315
|
-
export function isValidKeypair(privateKey, publicKey) {
|
|
316
|
-
var message = Buffer.from('TheMessage');
|
|
317
|
-
var signature = sign(message, privateKey);
|
|
318
|
-
return verify(message, signature, publicKey);
|
|
319
|
-
}
|
|
320
|
-
/**
|
|
321
|
-
* This function encrypts a message using base58check encoded and 'ak' prefixed
|
|
322
|
-
* publicKey such that only the corresponding secretKey will
|
|
323
|
-
* be able to decrypt
|
|
324
|
-
* @rtype (msg: String, publicKey: String) => Object
|
|
325
|
-
* @param {Buffer} msg - Data to encode
|
|
326
|
-
* @param {String} publicKey - Public key
|
|
327
|
-
* @return {Object}
|
|
328
|
-
*/
|
|
329
|
-
|
|
330
|
-
export function encryptData(msg, publicKey) {
|
|
331
|
-
var ephemeralKeyPair = nacl.box.keyPair();
|
|
332
|
-
var pubKeyUInt8Array = decode(publicKey, 'ak');
|
|
333
|
-
var nonce = nacl.randomBytes(nacl.box.nonceLength);
|
|
334
|
-
var encryptedMessage = nacl.box(Buffer.from(msg), nonce, ed2curve.convertPublicKey(pubKeyUInt8Array), ephemeralKeyPair.secretKey);
|
|
335
|
-
return {
|
|
336
|
-
ciphertext: Buffer.from(encryptedMessage).toString('hex'),
|
|
337
|
-
ephemPubKey: Buffer.from(ephemeralKeyPair.publicKey).toString('hex'),
|
|
338
|
-
nonce: Buffer.from(nonce).toString('hex'),
|
|
339
|
-
version: 'x25519-xsalsa20-poly1305'
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
/**
|
|
343
|
-
* This function decrypt a message using secret key
|
|
344
|
-
* @rtype (secretKey: String, encryptedData: Object) => Buffer|null
|
|
345
|
-
* @param {String} secretKey - Secret key
|
|
346
|
-
* @param {Object} encryptedData - Encrypted data
|
|
347
|
-
* @return {Buffer|null}
|
|
348
|
-
*/
|
|
349
|
-
|
|
350
|
-
export function decryptData(secretKey, encryptedData) {
|
|
351
|
-
var receiverSecretKeyUint8Array = ed2curve.convertSecretKey(Buffer.from(secretKey, 'hex'));
|
|
352
|
-
var nonce = Buffer.from(encryptedData.nonce, 'hex');
|
|
353
|
-
var ciphertext = Buffer.from(encryptedData.ciphertext, 'hex');
|
|
354
|
-
var ephemPubKey = Buffer.from(encryptedData.ephemPubKey, 'hex');
|
|
355
|
-
var decrypted = nacl.box.open(ciphertext, nonce, ephemPubKey, receiverSecretKeyUint8Array);
|
|
356
|
-
return decrypted ? Buffer.from(decrypted) : decrypted;
|
|
357
|
-
}
|
|
358
|
-
//# sourceMappingURL=crypto.js.map
|
package/es/utils/crypto.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/crypto.js"],"names":["bs58check","ed2curve","nacl","aesjs","shajs","str2buf","toBytes","decode","hash","Ecb","ModeOfOperation","ecb","getAddressFromPriv","secret","keys","sign","keyPair","fromSecretKey","publicBuffer","Buffer","from","publicKey","encodeBase58Check","isAddressValid","address","prefix","length","e","addressFromDecimal","decimalAddress","aeEncodeKey","sha256hash","input","update","digest","salt","Math","floor","random","encodeBase64Check","buffer","checksum","checkSumFn","payloadWithChecksum","toString","payload","decodeRaw","newChecksum","equals","decodeBase64Check","str","Error","encode","decodeBase58Check","encodeUnsigned","value","binary","allocUnsafe","writeUInt32BE","i","encodeContractAddress","owner","nonce","generateKeyPairFromSecret","generateKeyPair","raw","secretBuffer","secretKey","encryptKey","password","binaryData","hashedPasswordBytes","aesEcb","encrypt","decryptKey","encrypted","encryptedBytes","decrypt","data","privateKey","detached","verify","signature","Uint8Array","messageToHash","message","p","msg","signMessage","verifyMessage","binaryKey","publicKeyBuffer","pubKeyAddress","isValidKeypair","encryptData","ephemeralKeyPair","box","pubKeyUInt8Array","randomBytes","nonceLength","encryptedMessage","convertPublicKey","ciphertext","ephemPubKey","version","decryptData","encryptedData","receiverSecretKeyUint8Array","convertSecretKey","decrypted","open"],"mappings":";;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AAEA,OAAOA,SAAP,MAAsB,WAAtB;AACA,OAAOC,QAAP,MAAqB,UAArB;AACA,OAAOC,IAAP,MAAiB,WAAjB;AACA,OAAOC,KAAP,MAAkB,QAAlB;AACA,OAAOC,KAAP,MAAkB,QAAlB;AAEA,SAASC,OAAT,EAAkBC,OAAlB,QAAiC,SAAjC;AACA,SAASC,MAAT,QAAuB,uBAAvB;AACA,SAASC,IAA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\n *\n * Permission to use, copy, modify, and/or distribute this software for any\n * purpose with or without fee is hereby granted, provided that the above\n * copyright notice and this permission notice appear in all copies.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\n * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\n * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\n * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\n * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\n * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\n * PERFORMANCE OF THIS SOFTWARE.\n */\n\n/**\n * Crypto module\n * @module @aeternity/aepp-sdk/es/utils/crypto\n * @example import { Crypto } from '@aeternity/aepp-sdk'\n */\n\nimport bs58check from 'bs58check'\nimport ed2curve from 'ed2curve'\nimport nacl from 'tweetnacl'\nimport aesjs from 'aes-js'\nimport shajs from 'sha.js'\n\nimport { str2buf, toBytes } from './bytes'\nimport { decode } from '../tx/builder/helpers'\nimport { hash } from './crypto-ts'\n\nexport * from './crypto-ts'\n\nconst Ecb = aesjs.ModeOfOperation.ecb\n\n/**\n * Generate address from secret key\n * @rtype (secret: String) => tx: Promise[String]\n * @param {String} secret - Private key\n * @return {String} Public key\n */\nexport function getAddressFromPriv (secret) {\n const keys = nacl.sign.keyPair.fromSecretKey(str2buf(secret))\n const publicBuffer = Buffer.from(keys.publicKey)\n return `ak_${encodeBase58Check(publicBuffer)}`\n}\n\n/**\n * Check if address is valid\n * @rtype (input: String) => valid: Boolean\n * @param {String} address - Address\n * @param {String} prefix Transaction prefix. Default: 'ak'\n * @return {Boolean} valid\n */\nexport function isAddressValid (address, prefix = 'ak') {\n if (typeof address !== 'string') return false\n try {\n return decode(address, prefix).length === 32\n } catch (e) {\n return false\n }\n}\n\n/**\n * Parse decimal address and return base58Check encoded address with prefix 'ak'\n * @rtype (input: String) => address: String\n * @param {String} decimalAddress - Address\n * @return {String} address\n */\nexport function addressFromDecimal (decimalAddress) {\n return aeEncodeKey(toBytes(decimalAddress, true))\n}\n\n/**\n * Calculate SHA256 hash of `input`\n * @rtype (input: String) => hash: String\n * @param {String} input - Data to hash\n * @return {String} Hash\n */\nexport function sha256hash (input) {\n return shajs('sha256').update(input).digest()\n}\n\n/**\n * Generate a random salt (positive integer)\n * @rtype () => salt: Number\n * @return {Number} random salt\n */\nexport function salt () {\n return Math.floor(Math.random() * Math.floor(Number.MAX_SAFE_INTEGER))\n}\n\n/**\n * Base64check encode given `input`\n * @rtype (input: String|buffer) => Buffer\n * @param {String} input - Data to encode\n * @return {Buffer} Base64check encoded data\n */\nexport function encodeBase64Check (input) {\n const buffer = Buffer.from(input)\n const checksum = checkSumFn(input)\n const payloadWithChecksum = Buffer.concat([buffer, checksum], buffer.length + 4)\n return payloadWithChecksum.toString('base64')\n}\n\nexport function checkSumFn (payload) {\n return sha256hash(sha256hash(payload)).slice(0, 4)\n}\n\nfunction decodeRaw (buffer) {\n const payload = buffer.slice(0, -4)\n const checksum = buffer.slice(-4)\n const newChecksum = checkSumFn(payload)\n\n if (!checksum.equals(newChecksum)) return\n\n return payload\n}\n\n/**\n * Base64check decode given `str`\n * @rtype (str: String) => Buffer\n * @param {String} str - Data to decode\n * @return {Buffer} Base64check decoded data\n */\nexport function decodeBase64Check (str) {\n const buffer = Buffer.from(str, 'base64')\n const payload = decodeRaw(buffer)\n if (!payload) throw new Error('Invalid checksum')\n return Buffer.from(payload)\n}\n\n/**\n * Base58 encode given `input`\n * @rtype (input: String) => String\n * @param {String|Buffer} input - Data to encode\n * @return {String} Base58 encoded data\n */\nexport function encodeBase58Check (input) {\n return bs58check.encode(Buffer.from(input))\n}\n\n/**\n * Base58 decode given `str`\n * @rtype (str: String) => Buffer\n * @param {String} str - Data to decode\n * @return {Buffer} Base58 decoded data\n */\nexport function decodeBase58Check (str) {\n return bs58check.decode(str)\n}\n\n/**\n * Converts a positive integer to the smallest possible\n * representation in a binary digit representation\n * @rtype (value: Number) => Buffer\n * @param {Number} value - Value to encode\n * @return {Buffer} - Encoded data\n */\nexport function encodeUnsigned (value) {\n const binary = Buffer.allocUnsafe(4)\n binary.writeUInt32BE(value)\n return binary.slice(binary.findIndex(i => i !== 0))\n}\n\n// Todo Duplicated in tx builder. remove\n/**\n * Compute contract address\n * @rtype (owner: String, nonce: Number) => String\n * @param {String} owner - Address of contract owner\n * @param {Number} nonce - Round when contract was created\n * @return {String} - Contract address\n */\nexport function encodeContractAddress (owner, nonce) {\n const publicKey = decode(owner, 'ak')\n const binary = Buffer.concat([publicKey, encodeUnsigned(nonce)])\n return `ct_${encodeBase58Check(hash(binary))}`\n}\n\n// KEY-PAIR HELPERS\n\n/**\n * Generate keyPair from secret key\n * @rtype (secret: Uint8Array) => KeyPair\n * @param {Uint8Array} secret - secret key\n * @return {Object} - Object with Private(privateKey) and Public(publicKey) keys\n */\nexport function generateKeyPairFromSecret (secret) {\n return nacl.sign.keyPair.fromSecretKey(secret)\n}\n\n/**\n * Generate a random ED25519 keypair\n * @rtype (raw: Boolean) => {publicKey: String, secretKey: String} | {publicKey: Buffer, secretKey: Buffer}\n * @param {Boolean} raw - Whether to return raw (binary) keys\n * @return {Object} Key pair\n */\nexport function generateKeyPair (raw = false) {\n // <node>/apps/aens/test/aens_test_utils.erl\n const keyPair = nacl.sign.keyPair()\n\n const publicBuffer = Buffer.from(keyPair.publicKey)\n const secretBuffer = Buffer.from(keyPair.secretKey)\n\n if (raw) {\n return {\n publicKey: publicBuffer,\n secretKey: secretBuffer\n }\n } else {\n return {\n publicKey: `ak_${encodeBase58Check(publicBuffer)}`,\n secretKey: secretBuffer.toString('hex')\n }\n }\n}\n\n/**\n * Encrypt given data using `password`\n * @rtype (password: String, binaryData: Buffer) => Uint8Array\n * @param {String} password - Password to encrypt with\n * @param {Buffer} binaryData - Data to encrypt\n * @return {Uint8Array} Encrypted data\n */\nexport function encryptKey (password, binaryData) {\n const hashedPasswordBytes = sha256hash(password)\n const aesEcb = new Ecb(hashedPasswordBytes)\n return aesEcb.encrypt(binaryData)\n}\n\n/**\n * Decrypt given data using `password`\n * @rtype (password: String, encrypted: String) => Uint8Array\n * @param {String} password - Password to decrypt with\n * @param {String} encrypted - Data to decrypt\n * @return {Buffer} Decrypted data\n */\nexport function decryptKey (password, encrypted) {\n const encryptedBytes = Buffer.from(encrypted)\n const hashedPasswordBytes = sha256hash(password)\n const aesEcb = new Ecb(hashedPasswordBytes)\n return Buffer.from(aesEcb.decrypt(encryptedBytes))\n}\n\n// SIGNATURES\n\n/**\n * Generate signature\n * @rtype (data: String|Buffer, privateKey: Buffer) => Buffer\n * @param {String|Buffer} data - Data to sign\n * @param {String|Buffer} privateKey - Key to sign with\n * @return {Buffer|Uint8Array} Signature\n */\nexport function sign (data, privateKey) {\n return nacl.sign.detached(Buffer.from(data), Buffer.from(privateKey))\n}\n\n/**\n * Verify that signature was signed by public key\n * @rtype (str: String, signature: Buffer, publicKey: Buffer) => Boolean\n * @param {String|Buffer} str - Data to verify\n * @param {Buffer} signature - Signature to verify\n * @param {Buffer} publicKey - Key to verify against\n * @return {Boolean} Valid?\n */\nexport function verify (str, signature, publicKey) {\n return nacl.sign.detached.verify(new Uint8Array(str), signature, publicKey)\n}\n\nexport function messageToHash (message) {\n const p = Buffer.from('aeternity Signed Message:\\n', 'utf8')\n const msg = Buffer.from(message, 'utf8')\n if (msg.length >= 0xFD) throw new Error('message too long')\n return hash(Buffer.concat([Buffer.from([p.length]), p, Buffer.from([msg.length]), msg]))\n}\n\nexport function signMessage (message, privateKey) {\n return sign(messageToHash(message), privateKey)\n}\n\nexport function verifyMessage (str, signature, publicKey) {\n return verify(messageToHash(str), signature, publicKey)\n}\n\n/**\n * æternity readable public keys are the base58-encoded public key, prepended\n * with 'ak_'\n * @rtype (binaryKey: Buffer) => String\n * @param {Buffer} binaryKey - Key to encode\n * @return {String} Encoded key\n */\nexport function aeEncodeKey (binaryKey) {\n const publicKeyBuffer = Buffer.from(binaryKey, 'hex')\n const pubKeyAddress = encodeBase58Check(publicKeyBuffer)\n return `ak_${pubKeyAddress}`\n}\n\n/**\n * Check key pair for validity\n *\n * Sign a message, and then verifying that signature\n * @rtype (privateKey: Buffer, publicKey: Buffer) => Boolean\n * @param {Buffer} privateKey - Private key to verify\n * @param {Buffer} publicKey - Public key to verify\n * @return {Boolean} Valid?\n */\nexport function isValidKeypair (privateKey, publicKey) {\n const message = Buffer.from('TheMessage')\n const signature = sign(message, privateKey)\n return verify(message, signature, publicKey)\n}\n\n/**\n * This function encrypts a message using base58check encoded and 'ak' prefixed\n * publicKey such that only the corresponding secretKey will\n * be able to decrypt\n * @rtype (msg: String, publicKey: String) => Object\n * @param {Buffer} msg - Data to encode\n * @param {String} publicKey - Public key\n * @return {Object}\n */\nexport function encryptData (msg, publicKey) {\n const ephemeralKeyPair = nacl.box.keyPair()\n const pubKeyUInt8Array = decode(publicKey, 'ak')\n const nonce = nacl.randomBytes(nacl.box.nonceLength)\n\n const encryptedMessage = nacl.box(\n Buffer.from(msg),\n nonce,\n ed2curve.convertPublicKey(pubKeyUInt8Array),\n ephemeralKeyPair.secretKey\n )\n\n return {\n ciphertext: Buffer.from(encryptedMessage).toString('hex'),\n ephemPubKey: Buffer.from(ephemeralKeyPair.publicKey).toString('hex'),\n nonce: Buffer.from(nonce).toString('hex'),\n version: 'x25519-xsalsa20-poly1305'\n }\n}\n\n/**\n * This function decrypt a message using secret key\n * @rtype (secretKey: String, encryptedData: Object) => Buffer|null\n * @param {String} secretKey - Secret key\n * @param {Object} encryptedData - Encrypted data\n * @return {Buffer|null}\n */\nexport function decryptData (secretKey, encryptedData) {\n const receiverSecretKeyUint8Array = ed2curve.convertSecretKey(Buffer.from(secretKey, 'hex'))\n const nonce = Buffer.from(encryptedData.nonce, 'hex')\n const ciphertext = Buffer.from(encryptedData.ciphertext, 'hex')\n const ephemPubKey = Buffer.from(encryptedData.ephemPubKey, 'hex')\n const decrypted = nacl.box.open(\n ciphertext,\n nonce,\n ephemPubKey,\n receiverSecretKeyUint8Array\n )\n return decrypted ? Buffer.from(decrypted) : decrypted\n}\n"],"file":"crypto.js"}
|
package/es/utils/hd-wallet.js
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
import _defineProperty from "@babel/runtime-corejs3/helpers/defineProperty";
|
|
2
|
-
|
|
3
|
-
function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) { symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
|
|
4
|
-
|
|
5
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { var _context4; _forEachInstanceProperty(_context4 = ownKeys(Object(source), true)).call(_context4, function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { var _context5; _forEachInstanceProperty(_context5 = ownKeys(Object(source))).call(_context5, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
|
6
|
-
|
|
7
|
-
import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each";
|
|
8
|
-
import _reduceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/reduce";
|
|
9
|
-
import _includesInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/includes";
|
|
10
|
-
import _sliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/slice";
|
|
11
|
-
import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
|
|
12
|
-
import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
|
|
13
|
-
import _fillInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/fill";
|
|
14
|
-
import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
|
|
15
|
-
import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
|
|
16
|
-
import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
|
|
17
|
-
import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
|
|
18
|
-
import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
|
|
19
|
-
import _Object$defineProperties from "@babel/runtime-corejs3/core-js-stable/object/define-properties";
|
|
20
|
-
import _Object$defineProperty from "@babel/runtime-corejs3/core-js-stable/object/define-property";
|
|
21
|
-
import nacl from 'tweetnacl';
|
|
22
|
-
import { full as hmac } from 'tweetnacl-auth';
|
|
23
|
-
import { fromString } from 'bip32-path';
|
|
24
|
-
import { validateMnemonic, mnemonicToSeed, generateMnemonic as genMnemonic } from '@aeternity/bip39';
|
|
25
|
-
import { decryptKey, encodeBase58Check, encryptKey } from './crypto';
|
|
26
|
-
var ED25519_CURVE = Buffer.from('ed25519 seed');
|
|
27
|
-
var HARDENED_OFFSET = 0x80000000;
|
|
28
|
-
|
|
29
|
-
var toHex = function toHex(buffer) {
|
|
30
|
-
return Buffer.from(buffer).toString('hex');
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export function derivePathFromKey(path, key) {
|
|
34
|
-
var segments = path === '' ? [] : fromString(path).toPathArray();
|
|
35
|
-
|
|
36
|
-
_forEachInstanceProperty(segments).call(segments, function (segment, i) {
|
|
37
|
-
if (segment < HARDENED_OFFSET) {
|
|
38
|
-
throw new Error("Segment #".concat(i + 1, " is not hardened"));
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
return _reduceInstanceProperty(segments).call(segments, function (parentKey, segment) {
|
|
43
|
-
return deriveChild(parentKey, segment);
|
|
44
|
-
}, key);
|
|
45
|
-
}
|
|
46
|
-
export function derivePathFromSeed(path, seed) {
|
|
47
|
-
var _context;
|
|
48
|
-
|
|
49
|
-
if (!_includesInstanceProperty(_context = ['m', 'm/']).call(_context, _sliceInstanceProperty(path).call(path, 0, 2))) {
|
|
50
|
-
throw new Error('Invalid path');
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
var masterKey = getMasterKeyFromSeed(seed);
|
|
54
|
-
return derivePathFromKey(_sliceInstanceProperty(path).call(path, 2), masterKey);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function formatAccount(keys) {
|
|
58
|
-
var secretKey = keys.secretKey,
|
|
59
|
-
publicKey = keys.publicKey;
|
|
60
|
-
return {
|
|
61
|
-
secretKey: toHex(secretKey),
|
|
62
|
-
publicKey: "ak_".concat(encodeBase58Check(publicKey))
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function getKeyPair(secretKey) {
|
|
67
|
-
return nacl.sign.keyPair.fromSeed(secretKey);
|
|
68
|
-
}
|
|
69
|
-
export function generateMnemonic() {
|
|
70
|
-
return genMnemonic();
|
|
71
|
-
}
|
|
72
|
-
export function getMasterKeyFromSeed(seed) {
|
|
73
|
-
var I = hmac(seed, ED25519_CURVE);
|
|
74
|
-
|
|
75
|
-
var IL = _sliceInstanceProperty(I).call(I, 0, 32);
|
|
76
|
-
|
|
77
|
-
var IR = _sliceInstanceProperty(I).call(I, 32);
|
|
78
|
-
|
|
79
|
-
return {
|
|
80
|
-
secretKey: IL,
|
|
81
|
-
chainCode: IR
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
export function deriveChild(_ref, index) {
|
|
85
|
-
var secretKey = _ref.secretKey,
|
|
86
|
-
chainCode = _ref.chainCode;
|
|
87
|
-
|
|
88
|
-
if (index < HARDENED_OFFSET) {
|
|
89
|
-
throw new Error("Child index #".concat(index, " is not supported"));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
var indexBuffer = Buffer.allocUnsafe(4);
|
|
93
|
-
indexBuffer.writeUInt32BE(index, 0);
|
|
94
|
-
|
|
95
|
-
var data = _concatInstanceProperty(Buffer).call(Buffer, [Buffer.alloc(1, 0), Buffer.from(secretKey), Buffer.from(indexBuffer)]);
|
|
96
|
-
|
|
97
|
-
var I = hmac(data, chainCode);
|
|
98
|
-
|
|
99
|
-
var IL = _sliceInstanceProperty(I).call(I, 0, 32);
|
|
100
|
-
|
|
101
|
-
var IR = _sliceInstanceProperty(I).call(I, 32);
|
|
102
|
-
|
|
103
|
-
return {
|
|
104
|
-
secretKey: IL,
|
|
105
|
-
chainCode: IR
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
export function generateSaveHDWallet(mnemonic, password) {
|
|
109
|
-
if (!validateMnemonic(mnemonic)) {
|
|
110
|
-
throw new Error('Invalid mnemonic');
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
var seed = mnemonicToSeed(mnemonic);
|
|
114
|
-
var walletKey = derivePathFromSeed('m/44h/457h', seed);
|
|
115
|
-
return {
|
|
116
|
-
secretKey: toHex(encryptKey(password, walletKey.secretKey)),
|
|
117
|
-
chainCode: toHex(encryptKey(password, walletKey.chainCode))
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
export function getSaveHDWalletAccounts(saveHDWallet, password, accountCount) {
|
|
121
|
-
var _context2, _context3;
|
|
122
|
-
|
|
123
|
-
var walletKey = {
|
|
124
|
-
secretKey: decryptKey(password, Buffer.from(saveHDWallet.secretKey, 'hex')),
|
|
125
|
-
chainCode: decryptKey(password, Buffer.from(saveHDWallet.chainCode, 'hex'))
|
|
126
|
-
};
|
|
127
|
-
return _mapInstanceProperty(_context2 = _fillInstanceProperty(_context3 = new Array(accountCount)).call(_context3)).call(_context2, function (_, idx) {
|
|
128
|
-
return formatAccount(getKeyPair(derivePathFromKey("".concat(idx, "h/0h/0h"), walletKey).secretKey));
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
export var getHdWalletAccountFromMnemonic = function getHdWalletAccountFromMnemonic(mnemonic, accountIdx) {
|
|
132
|
-
var seed = mnemonicToSeed(mnemonic);
|
|
133
|
-
var walletKey = derivePathFromSeed('m/44h/457h', seed);
|
|
134
|
-
var derived = derivePathFromKey("".concat(accountIdx, "h/0h/0h"), walletKey);
|
|
135
|
-
var keyPair = getKeyPair(derived.secretKey);
|
|
136
|
-
return _objectSpread(_objectSpread({}, formatAccount(keyPair)), {}, {
|
|
137
|
-
idx: accountIdx
|
|
138
|
-
});
|
|
139
|
-
};
|
|
140
|
-
export default {
|
|
141
|
-
getHdWalletAccountFromMnemonic: getHdWalletAccountFromMnemonic,
|
|
142
|
-
getSaveHDWalletAccounts: getSaveHDWalletAccounts,
|
|
143
|
-
generateSaveHDWallet: generateSaveHDWallet,
|
|
144
|
-
generateMnemonic: generateMnemonic,
|
|
145
|
-
deriveChild: deriveChild,
|
|
146
|
-
getMasterKeyFromSeed: getMasterKeyFromSeed
|
|
147
|
-
};
|
|
148
|
-
//# sourceMappingURL=hd-wallet.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/hd-wallet.js"],"names":["nacl","full","hmac","fromString","validateMnemonic","mnemonicToSeed","generateMnemonic","genMnemonic","decryptKey","encodeBase58Check","encryptKey","ED25519_CURVE","Buffer","from","HARDENED_OFFSET","toHex","buffer","toString","derivePathFromKey","path","key","segments","toPathArray","segment","i","Error","parentKey","deriveChild","derivePathFromSeed","seed","masterKey","getMasterKeyFromSeed","formatAccount","keys","secretKey","publicKey","getKeyPair","sign","keyPair","fromSeed","I","IL","IR","chainCode","index","indexBuffer","allocUnsafe","writeUInt32BE","data","alloc","generateSaveHDWallet","mnemonic","password","walletKey","getSaveHDWalletAccounts","saveHDWallet","accountCount","Array","_","idx","getHdWalletAccountFromMnemonic","accountIdx","derived"],"mappings":";;;;;;;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AAA,gBAAgB,EAAhBA,gBAJa;AAKbqB,EAAAA,WAAW,EAAXA,WALa;AAMbI,EAAAA,oBAAoB,EAApBA;AANa,CAAf","sourcesContent":["import nacl from 'tweetnacl'\nimport { full as hmac } from 'tweetnacl-auth'\nimport { fromString } from 'bip32-path'\nimport { validateMnemonic, mnemonicToSeed, generateMnemonic as genMnemonic } from '@aeternity/bip39'\nimport { decryptKey, encodeBase58Check, encryptKey } from './crypto'\n\nconst ED25519_CURVE = Buffer.from('ed25519 seed')\nconst HARDENED_OFFSET = 0x80000000\n\nconst toHex = (buffer) => Buffer.from(buffer).toString('hex')\n\nexport function derivePathFromKey (path, key) {\n const segments = path === '' ? [] : fromString(path).toPathArray()\n segments.forEach((segment, i) => {\n if (segment < HARDENED_OFFSET) {\n throw new Error(`Segment #${i + 1} is not hardened`)\n }\n })\n\n return segments.reduce((parentKey, segment) => deriveChild(parentKey, segment), key)\n}\n\nexport function derivePathFromSeed (path, seed) {\n if (!['m', 'm/'].includes(path.slice(0, 2))) {\n throw new Error('Invalid path')\n }\n const masterKey = getMasterKeyFromSeed(seed)\n return derivePathFromKey(path.slice(2), masterKey)\n}\n\nfunction formatAccount (keys) {\n const { secretKey, publicKey } = keys\n return {\n secretKey: toHex(secretKey),\n publicKey: `ak_${encodeBase58Check(publicKey)}`\n }\n}\n\nexport function getKeyPair (secretKey) {\n return nacl.sign.keyPair.fromSeed(secretKey)\n}\n\nexport function generateMnemonic () {\n return genMnemonic()\n}\n\nexport function getMasterKeyFromSeed (seed) {\n const I = hmac(seed, ED25519_CURVE)\n const IL = I.slice(0, 32)\n const IR = I.slice(32)\n return {\n secretKey: IL,\n chainCode: IR\n }\n}\n\nexport function deriveChild ({ secretKey, chainCode }, index) {\n if (index < HARDENED_OFFSET) {\n throw new Error(`Child index #${index} is not supported`)\n }\n const indexBuffer = Buffer.allocUnsafe(4)\n indexBuffer.writeUInt32BE(index, 0)\n\n const data = Buffer.concat([Buffer.alloc(1, 0), Buffer.from(secretKey), Buffer.from(indexBuffer)])\n\n const I = hmac(data, chainCode)\n const IL = I.slice(0, 32)\n const IR = I.slice(32)\n return {\n secretKey: IL,\n chainCode: IR\n }\n}\n\nexport function generateSaveHDWallet (mnemonic, password) {\n if (!validateMnemonic(mnemonic)) {\n throw new Error('Invalid mnemonic')\n }\n const seed = mnemonicToSeed(mnemonic)\n const walletKey = derivePathFromSeed('m/44h/457h', seed)\n return {\n secretKey: toHex(encryptKey(password, walletKey.secretKey)),\n chainCode: toHex(encryptKey(password, walletKey.chainCode))\n }\n}\n\nexport function getSaveHDWalletAccounts (saveHDWallet, password, accountCount) {\n const walletKey = {\n secretKey: decryptKey(password, Buffer.from(saveHDWallet.secretKey, 'hex')),\n chainCode: decryptKey(password, Buffer.from(saveHDWallet.chainCode, 'hex'))\n }\n return (new Array(accountCount)).fill()\n .map((_, idx) =>\n formatAccount(getKeyPair(derivePathFromKey(`${idx}h/0h/0h`, walletKey).secretKey)))\n}\n\nexport const getHdWalletAccountFromMnemonic = (mnemonic, accountIdx) => {\n const seed = mnemonicToSeed(mnemonic)\n const walletKey = derivePathFromSeed('m/44h/457h', seed)\n const derived = derivePathFromKey(`${accountIdx}h/0h/0h`, walletKey)\n const keyPair = getKeyPair(derived.secretKey)\n return {\n ...formatAccount(keyPair),\n idx: accountIdx\n }\n}\n\nexport default {\n getHdWalletAccountFromMnemonic,\n getSaveHDWalletAccounts,\n generateSaveHDWallet,\n generateMnemonic,\n deriveChild,\n getMasterKeyFromSeed\n}\n"],"file":"hd-wallet.js"}
|