@aeternity/aepp-sdk 10.0.0-beta.1 → 11.0.1
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} +23 -11
- 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} +14 -11
- package/es/account/multiple.mjs.map +1 -0
- package/es/ae/{aens.js → aens.mjs} +69 -59
- 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} +12 -10
- package/es/ae/index.mjs.map +1 -0
- package/es/ae/{oracle.js → oracle.mjs} +37 -20
- 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} +28 -8
- package/es/chain/index.mjs.map +1 -0
- package/es/chain/{node.js → node.mjs} +133 -90
- 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} +43 -22
- package/es/channel/index.mjs.map +1 -0
- package/es/channel/{internal.js → internal.mjs} +51 -35
- 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} +40 -28
- package/es/contract/ga/index.mjs.map +1 -0
- package/es/encoder.d.ts +26 -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} +15 -12
- package/es/node-pool/index.mjs.map +1 -0
- package/es/{node.js → node.mjs} +17 -11
- package/es/node.mjs.map +1 -0
- package/es/tx/builder/constants.mjs +108 -0
- package/es/tx/builder/constants.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.mjs +321 -0
- package/es/tx/builder/helpers.mjs.map +1 -0
- package/es/tx/builder/{index.js → index.mjs} +92 -74
- package/es/tx/builder/index.mjs.map +1 -0
- package/es/tx/builder/{schema.js → schema.mjs} +16 -172
- package/es/tx/builder/schema.mjs.map +1 -0
- package/es/tx/{index.js → index.mjs} +106 -14
- package/es/tx/index.mjs.map +1 -0
- package/es/tx/{tx-object.js → tx-object.mjs} +31 -18
- 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} +10 -8
- 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} +25 -17
- 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} +15 -8
- 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} +126 -72
- 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} +23 -14
- 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} +116 -91
- 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} +42 -156
- package/es/utils/crypto.mjs.map +1 -0
- package/es/utils/encoder.mjs +106 -0
- package/es/utils/encoder.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} +49 -15
- package/es/utils/swagger.mjs.map +1 -0
- package/package.json +49 -58
- package/src/account/base.js +3 -1
- package/src/account/memory.js +13 -8
- package/src/account/multiple.js +16 -6
- package/src/ae/aens.js +52 -58
- package/src/ae/aepp.js +2 -2
- package/src/ae/contract.js +21 -154
- package/src/ae/index.js +6 -4
- package/src/ae/oracle.js +33 -14
- 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 -65
- package/src/channel/handlers.js +30 -20
- package/src/channel/index.js +46 -16
- package/src/channel/internal.js +35 -30
- package/src/contract/aci.js +376 -0
- package/src/contract/compiler.js +22 -113
- package/src/contract/ga/helpers.js +10 -6
- package/src/contract/ga/index.js +31 -24
- package/src/index.js +9 -8
- package/src/node-pool/index.js +13 -10
- package/src/node.js +11 -10
- package/src/tx/builder/constants.js +100 -0
- package/src/tx/builder/field-types.js +61 -0
- package/src/tx/builder/helpers.js +99 -103
- package/src/tx/builder/index.js +81 -40
- package/src/tx/builder/schema.js +44 -132
- package/src/tx/index.js +101 -12
- package/src/tx/tx-object.js +35 -15
- package/src/tx/tx.js +110 -35
- package/src/tx/validator.js +25 -5
- 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 +106 -57
- package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +24 -11
- package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +95 -53
- 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 +1 -9
- package/src/utils/bytes.ts +3 -2
- package/src/utils/crypto-ts.ts +2 -2
- package/src/utils/crypto.js +11 -155
- package/src/utils/encoder.ts +101 -0
- 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/swagger.js +68 -45
- 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 -308
- 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 -356
- 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 -242
- 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 -158
- package/es/contract/index.js.map +0 -1
- package/es/index.js +0 -56
- 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 +0 -318
- 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/index.js +0 -310
- package/src/contract/aci/transformation.js +0 -72
- package/src/contract/index.js +0 -159
- package/src/utils/aepp-wallet-communication/schema.js +0 -108
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import _buffer from "buffer";
|
|
2
|
+
const {
|
|
3
|
+
Buffer: _Buffer
|
|
4
|
+
} = _buffer;
|
|
5
|
+
import * as bs58 from 'bs58';
|
|
6
|
+
import _shaJs from 'sha.js';
|
|
7
|
+
const {
|
|
8
|
+
sha256
|
|
9
|
+
} = _shaJs;
|
|
10
|
+
import { DecodeError, EncodeError, InvalidChecksumError, PayloadLengthError, PrefixMismatchError } from "./errors.mjs";
|
|
11
|
+
/**
|
|
12
|
+
* Calculate SHA256 hash of `input`
|
|
13
|
+
* @rtype (input: String) => hash: String
|
|
14
|
+
* @param {Buffer|String} input - Data to hash
|
|
15
|
+
* @return {String} Hash
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export function sha256hash(input) {
|
|
19
|
+
return new sha256().update(input).digest();
|
|
20
|
+
} // based on https://github.com/aeternity/protocol/blob/master/node/api/api_encoding.md
|
|
21
|
+
|
|
22
|
+
const base64Types = ['ba', 'cb', 'or', 'ov', 'pi', 'ss', 'cs', 'ck', 'cv', 'st', 'tx'];
|
|
23
|
+
const base58Types = ['ak', 'bf', 'bs', 'bx', 'ch', 'cm', 'ct', 'kh', 'mh', 'nm', 'ok', 'oq', 'pp', 'sg', 'th']; // TODO: add all types with a fixed length
|
|
24
|
+
|
|
25
|
+
const typesLength = {
|
|
26
|
+
ak: 32,
|
|
27
|
+
ct: 32,
|
|
28
|
+
ok: 32
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function ensureValidLength(data, type) {
|
|
32
|
+
if (!typesLength[type]) return;
|
|
33
|
+
if (data.length === typesLength[type]) return;
|
|
34
|
+
throw new PayloadLengthError(`Payload should be ${typesLength[type]} bytes, got ${data.length} instead`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const getChecksum = payload => sha256hash(sha256hash(payload)).slice(0, 4);
|
|
38
|
+
|
|
39
|
+
const addChecksum = input => {
|
|
40
|
+
const payload = _Buffer.from(input);
|
|
41
|
+
|
|
42
|
+
return _Buffer.concat([payload, getChecksum(payload)]);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
function getPayload(buffer) {
|
|
46
|
+
const payload = buffer.slice(0, -4);
|
|
47
|
+
if (!getChecksum(payload).equals(buffer.slice(-4))) throw new InvalidChecksumError();
|
|
48
|
+
return payload;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const base64 = {
|
|
52
|
+
encode: buffer => addChecksum(buffer).toString('base64'),
|
|
53
|
+
decode: string => getPayload(_Buffer.from(string, 'base64'))
|
|
54
|
+
};
|
|
55
|
+
const base58 = {
|
|
56
|
+
encode: buffer => bs58.encode(addChecksum(buffer)),
|
|
57
|
+
decode: string => getPayload(bs58.decode(string))
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Decode data using the default encoding/decoding algorithm
|
|
61
|
+
* @function
|
|
62
|
+
* @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
|
|
63
|
+
* @param {string} data An Base58/64check encoded and prefixed string (ex tx_..., sg_..., ak_....)
|
|
64
|
+
* @param {string} [requiredPrefix] Ensure that data have this prefix
|
|
65
|
+
* @return {Buffer} Decoded data
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
export function decode(data, requiredPrefix) {
|
|
69
|
+
const [prefix, encodedPayload, extra] = data.split('_');
|
|
70
|
+
if (!encodedPayload) throw new DecodeError(`Encoded string missing payload: ${data}`);
|
|
71
|
+
if (extra) throw new DecodeError(`Encoded string have extra parts: ${data}`);
|
|
72
|
+
|
|
73
|
+
if (requiredPrefix && requiredPrefix !== prefix) {
|
|
74
|
+
throw new PrefixMismatchError(prefix, requiredPrefix);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const decoder = base64Types.includes(prefix) && base64.decode || base58Types.includes(prefix) && base58.decode;
|
|
78
|
+
|
|
79
|
+
if (!decoder) {
|
|
80
|
+
throw new DecodeError(`Encoded string have unknown type: ${prefix}`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const payload = decoder(encodedPayload);
|
|
84
|
+
ensureValidLength(payload, prefix);
|
|
85
|
+
return payload;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Encode data using the default encoding/decoding algorithm
|
|
89
|
+
* @function
|
|
90
|
+
* @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
|
|
91
|
+
* @param {Buffer|String} data An decoded data
|
|
92
|
+
* @param {string} type Prefix of Transaction
|
|
93
|
+
* @return {String} Encoded string Base58check or Base64check data
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
export function encode(data, type) {
|
|
97
|
+
const encoder = base64Types.includes(type) && base64.encode || base58Types.includes(type) && base58.encode;
|
|
98
|
+
|
|
99
|
+
if (!encoder) {
|
|
100
|
+
throw new EncodeError(`Unknown type: ${type}`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
ensureValidLength(data, type);
|
|
104
|
+
return `${type}_${encoder(data)}`;
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=encoder.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utils/encoder.ts"],"names":["bs58","DecodeError","EncodeError","InvalidChecksumError","PayloadLengthError","PrefixMismatchError","sha256hash","input","sha256","update","digest","base64Types","base58Types","typesLength","ak","ct","ok","ensureValidLength","data","type","length","getChecksum","payload","slice","addChecksum","from","concat","getPayload","buffer","equals","base64","encode","toString","decode","string","base58","requiredPrefix","prefix","encodedPayload","extra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s58 from 'bs58'\nimport { sha256 } from 'sha.js'\nimport {\n DecodeError,\n EncodeError,\n InvalidChecksumError,\n PayloadLengthError,\n PrefixMismatchError\n} from './errors'\n\n/**\n * Calculate SHA256 hash of `input`\n * @rtype (input: String) => hash: String\n * @param {Buffer|String} input - Data to hash\n * @return {String} Hash\n */\nexport function sha256hash (input: Buffer | string) {\n return new sha256().update(input).digest()\n}\n\n// based on https://github.com/aeternity/protocol/blob/master/node/api/api_encoding.md\nconst base64Types = ['ba', 'cb', 'or', 'ov', 'pi', 'ss', 'cs', 'ck', 'cv', 'st', 'tx']\nconst base58Types = ['ak', 'bf', 'bs', 'bx', 'ch', 'cm', 'ct', 'kh', 'mh', 'nm', 'ok', 'oq', 'pp', 'sg', 'th']\n// TODO: add all types with a fixed length\nconst typesLength: { [name: string]: number } = {\n ak: 32,\n ct: 32,\n ok: 32\n} as const\n\nfunction ensureValidLength (data: Buffer | string, type: string) {\n if (!typesLength[type]) return\n if (data.length === typesLength[type]) return\n throw new PayloadLengthError(`Payload should be ${typesLength[type]} bytes, got ${data.length} instead`)\n}\n\nconst getChecksum = (payload: Buffer | string) => sha256hash(sha256hash(payload)).slice(0, 4)\n\nconst addChecksum = (input: Buffer | string) => {\n const payload = Buffer.from(input)\n return Buffer.concat([payload, getChecksum(payload)])\n}\n\nfunction getPayload (buffer: Buffer) {\n const payload = buffer.slice(0, -4)\n if (!getChecksum(payload).equals(buffer.slice(-4))) throw new InvalidChecksumError()\n return payload\n}\n\nconst base64 = {\n encode: (buffer: Buffer | string) => addChecksum(buffer).toString('base64'),\n decode: (string: string) => getPayload(Buffer.from(string, 'base64'))\n}\n\nconst base58 = {\n encode: (buffer: Buffer | string) => bs58.encode(addChecksum(buffer)),\n decode: (string: string) => getPayload(bs58.decode(string))\n}\n\n/**\n * Decode data using the default encoding/decoding algorithm\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {string} data An Base58/64check encoded and prefixed string (ex tx_..., sg_..., ak_....)\n * @param {string} [requiredPrefix] Ensure that data have this prefix\n * @return {Buffer} Decoded data\n */\nexport function decode (data: string, requiredPrefix?: string) {\n const [prefix, encodedPayload, extra] = data.split('_')\n if (!encodedPayload) throw new DecodeError(`Encoded string missing payload: ${data}`)\n if (extra) throw new DecodeError(`Encoded string have extra parts: ${data}`)\n if (requiredPrefix && requiredPrefix !== prefix) {\n throw new PrefixMismatchError(prefix, requiredPrefix)\n }\n const decoder = (base64Types.includes(prefix) && base64.decode) ||\n (base58Types.includes(prefix) && base58.decode)\n if (!decoder) {\n throw new DecodeError(`Encoded string have unknown type: ${prefix}`)\n }\n const payload = decoder(encodedPayload)\n ensureValidLength(payload, prefix)\n return payload\n}\n\n/**\n * Encode data using the default encoding/decoding algorithm\n * @function\n * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers\n * @param {Buffer|String} data An decoded data\n * @param {string} type Prefix of Transaction\n * @return {String} Encoded string Base58check or Base64check data\n */\nexport function encode (data: Buffer | string, type: string) {\n const encoder = (base64Types.includes(type) && base64.encode) ||\n (base58Types.includes(type) && base58.encode)\n if (!encoder) {\n throw new EncodeError(`Unknown type: ${type}`)\n }\n ensureValidLength(data, type)\n return `${type}_${encoder(data)}`\n}\n"],"file":"encoder.mjs"}
|