@aeternity/aepp-sdk 11.0.0 → 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.
Files changed (38) hide show
  1. package/dist/aepp-sdk.browser-script.js +1 -1
  2. package/dist/aepp-sdk.browser-script.js.map +1 -1
  3. package/dist/aepp-sdk.browser.js +1 -1
  4. package/dist/aepp-sdk.browser.js.map +1 -1
  5. package/dist/aepp-sdk.js +1 -1
  6. package/dist/aepp-sdk.js.map +1 -1
  7. package/es/ae/contract.mjs +1 -1
  8. package/es/ae/contract.mjs.map +1 -1
  9. package/es/channel/index.mjs +1 -1
  10. package/es/channel/index.mjs.map +1 -1
  11. package/es/channel/internal.mjs +3 -4
  12. package/es/channel/internal.mjs.map +1 -1
  13. package/es/contract/aci.mjs +1 -1
  14. package/es/contract/aci.mjs.map +1 -1
  15. package/es/encoder.d.ts +26 -0
  16. package/es/tx/builder/constants.mjs +108 -0
  17. package/es/tx/builder/constants.mjs.map +1 -0
  18. package/es/tx/builder/helpers.mjs +10 -52
  19. package/es/tx/builder/helpers.mjs.map +1 -1
  20. package/es/tx/builder/schema.mjs +4 -107
  21. package/es/tx/builder/schema.mjs.map +1 -1
  22. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs +1 -1
  23. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs.map +1 -1
  24. package/es/utils/crypto.mjs +2 -12
  25. package/es/utils/crypto.mjs.map +1 -1
  26. package/es/utils/encoder.mjs +106 -0
  27. package/es/utils/encoder.mjs.map +1 -0
  28. package/package.json +4 -2
  29. package/src/ae/contract.js +1 -1
  30. package/src/channel/index.js +6 -1
  31. package/src/channel/internal.js +5 -6
  32. package/src/contract/aci.js +1 -1
  33. package/src/tx/builder/constants.js +100 -0
  34. package/src/tx/builder/helpers.js +7 -53
  35. package/src/tx/builder/schema.js +1 -98
  36. package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +1 -1
  37. package/src/utils/crypto.js +2 -12
  38. package/src/utils/encoder.ts +101 -0
@@ -0,0 +1,101 @@
1
+ import * as bs58 from 'bs58'
2
+ import { sha256 } from 'sha.js'
3
+ import {
4
+ DecodeError,
5
+ EncodeError,
6
+ InvalidChecksumError,
7
+ PayloadLengthError,
8
+ PrefixMismatchError
9
+ } from './errors'
10
+
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
+ export function sha256hash (input: Buffer | string) {
18
+ return new sha256().update(input).digest()
19
+ }
20
+
21
+ // based on https://github.com/aeternity/protocol/blob/master/node/api/api_encoding.md
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']
24
+ // TODO: add all types with a fixed length
25
+ const typesLength: { [name: string]: number } = {
26
+ ak: 32,
27
+ ct: 32,
28
+ ok: 32
29
+ } as const
30
+
31
+ function ensureValidLength (data: Buffer | string, type: string) {
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: Buffer | string) => sha256hash(sha256hash(payload)).slice(0, 4)
38
+
39
+ const addChecksum = (input: Buffer | string) => {
40
+ const payload = Buffer.from(input)
41
+ return Buffer.concat([payload, getChecksum(payload)])
42
+ }
43
+
44
+ function getPayload (buffer: Buffer) {
45
+ const payload = buffer.slice(0, -4)
46
+ if (!getChecksum(payload).equals(buffer.slice(-4))) throw new InvalidChecksumError()
47
+ return payload
48
+ }
49
+
50
+ const base64 = {
51
+ encode: (buffer: Buffer | string) => addChecksum(buffer).toString('base64'),
52
+ decode: (string: string) => getPayload(Buffer.from(string, 'base64'))
53
+ }
54
+
55
+ const base58 = {
56
+ encode: (buffer: Buffer | string) => bs58.encode(addChecksum(buffer)),
57
+ decode: (string: string) => getPayload(bs58.decode(string))
58
+ }
59
+
60
+ /**
61
+ * Decode data using the default encoding/decoding algorithm
62
+ * @function
63
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
64
+ * @param {string} data An Base58/64check encoded and prefixed string (ex tx_..., sg_..., ak_....)
65
+ * @param {string} [requiredPrefix] Ensure that data have this prefix
66
+ * @return {Buffer} Decoded data
67
+ */
68
+ export function decode (data: string, requiredPrefix?: string) {
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
+ if (requiredPrefix && requiredPrefix !== prefix) {
73
+ throw new PrefixMismatchError(prefix, requiredPrefix)
74
+ }
75
+ const decoder = (base64Types.includes(prefix) && base64.decode) ||
76
+ (base58Types.includes(prefix) && base58.decode)
77
+ if (!decoder) {
78
+ throw new DecodeError(`Encoded string have unknown type: ${prefix}`)
79
+ }
80
+ const payload = decoder(encodedPayload)
81
+ ensureValidLength(payload, prefix)
82
+ return payload
83
+ }
84
+
85
+ /**
86
+ * Encode data using the default encoding/decoding algorithm
87
+ * @function
88
+ * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
89
+ * @param {Buffer|String} data An decoded data
90
+ * @param {string} type Prefix of Transaction
91
+ * @return {String} Encoded string Base58check or Base64check data
92
+ */
93
+ export function encode (data: Buffer | string, type: string) {
94
+ const encoder = (base64Types.includes(type) && base64.encode) ||
95
+ (base58Types.includes(type) && base58.encode)
96
+ if (!encoder) {
97
+ throw new EncodeError(`Unknown type: ${type}`)
98
+ }
99
+ ensureValidLength(data, type)
100
+ return `${type}_${encoder(data)}`
101
+ }