@aeternity/aepp-sdk 10.0.0 → 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 -27
- 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.js → base.mjs} +20 -10
- package/es/account/base.mjs.map +1 -0
- package/es/account/{memory.js → memory.mjs} +29 -14
- package/es/account/memory.mjs.map +1 -0
- package/es/account/{multiple.js → multiple.mjs} +10 -9
- package/es/account/multiple.mjs.map +1 -0
- package/es/ae/{aens.js → aens.mjs} +18 -22
- 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.js → index.mjs} +8 -7
- package/es/ae/index.mjs.map +1 -0
- package/es/ae/{oracle.js → oracle.mjs} +33 -16
- 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} +20 -4
- package/es/chain/index.mjs.map +1 -0
- package/es/chain/{node.js → node.mjs} +121 -73
- package/es/chain/node.mjs.map +1 -0
- package/es/channel/{handlers.js → handlers.mjs} +18 -20
- package/es/channel/handlers.mjs.map +1 -0
- package/es/channel/{index.js → index.mjs} +7 -7
- package/es/channel/index.mjs.map +1 -0
- package/es/channel/{internal.js → internal.mjs} +49 -32
- 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.js → index.mjs} +36 -28
- 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.js → index.mjs} +11 -10
- package/es/node-pool/index.mjs.map +1 -0
- package/es/{node.js → node.mjs} +9 -8
- package/es/node.mjs.map +1 -0
- package/es/tx/builder/field-types.mjs +74 -0
- package/es/tx/builder/field-types.mjs.map +1 -0
- package/es/tx/builder/{helpers.js → helpers.mjs} +114 -56
- package/es/tx/builder/helpers.mjs.map +1 -0
- package/es/tx/builder/{index.js → index.mjs} +79 -68
- package/es/tx/builder/index.mjs.map +1 -0
- package/es/tx/builder/{schema.js → schema.mjs} +11 -65
- package/es/tx/builder/schema.mjs.map +1 -0
- package/es/tx/{index.js → index.mjs} +5 -2
- package/es/tx/index.mjs.map +1 -0
- package/es/tx/{tx-object.js → tx-object.mjs} +22 -15
- package/es/tx/tx-object.mjs.map +1 -0
- package/es/tx/{tx.js → tx.mjs} +15 -14
- package/es/tx/tx.mjs.map +1 -0
- package/es/tx/{validator.js → validator.mjs} +72 -33
- 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} +7 -6
- package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/connection/{browser-window-message.js → browser-window-message.mjs} +10 -11
- 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} +5 -2
- 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} +6 -5
- package/es/utils/aepp-wallet-communication/content-script-bridge.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/{helpers.js → helpers.mjs} +3 -14
- package/es/utils/aepp-wallet-communication/helpers.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/{aepp-rpc.js → aepp-rpc.mjs} +113 -67
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/{rpc-client.js → rpc-client.mjs} +14 -9
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/{wallet-rpc.js → wallet-rpc.mjs} +71 -63
- 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.js → wallet-detector.mjs} +6 -5
- package/es/utils/aepp-wallet-communication/wallet-detector.mjs.map +1 -0
- package/es/utils/{amount-formatter.js → amount-formatter.mjs} +14 -7
- package/es/utils/amount-formatter.mjs.map +1 -0
- package/es/utils/{async-init.js → async-init.mjs} +8 -11
- package/es/utils/async-init.mjs.map +1 -0
- package/es/utils/{bignumber.js → bignumber.mjs} +2 -9
- package/es/utils/bignumber.mjs.map +1 -0
- package/es/utils/{bytes.js → bytes.mjs} +16 -8
- 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.js → crypto.mjs} +40 -105
- 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.js → hd-wallet.mjs} +33 -34
- package/es/utils/hd-wallet.mjs.map +1 -0
- package/es/utils/{json-big.js → json-big.mjs} +1 -1
- package/es/utils/{json-big.js.map → json-big.mjs.map} +1 -1
- package/es/utils/{keystore.js → keystore.mjs} +63 -51
- package/es/utils/keystore.mjs.map +1 -0
- package/es/utils/{mptree.js → mptree.mjs} +21 -12
- package/es/utils/mptree.mjs.map +1 -0
- package/es/utils/{other.js → other.mjs} +2 -2
- package/es/utils/other.mjs.map +1 -0
- package/es/utils/{semver-satisfies.js → semver-satisfies.mjs} +1 -1
- package/es/utils/{semver-satisfies.js.map → semver-satisfies.mjs.map} +1 -1
- package/es/utils/{string.js → string.mjs} +1 -1
- package/es/utils/{string.js.map → string.mjs.map} +1 -1
- package/es/utils/{swagger.js → swagger.mjs} +46 -14
- package/es/utils/swagger.mjs.map +1 -0
- package/package.json +46 -55
- package/src/account/memory.js +13 -8
- package/src/account/multiple.js +12 -4
- package/src/ae/aens.js +12 -17
- package/src/ae/aepp.js +2 -2
- package/src/ae/contract.js +15 -152
- package/src/ae/index.js +2 -1
- package/src/ae/oracle.js +25 -10
- package/src/ae/universal.js +2 -2
- package/src/ae/wallet.js +2 -2
- package/src/chain/index.js +21 -2
- package/src/chain/node.js +80 -53
- package/src/channel/handlers.js +20 -17
- package/src/channel/internal.js +30 -24
- package/src/contract/{aci/index.js → aci.js} +152 -93
- package/src/contract/compiler.js +22 -117
- package/src/contract/ga/helpers.js +7 -5
- package/src/contract/ga/index.js +27 -24
- package/src/index.js +7 -8
- package/src/node-pool/index.js +9 -8
- package/src/node.js +5 -7
- package/src/tx/builder/field-types.js +61 -0
- package/src/tx/builder/helpers.js +113 -58
- package/src/tx/builder/index.js +39 -34
- package/src/tx/builder/schema.js +11 -25
- package/src/tx/tx-object.js +10 -9
- package/src/tx/tx.js +17 -8
- package/src/tx/validator.js +25 -5
- package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +8 -3
- package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +10 -6
- package/src/utils/aepp-wallet-communication/content-script-bridge.js +5 -4
- package/src/utils/aepp-wallet-communication/helpers.js +2 -12
- package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +81 -55
- package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +15 -6
- package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +60 -49
- package/src/utils/aepp-wallet-communication/schema.ts +80 -0
- package/src/utils/aepp-wallet-communication/wallet-detector.js +3 -2
- package/src/utils/amount-formatter.ts +4 -3
- package/src/utils/async-init.js +10 -6
- package/src/utils/bignumber.ts +1 -9
- package/src/utils/bytes.ts +3 -2
- package/src/utils/crypto-ts.ts +2 -2
- package/src/utils/crypto.js +9 -92
- package/src/utils/errors.ts +683 -0
- package/src/utils/hd-wallet.js +11 -26
- package/src/utils/keystore.js +47 -51
- package/src/utils/mptree.ts +12 -6
- package/src/utils/swagger.js +67 -44
- package/CHANGELOG.md +0 -1603
- package/es/account/base.js.map +0 -1
- package/es/account/memory.js.map +0 -1
- package/es/account/multiple.js.map +0 -1
- package/es/ae/aens.js.map +0 -1
- package/es/ae/aepp.js.map +0 -1
- package/es/ae/contract.js +0 -312
- package/es/ae/contract.js.map +0 -1
- package/es/ae/index.js.map +0 -1
- 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.map +0 -1
- package/es/channel/handlers.js.map +0 -1
- package/es/channel/index.js.map +0 -1
- 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 -361
- package/es/contract/aci/index.js.map +0 -1
- package/es/contract/aci/transformation.js +0 -66
- package/es/contract/aci/transformation.js.map +0 -1
- package/es/contract/compiler.js +0 -244
- package/es/contract/compiler.js.map +0 -1
- package/es/contract/ga/helpers.js +0 -22
- package/es/contract/ga/helpers.js.map +0 -1
- package/es/contract/ga/index.js.map +0 -1
- package/es/contract/index.js +0 -162
- 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.map +0 -1
- package/es/node.js.map +0 -1
- package/es/tx/builder/helpers.js.map +0 -1
- package/es/tx/builder/index.js.map +0 -1
- package/es/tx/builder/schema.js.map +0 -1
- package/es/tx/index.js.map +0 -1
- package/es/tx/tx-object.js.map +0 -1
- package/es/tx/tx.js.map +0 -1
- 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.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.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +0 -1
- package/es/utils/aepp-wallet-communication/schema.js +0 -107
- package/es/utils/aepp-wallet-communication/schema.js.map +0 -1
- package/es/utils/aepp-wallet-communication/wallet-detector.js.map +0 -1
- package/es/utils/amount-formatter.js.map +0 -1
- package/es/utils/async-init.js.map +0 -1
- 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.map +0 -1
- package/es/utils/hd-wallet.js.map +0 -1
- package/es/utils/keystore.js.map +0 -1
- package/es/utils/mptree.js.map +0 -1
- package/es/utils/other.js.map +0 -1
- package/es/utils/swagger.js.map +0 -1
- package/src/contract/aci/transformation.js +0 -72
- package/src/contract/index.js +0 -163
- package/src/utils/aepp-wallet-communication/schema.js +0 -108
|
@@ -1,16 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Big Number Helpers
|
|
3
3
|
* @module @aeternity/aepp-sdk/es/utils/bignumber
|
|
4
|
-
* @example import {
|
|
4
|
+
* @example import { isBigNumber, ceil } from '@aeternity/aepp-sdk/es/utils/bignumber'
|
|
5
5
|
*/
|
|
6
6
|
import BigNumber from 'bignumber.js';
|
|
7
|
-
/**
|
|
8
|
-
* Convert number to string
|
|
9
|
-
* @param {String|Number|BigNumber} number number to convert
|
|
10
|
-
* @return {String}
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
export const parseBigNumber = number => new BigNumber(number.toString()).toString(10);
|
|
14
7
|
/**
|
|
15
8
|
* Check if value is BigNumber, Number or number string representation
|
|
16
9
|
* @param {String|Number|BigNumber} number number to convert
|
|
@@ -25,4 +18,4 @@ export const isBigNumber = number => ['number', 'object', 'string', 'bigint'].in
|
|
|
25
18
|
*/
|
|
26
19
|
|
|
27
20
|
export const ceil = bigNumber => bigNumber.integerValue(BigNumber.ROUND_CEIL);
|
|
28
|
-
//# sourceMappingURL=bignumber.
|
|
21
|
+
//# sourceMappingURL=bignumber.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/bignumber.ts"],"names":["BigNumber","isBigNumber","number","includes","isNaN","Number","isInteger","ceil","bigNumber","integerValue","ROUND_CEIL"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA,OAAOA,SAAP,MAAsB,cAAtB;AAEA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,WAAW,GAAIC,MAAD,IACzB,CAAC,QAAD,EAAW,QAAX,EAAqB,QAArB,EAA+B,QAA/B,EAAyCC,QAAzC,CAAkD,OAAOD,MAAzD,MACC,CAACE,KAAK,CAACF,MAAD,CAAN,IAA4BG,MAAM,CAACC,SAAP,CAAiBJ,MAAjB,CAA5B,IAAwDF,SAAS,CAACC,WAAV,CAAsBC,MAAtB,CADzD,CADK;AAIP;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMK,IAAI,GAAIC,SAAD,IAAqCA,SAAS,CAACC,YAAV,CAAuBT,SAAS,CAACU,UAAjC,CAAlD","sourcesContent":["/**\n * Big Number Helpers\n * @module @aeternity/aepp-sdk/es/utils/bignumber\n * @example import { isBigNumber, ceil } from '@aeternity/aepp-sdk/es/utils/bignumber'\n */\nimport BigNumber from 'bignumber.js'\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 ['number', 'object', 'string', 'bigint'].includes(typeof number) &&\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.mjs"}
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import _buffer from "buffer";
|
|
2
|
+
const {
|
|
3
|
+
Buffer: _Buffer
|
|
4
|
+
} = _buffer;
|
|
5
|
+
|
|
1
6
|
/*
|
|
2
7
|
* ISC License (ISC)
|
|
3
8
|
* Copyright 2018 aeternity developers
|
|
@@ -15,7 +20,8 @@
|
|
|
15
20
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
16
21
|
*/
|
|
17
22
|
import BigNumber from 'bignumber.js';
|
|
18
|
-
import {
|
|
23
|
+
import { NoSerializerFoundError, TypeError } from "./errors.mjs";
|
|
24
|
+
import { isBase64, isHex } from "./string.mjs";
|
|
19
25
|
/**
|
|
20
26
|
* Bytes module
|
|
21
27
|
* @module @aeternity/aepp-sdk/es/utils/bytes
|
|
@@ -29,10 +35,10 @@ import { isBase64, isHex } from './string';
|
|
|
29
35
|
*/
|
|
30
36
|
|
|
31
37
|
export function bigNumberToByteArray(x) {
|
|
32
|
-
if (!x.isInteger()) throw new
|
|
38
|
+
if (!x.isInteger()) throw new TypeError(`Unexpected not integer value: ${x.toFixed()}`);
|
|
33
39
|
let hexString = x.toString(16);
|
|
34
40
|
if (hexString.length % 2 === 1) hexString = '0' + hexString;
|
|
35
|
-
return
|
|
41
|
+
return _Buffer.from(hexString, 'hex');
|
|
36
42
|
}
|
|
37
43
|
/**
|
|
38
44
|
* Convert string, number, or BigNumber to byte array
|
|
@@ -46,7 +52,7 @@ export function toBytes(val) {
|
|
|
46
52
|
// Encode a value to bytes.
|
|
47
53
|
// If the value is an int it will be encoded as bytes big endian
|
|
48
54
|
// Raises ValueError if the input is not an int or string
|
|
49
|
-
if (val === undefined || val === null) return
|
|
55
|
+
if (val === undefined || val === null) return _Buffer.from([]);
|
|
50
56
|
|
|
51
57
|
if (Number.isInteger(val) || BigNumber.isBigNumber(val) || big) {
|
|
52
58
|
if (!BigNumber.isBigNumber(val)) val = new BigNumber(val);
|
|
@@ -54,10 +60,10 @@ export function toBytes(val) {
|
|
|
54
60
|
}
|
|
55
61
|
|
|
56
62
|
if (typeof val === 'string') {
|
|
57
|
-
return
|
|
63
|
+
return _Buffer.from(val);
|
|
58
64
|
}
|
|
59
65
|
|
|
60
|
-
throw new
|
|
66
|
+
throw new NoSerializerFoundError();
|
|
61
67
|
}
|
|
62
68
|
/**
|
|
63
69
|
* Convert a string to a Buffer. If encoding is not specified, hex-encoding
|
|
@@ -69,6 +75,8 @@ export function toBytes(val) {
|
|
|
69
75
|
*/
|
|
70
76
|
|
|
71
77
|
export function str2buf(str, enc) {
|
|
72
|
-
|
|
78
|
+
var _ref;
|
|
79
|
+
|
|
80
|
+
return _Buffer.from(str, (_ref = enc !== null && enc !== void 0 ? enc : isHex(str) ? 'hex' : undefined) !== null && _ref !== void 0 ? _ref : isBase64(str) ? 'base64' : undefined);
|
|
73
81
|
}
|
|
74
|
-
//# sourceMappingURL=bytes.
|
|
82
|
+
//# sourceMappingURL=bytes.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/bytes.ts"],"names":["BigNumber","NoSerializerFoundError","TypeError","isBase64","isHex","bigNumberToByteArray","x","isInteger","toFixed","hexString","toString","length","from","toBytes","val","big","undefined","Number","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;SACSC,sB,EAAwBC,S;SACxBC,Q,EAAUC,K;AAEnB;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,oBAAT,CAA+BC,CAA/B,EAAqD;AAC1D,MAAI,CAACA,CAAC,CAACC,SAAF,EAAL,EAAoB,MAAM,IAAIL,SAAJ,CAAe,iCAAgCI,CAAC,CAACE,OAAF,EAAY,EAA3D,CAAN;AACpB,MAAIC,SAAS,GAAGH,CAAC,CAACI,QAAF,CAAW,EAAX,CAAhB;AACA,MAAID,SAAS,CAACE,MAAV,GAAmB,CAAnB,KAAyB,CAA7B,EAAgCF,SAAS,GAAG,MAAMA,SAAlB;AAChC,SAAO,QAAOG,IAAP,CAAYH,SAAZ,EAAuB,KAAvB,CAAP;AACD;AAED;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASI,OAAT,CAAkBC,GAAlB,EAA0F;AAAA,MAA9BC,GAA8B,uEAAf,KAAe;AAC/F;AACA;AACA;AAEA,MAAID,GAAG,KAAKE,SAAR,IAAqBF,GAAG,KAAK,IAAjC,EAAuC,OAAO,QAAOF,IAAP,CAAY,EAAZ,CAAP;;AACvC,MAAIK,MAAM,CAACV,SAAP,CAAiBO,GAAjB,KAAyBd,SAAS,CAACkB,WAAV,CAAsBJ,GAAtB,CAAzB,IAAuDC,GAA3D,EAAgE;AAC9D,QAAI,CAACf,SAAS,CAACkB,WAAV,CAAsBJ,GAAtB,CAAL,EAAiCA,GAAG,GAAG,IAAId,SAAJ,CAAcc,GAAd,CAAN;AACjC,WAAOT,oBAAoB,CAACS,GAAD,CAA3B;AACD;;AACD,MAAI,OAAOA,GAAP,KAAe,QAAnB,EAA6B;AAC3B,WAAO,QAAOF,IAAP,CAAYE,GAAZ,CAAP;AACD;;AACD,QAAM,IAAIb,sBAAJ,EAAN;AACD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASkB,OAAT,CAAkBC,GAAlB,EAA+BC,GAA/B,EAA6D;AAAA;;AAClE,SAAO,QAAOT,IAAP,CACLQ,GADK,UAELC,GAFK,aAELA,GAFK,cAELA,GAFK,GAEGjB,KAAK,CAACgB,GAAD,CAAL,GAAa,KAAb,GAAqBJ,SAFxB,uCAEuCb,QAAQ,CAACiB,GAAD,CAAR,GAAgB,QAAhB,GAA2BJ,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 { NoSerializerFoundError, TypeError } from './errors'\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 TypeError(`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 NoSerializerFoundError()\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.mjs"}
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import _buffer from "buffer";
|
|
2
|
+
const {
|
|
3
|
+
Buffer: _Buffer
|
|
4
|
+
} = _buffer;
|
|
5
|
+
|
|
1
6
|
/*
|
|
2
7
|
* ISC License (ISC)
|
|
3
8
|
* Copyright 2021 aeternity developers
|
|
@@ -14,7 +19,10 @@
|
|
|
14
19
|
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
20
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
16
21
|
*/
|
|
17
|
-
import
|
|
22
|
+
import _aeternityBlakejs from '@aeternity/blakejs';
|
|
23
|
+
const {
|
|
24
|
+
blake2b
|
|
25
|
+
} = _aeternityBlakejs;
|
|
18
26
|
|
|
19
27
|
/**
|
|
20
28
|
* Calculate 256bits Blake2b hash of `input`
|
|
@@ -23,6 +31,6 @@ import { blake2b } from 'blakejs';
|
|
|
23
31
|
* @return {Buffer} Hash
|
|
24
32
|
*/
|
|
25
33
|
export function hash(input) {
|
|
26
|
-
return
|
|
34
|
+
return _Buffer.from(blake2b(input, undefined, 32)); // 256 bits
|
|
27
35
|
}
|
|
28
|
-
//# sourceMappingURL=crypto-ts.
|
|
36
|
+
//# sourceMappingURL=crypto-ts.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/crypto-ts.ts"],"names":["hash","input","from","blake2b","undefined"],"mappings":";;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAEA,8BAAwB,oBAAxB;;;;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,IAAT,CAAeC,KAAf,EAAoC;AACzC,SAAO,QAAOC,IAAP,CAAYC,OAAO,CAACF,KAAD,EAAQG,SAAR,EAAmB,EAAnB,CAAnB,CAAP,CADyC,CACS;AACnD","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 '@aeternity/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, undefined, 32)) // 256 bits\n}\n"],"file":"crypto-ts.mjs"}
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import _buffer from "buffer";
|
|
2
|
+
const {
|
|
3
|
+
Buffer: _Buffer
|
|
4
|
+
} = _buffer;
|
|
5
|
+
|
|
1
6
|
/*
|
|
2
7
|
* ISC License (ISC)
|
|
3
8
|
* Copyright 2018 aeternity developers
|
|
@@ -20,14 +25,14 @@
|
|
|
20
25
|
* @module @aeternity/aepp-sdk/es/utils/crypto
|
|
21
26
|
* @example import { Crypto } from '@aeternity/aepp-sdk'
|
|
22
27
|
*/
|
|
23
|
-
import bs58check from 'bs58check';
|
|
24
28
|
import nacl from 'tweetnacl';
|
|
25
29
|
import aesjs from 'aes-js';
|
|
26
30
|
import shajs from 'sha.js';
|
|
27
|
-
import { str2buf
|
|
28
|
-
import { decode } from
|
|
29
|
-
import { hash } from
|
|
30
|
-
|
|
31
|
+
import { str2buf } from "./bytes.mjs";
|
|
32
|
+
import { encode, decode } from "../tx/builder/helpers.mjs";
|
|
33
|
+
import { hash } from "./crypto-ts.mjs";
|
|
34
|
+
import { NotImplementedError } from "./errors.mjs";
|
|
35
|
+
export * from "./crypto-ts.mjs";
|
|
31
36
|
const Ecb = aesjs.ModeOfOperation.ecb;
|
|
32
37
|
/**
|
|
33
38
|
* Generate address from secret key
|
|
@@ -38,8 +43,10 @@ const Ecb = aesjs.ModeOfOperation.ecb;
|
|
|
38
43
|
|
|
39
44
|
export function getAddressFromPriv(secret) {
|
|
40
45
|
const keys = nacl.sign.keyPair.fromSecretKey(str2buf(secret));
|
|
41
|
-
|
|
42
|
-
|
|
46
|
+
|
|
47
|
+
const publicBuffer = _Buffer.from(keys.publicKey);
|
|
48
|
+
|
|
49
|
+
return encode(publicBuffer, 'ak');
|
|
43
50
|
}
|
|
44
51
|
/**
|
|
45
52
|
* Check if address is valid
|
|
@@ -51,24 +58,14 @@ export function getAddressFromPriv(secret) {
|
|
|
51
58
|
|
|
52
59
|
export function isAddressValid(address) {
|
|
53
60
|
let prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'ak';
|
|
54
|
-
if (typeof address !== 'string') return false;
|
|
55
61
|
|
|
56
62
|
try {
|
|
57
|
-
|
|
63
|
+
decode(address, prefix);
|
|
64
|
+
return true;
|
|
58
65
|
} catch (e) {
|
|
59
66
|
return false;
|
|
60
67
|
}
|
|
61
68
|
}
|
|
62
|
-
/**
|
|
63
|
-
* Parse decimal address and return base58Check encoded address with prefix 'ak'
|
|
64
|
-
* @rtype (input: String) => address: String
|
|
65
|
-
* @param {String} decimalAddress - Address
|
|
66
|
-
* @return {String} address
|
|
67
|
-
*/
|
|
68
|
-
|
|
69
|
-
export function addressFromDecimal(decimalAddress) {
|
|
70
|
-
return aeEncodeKey(toBytes(decimalAddress, true));
|
|
71
|
-
}
|
|
72
69
|
/**
|
|
73
70
|
* Calculate SHA256 hash of `input`
|
|
74
71
|
* @rtype (input: String) => hash: String
|
|
@@ -88,64 +85,6 @@ export function sha256hash(input) {
|
|
|
88
85
|
export function salt() {
|
|
89
86
|
return Math.floor(Math.random() * Math.floor(Number.MAX_SAFE_INTEGER));
|
|
90
87
|
}
|
|
91
|
-
/**
|
|
92
|
-
* Base64check encode given `input`
|
|
93
|
-
* @rtype (input: String|buffer) => Buffer
|
|
94
|
-
* @param {String} input - Data to encode
|
|
95
|
-
* @return {Buffer} Base64check encoded data
|
|
96
|
-
*/
|
|
97
|
-
|
|
98
|
-
export function encodeBase64Check(input) {
|
|
99
|
-
const buffer = Buffer.from(input);
|
|
100
|
-
const checksum = checkSumFn(input);
|
|
101
|
-
const payloadWithChecksum = Buffer.concat([buffer, checksum], buffer.length + 4);
|
|
102
|
-
return payloadWithChecksum.toString('base64');
|
|
103
|
-
}
|
|
104
|
-
export function checkSumFn(payload) {
|
|
105
|
-
return sha256hash(sha256hash(payload)).slice(0, 4);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function decodeRaw(buffer) {
|
|
109
|
-
const payload = buffer.slice(0, -4);
|
|
110
|
-
const checksum = buffer.slice(-4);
|
|
111
|
-
const newChecksum = checkSumFn(payload);
|
|
112
|
-
if (!checksum.equals(newChecksum)) return;
|
|
113
|
-
return payload;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Base64check decode given `str`
|
|
117
|
-
* @rtype (str: String) => Buffer
|
|
118
|
-
* @param {String} str - Data to decode
|
|
119
|
-
* @return {Buffer} Base64check decoded data
|
|
120
|
-
*/
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
export function decodeBase64Check(str) {
|
|
124
|
-
const buffer = Buffer.from(str, 'base64');
|
|
125
|
-
const payload = decodeRaw(buffer);
|
|
126
|
-
if (!payload) throw new Error('Invalid checksum');
|
|
127
|
-
return Buffer.from(payload);
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Base58 encode given `input`
|
|
131
|
-
* @rtype (input: String) => String
|
|
132
|
-
* @param {String|Buffer} input - Data to encode
|
|
133
|
-
* @return {String} Base58 encoded data
|
|
134
|
-
*/
|
|
135
|
-
|
|
136
|
-
export function encodeBase58Check(input) {
|
|
137
|
-
return bs58check.encode(Buffer.from(input));
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Base58 decode given `str`
|
|
141
|
-
* @rtype (str: String) => Buffer
|
|
142
|
-
* @param {String} str - Data to decode
|
|
143
|
-
* @return {Buffer} Base58 decoded data
|
|
144
|
-
*/
|
|
145
|
-
|
|
146
|
-
export function decodeBase58Check(str) {
|
|
147
|
-
return bs58check.decode(str);
|
|
148
|
-
}
|
|
149
88
|
/**
|
|
150
89
|
* Converts a positive integer to the smallest possible
|
|
151
90
|
* representation in a binary digit representation
|
|
@@ -155,7 +94,8 @@ export function decodeBase58Check(str) {
|
|
|
155
94
|
*/
|
|
156
95
|
|
|
157
96
|
export function encodeUnsigned(value) {
|
|
158
|
-
const binary =
|
|
97
|
+
const binary = _Buffer.allocUnsafe(4);
|
|
98
|
+
|
|
159
99
|
binary.writeUInt32BE(value);
|
|
160
100
|
return binary.slice(binary.findIndex(i => i !== 0));
|
|
161
101
|
} // Todo Duplicated in tx builder. remove
|
|
@@ -170,8 +110,10 @@ export function encodeUnsigned(value) {
|
|
|
170
110
|
|
|
171
111
|
export function encodeContractAddress(owner, nonce) {
|
|
172
112
|
const publicKey = decode(owner, 'ak');
|
|
173
|
-
|
|
174
|
-
|
|
113
|
+
|
|
114
|
+
const binary = _Buffer.concat([publicKey, encodeUnsigned(nonce)]);
|
|
115
|
+
|
|
116
|
+
return encode(hash(binary), 'ct');
|
|
175
117
|
} // KEY-PAIR HELPERS
|
|
176
118
|
|
|
177
119
|
/**
|
|
@@ -195,8 +137,10 @@ export function generateKeyPair() {
|
|
|
195
137
|
let raw = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
196
138
|
// <node>/apps/aens/test/aens_test_utils.erl
|
|
197
139
|
const keyPair = nacl.sign.keyPair();
|
|
198
|
-
|
|
199
|
-
const
|
|
140
|
+
|
|
141
|
+
const publicBuffer = _Buffer.from(keyPair.publicKey);
|
|
142
|
+
|
|
143
|
+
const secretBuffer = _Buffer.from(keyPair.secretKey);
|
|
200
144
|
|
|
201
145
|
if (raw) {
|
|
202
146
|
return {
|
|
@@ -205,7 +149,7 @@ export function generateKeyPair() {
|
|
|
205
149
|
};
|
|
206
150
|
} else {
|
|
207
151
|
return {
|
|
208
|
-
publicKey:
|
|
152
|
+
publicKey: encode(publicBuffer, 'ak'),
|
|
209
153
|
secretKey: secretBuffer.toString('hex')
|
|
210
154
|
};
|
|
211
155
|
}
|
|
@@ -232,10 +176,11 @@ export function encryptKey(password, binaryData) {
|
|
|
232
176
|
*/
|
|
233
177
|
|
|
234
178
|
export function decryptKey(password, encrypted) {
|
|
235
|
-
const encryptedBytes =
|
|
179
|
+
const encryptedBytes = _Buffer.from(encrypted);
|
|
180
|
+
|
|
236
181
|
const hashedPasswordBytes = sha256hash(password);
|
|
237
182
|
const aesEcb = new Ecb(hashedPasswordBytes);
|
|
238
|
-
return
|
|
183
|
+
return _Buffer.from(aesEcb.decrypt(encryptedBytes));
|
|
239
184
|
} // SIGNATURES
|
|
240
185
|
|
|
241
186
|
/**
|
|
@@ -247,7 +192,7 @@ export function decryptKey(password, encrypted) {
|
|
|
247
192
|
*/
|
|
248
193
|
|
|
249
194
|
export function sign(data, privateKey) {
|
|
250
|
-
return nacl.sign.detached(
|
|
195
|
+
return nacl.sign.detached(_Buffer.from(data), _Buffer.from(privateKey));
|
|
251
196
|
}
|
|
252
197
|
/**
|
|
253
198
|
* Verify that signature was signed by public key
|
|
@@ -262,10 +207,12 @@ export function verify(str, signature, publicKey) {
|
|
|
262
207
|
return nacl.sign.detached.verify(new Uint8Array(str), signature, publicKey);
|
|
263
208
|
}
|
|
264
209
|
export function messageToHash(message) {
|
|
265
|
-
const p =
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
210
|
+
const p = _Buffer.from('aeternity Signed Message:\n', 'utf8');
|
|
211
|
+
|
|
212
|
+
const msg = _Buffer.from(message, 'utf8');
|
|
213
|
+
|
|
214
|
+
if (msg.length >= 0xFD) throw new NotImplementedError('Message too long');
|
|
215
|
+
return hash(_Buffer.concat([_Buffer.from([p.length]), p, _Buffer.from([msg.length]), msg]));
|
|
269
216
|
}
|
|
270
217
|
export function signMessage(message, privateKey) {
|
|
271
218
|
return sign(messageToHash(message), privateKey);
|
|
@@ -273,19 +220,6 @@ export function signMessage(message, privateKey) {
|
|
|
273
220
|
export function verifyMessage(str, signature, publicKey) {
|
|
274
221
|
return verify(messageToHash(str), signature, publicKey);
|
|
275
222
|
}
|
|
276
|
-
/**
|
|
277
|
-
* æternity readable public keys are the base58-encoded public key, prepended
|
|
278
|
-
* with 'ak_'
|
|
279
|
-
* @rtype (binaryKey: Buffer) => String
|
|
280
|
-
* @param {Buffer} binaryKey - Key to encode
|
|
281
|
-
* @return {String} Encoded key
|
|
282
|
-
*/
|
|
283
|
-
|
|
284
|
-
export function aeEncodeKey(binaryKey) {
|
|
285
|
-
const publicKeyBuffer = Buffer.from(binaryKey, 'hex');
|
|
286
|
-
const pubKeyAddress = encodeBase58Check(publicKeyBuffer);
|
|
287
|
-
return `ak_${pubKeyAddress}`;
|
|
288
|
-
}
|
|
289
223
|
/**
|
|
290
224
|
* Check key pair for validity
|
|
291
225
|
*
|
|
@@ -297,8 +231,9 @@ export function aeEncodeKey(binaryKey) {
|
|
|
297
231
|
*/
|
|
298
232
|
|
|
299
233
|
export function isValidKeypair(privateKey, publicKey) {
|
|
300
|
-
const message =
|
|
234
|
+
const message = _Buffer.from('TheMessage');
|
|
235
|
+
|
|
301
236
|
const signature = sign(message, privateKey);
|
|
302
237
|
return verify(message, signature, publicKey);
|
|
303
238
|
}
|
|
304
|
-
//# sourceMappingURL=crypto.
|
|
239
|
+
//# sourceMappingURL=crypto.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/crypto.js"],"names":["nacl","aesjs","shajs","str2buf","encode","decode","hash","NotImplementedError","Ecb","ModeOfOperation","ecb","getAddressFromPriv","secret","keys","sign","keyPair","fromSecretKey","publicBuffer","from","publicKey","isAddressValid","address","prefix","e","sha256hash","input","update","digest","salt","Math","floor","random","Number","MAX_SAFE_INTEGER","encodeUnsigned","value","binary","allocUnsafe","writeUInt32BE","slice","findIndex","i","encodeContractAddress","owner","nonce","concat","generateKeyPairFromSecret","generateKeyPair","raw","secretBuffer","secretKey","toString","encryptKey","password","binaryData","hashedPasswordBytes","aesEcb","encrypt","decryptKey","encrypted","encryptedBytes","decrypt","data","privateKey","detached","verify","str","signature","Uint8Array","messageToHash","message","p","msg","length","signMessage","verifyMessage","isValidKeypair"],"mappings":";;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AAEA,OAAOA,IAAP,MAAiB,WAAjB;AACA,OAAOC,KAAP,MAAkB,QAAlB;AACA,OAAOC,KAAP,MAAkB,QAAlB;SAESC,O;SACAC,M,EAAQC,M;SACRC,I;SACAC,mB;;AAIT,MAAMC,GAAG,GAAGP,KAAK,CAACQ,eAAN,CAAsBC,GAAlC;AAEA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,kBAAT,CAA6BC,MAA7B,EAAqC;AAC1C,QAAMC,IAAI,GAAGb,IAAI,CAACc,IAAL,CAAUC,OAAV,CAAkBC,aAAlB,CAAgCb,OAAO,CAACS,MAAD,CAAvC,CAAb;;AACA,QAAMK,YAAY,GAAG,QAAOC,IAAP,CAAYL,IAAI,CAACM,SAAjB,CAArB;;AACA,SAAOf,MAAM,CAACa,YAAD,EAAe,IAAf,CAAb;AACD;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASG,cAAT,CAAyBC,OAAzB,EAAiD;AAAA,MAAfC,MAAe,uEAAN,IAAM;;AACtD,MAAI;AACFjB,IAAAA,MAAM,CAACgB,OAAD,EAAUC,MAAV,CAAN;AACA,WAAO,IAAP;AACD,GAHD,CAGE,OAAOC,CAAP,EAAU;AACV,WAAO,KAAP;AACD;AACF;AAED;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASC,UAAT,CAAqBC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D;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASyD,cAAT,CAAyBb,UAAzB,EAAqC5C,SAArC,EAAgD;AACrD,QAAMmD,OAAO,GAAG,QAAOpD,IAAP,CAAY,YAAZ,CAAhB;;AACA,QAAMiD,SAAS,GAAGrD,IAAI,CAACwD,OAAD,EAAUP,UAAV,CAAtB;AACA,SAAOE,MAAM,CAACK,OAAD,EAAUH,SAAV,EAAqBhD,SAArB,CAAb;AACD","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 */\n\n/**\n * Crypto module\n * @module @aeternity/aepp-sdk/es/utils/crypto\n * @example import { Crypto } from '@aeternity/aepp-sdk'\n */\n\nimport nacl from 'tweetnacl'\nimport aesjs from 'aes-js'\nimport shajs from 'sha.js'\n\nimport { str2buf } from './bytes'\nimport { encode, decode } from '../tx/builder/helpers'\nimport { hash } from './crypto-ts'\nimport { NotImplementedError } from './errors'\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 encode(publicBuffer, 'ak')\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 try {\n decode(address, prefix)\n return true\n } catch (e) {\n return false\n }\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 * 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 encode(hash(binary), 'ct')\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 | Buffer, secretKey: String | 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: encode(publicBuffer, 'ak'),\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 NotImplementedError('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 * 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"],"file":"crypto.mjs"}
|