@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
package/src/utils/crypto.js
CHANGED
|
@@ -21,17 +21,16 @@
|
|
|
21
21
|
* @example import { Crypto } from '@aeternity/aepp-sdk'
|
|
22
22
|
*/
|
|
23
23
|
|
|
24
|
-
import bs58check from 'bs58check'
|
|
25
|
-
import ed2curve from 'ed2curve'
|
|
26
24
|
import nacl from 'tweetnacl'
|
|
27
25
|
import aesjs from 'aes-js'
|
|
28
|
-
import shajs from 'sha.js'
|
|
29
26
|
|
|
30
|
-
import { str2buf
|
|
31
|
-
import { decode } from '
|
|
27
|
+
import { str2buf } from './bytes'
|
|
28
|
+
import { encode, decode, sha256hash } from './encoder'
|
|
32
29
|
import { hash } from './crypto-ts'
|
|
30
|
+
import { NotImplementedError } from './errors'
|
|
33
31
|
|
|
34
32
|
export * from './crypto-ts'
|
|
33
|
+
export { sha256hash }
|
|
35
34
|
|
|
36
35
|
const Ecb = aesjs.ModeOfOperation.ecb
|
|
37
36
|
|
|
@@ -44,7 +43,7 @@ const Ecb = aesjs.ModeOfOperation.ecb
|
|
|
44
43
|
export function getAddressFromPriv (secret) {
|
|
45
44
|
const keys = nacl.sign.keyPair.fromSecretKey(str2buf(secret))
|
|
46
45
|
const publicBuffer = Buffer.from(keys.publicKey)
|
|
47
|
-
return
|
|
46
|
+
return encode(publicBuffer, 'ak')
|
|
48
47
|
}
|
|
49
48
|
|
|
50
49
|
/**
|
|
@@ -55,34 +54,14 @@ export function getAddressFromPriv (secret) {
|
|
|
55
54
|
* @return {Boolean} valid
|
|
56
55
|
*/
|
|
57
56
|
export function isAddressValid (address, prefix = 'ak') {
|
|
58
|
-
if (typeof address !== 'string') return false
|
|
59
57
|
try {
|
|
60
|
-
|
|
58
|
+
decode(address, prefix)
|
|
59
|
+
return true
|
|
61
60
|
} catch (e) {
|
|
62
61
|
return false
|
|
63
62
|
}
|
|
64
63
|
}
|
|
65
64
|
|
|
66
|
-
/**
|
|
67
|
-
* Parse decimal address and return base58Check encoded address with prefix 'ak'
|
|
68
|
-
* @rtype (input: String) => address: String
|
|
69
|
-
* @param {String} decimalAddress - Address
|
|
70
|
-
* @return {String} address
|
|
71
|
-
*/
|
|
72
|
-
export function addressFromDecimal (decimalAddress) {
|
|
73
|
-
return aeEncodeKey(toBytes(decimalAddress, true))
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Calculate SHA256 hash of `input`
|
|
78
|
-
* @rtype (input: String) => hash: String
|
|
79
|
-
* @param {String} input - Data to hash
|
|
80
|
-
* @return {String} Hash
|
|
81
|
-
*/
|
|
82
|
-
export function sha256hash (input) {
|
|
83
|
-
return shajs('sha256').update(input).digest()
|
|
84
|
-
}
|
|
85
|
-
|
|
86
65
|
/**
|
|
87
66
|
* Generate a random salt (positive integer)
|
|
88
67
|
* @rtype () => salt: Number
|
|
@@ -92,66 +71,6 @@ export function salt () {
|
|
|
92
71
|
return Math.floor(Math.random() * Math.floor(Number.MAX_SAFE_INTEGER))
|
|
93
72
|
}
|
|
94
73
|
|
|
95
|
-
/**
|
|
96
|
-
* Base64check encode given `input`
|
|
97
|
-
* @rtype (input: String|buffer) => Buffer
|
|
98
|
-
* @param {String} input - Data to encode
|
|
99
|
-
* @return {Buffer} Base64check encoded data
|
|
100
|
-
*/
|
|
101
|
-
export function encodeBase64Check (input) {
|
|
102
|
-
const buffer = Buffer.from(input)
|
|
103
|
-
const checksum = checkSumFn(input)
|
|
104
|
-
const payloadWithChecksum = Buffer.concat([buffer, checksum], buffer.length + 4)
|
|
105
|
-
return payloadWithChecksum.toString('base64')
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export function checkSumFn (payload) {
|
|
109
|
-
return sha256hash(sha256hash(payload)).slice(0, 4)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
function decodeRaw (buffer) {
|
|
113
|
-
const payload = buffer.slice(0, -4)
|
|
114
|
-
const checksum = buffer.slice(-4)
|
|
115
|
-
const newChecksum = checkSumFn(payload)
|
|
116
|
-
|
|
117
|
-
if (!checksum.equals(newChecksum)) return
|
|
118
|
-
|
|
119
|
-
return payload
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Base64check decode given `str`
|
|
124
|
-
* @rtype (str: String) => Buffer
|
|
125
|
-
* @param {String} str - Data to decode
|
|
126
|
-
* @return {Buffer} Base64check decoded data
|
|
127
|
-
*/
|
|
128
|
-
export function decodeBase64Check (str) {
|
|
129
|
-
const buffer = Buffer.from(str, 'base64')
|
|
130
|
-
const payload = decodeRaw(buffer)
|
|
131
|
-
if (!payload) throw new Error('Invalid checksum')
|
|
132
|
-
return Buffer.from(payload)
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Base58 encode given `input`
|
|
137
|
-
* @rtype (input: String) => String
|
|
138
|
-
* @param {String|Buffer} input - Data to encode
|
|
139
|
-
* @return {String} Base58 encoded data
|
|
140
|
-
*/
|
|
141
|
-
export function encodeBase58Check (input) {
|
|
142
|
-
return bs58check.encode(Buffer.from(input))
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Base58 decode given `str`
|
|
147
|
-
* @rtype (str: String) => Buffer
|
|
148
|
-
* @param {String} str - Data to decode
|
|
149
|
-
* @return {Buffer} Base58 decoded data
|
|
150
|
-
*/
|
|
151
|
-
export function decodeBase58Check (str) {
|
|
152
|
-
return bs58check.decode(str)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
74
|
/**
|
|
156
75
|
* Converts a positive integer to the smallest possible
|
|
157
76
|
* representation in a binary digit representation
|
|
@@ -176,7 +95,7 @@ export function encodeUnsigned (value) {
|
|
|
176
95
|
export function encodeContractAddress (owner, nonce) {
|
|
177
96
|
const publicKey = decode(owner, 'ak')
|
|
178
97
|
const binary = Buffer.concat([publicKey, encodeUnsigned(nonce)])
|
|
179
|
-
return
|
|
98
|
+
return encode(hash(binary), 'ct')
|
|
180
99
|
}
|
|
181
100
|
|
|
182
101
|
// KEY-PAIR HELPERS
|
|
@@ -193,7 +112,7 @@ export function generateKeyPairFromSecret (secret) {
|
|
|
193
112
|
|
|
194
113
|
/**
|
|
195
114
|
* Generate a random ED25519 keypair
|
|
196
|
-
* @rtype (raw: Boolean) => {publicKey: String, secretKey: String
|
|
115
|
+
* @rtype (raw: Boolean) => {publicKey: String | Buffer, secretKey: String | Buffer}
|
|
197
116
|
* @param {Boolean} raw - Whether to return raw (binary) keys
|
|
198
117
|
* @return {Object} Key pair
|
|
199
118
|
*/
|
|
@@ -211,7 +130,7 @@ export function generateKeyPair (raw = false) {
|
|
|
211
130
|
}
|
|
212
131
|
} else {
|
|
213
132
|
return {
|
|
214
|
-
publicKey:
|
|
133
|
+
publicKey: encode(publicBuffer, 'ak'),
|
|
215
134
|
secretKey: secretBuffer.toString('hex')
|
|
216
135
|
}
|
|
217
136
|
}
|
|
@@ -272,7 +191,7 @@ export function verify (str, signature, publicKey) {
|
|
|
272
191
|
export function messageToHash (message) {
|
|
273
192
|
const p = Buffer.from('aeternity Signed Message:\n', 'utf8')
|
|
274
193
|
const msg = Buffer.from(message, 'utf8')
|
|
275
|
-
if (msg.length >= 0xFD) throw new
|
|
194
|
+
if (msg.length >= 0xFD) throw new NotImplementedError('Message too long')
|
|
276
195
|
return hash(Buffer.concat([Buffer.from([p.length]), p, Buffer.from([msg.length]), msg]))
|
|
277
196
|
}
|
|
278
197
|
|
|
@@ -284,19 +203,6 @@ export function verifyMessage (str, signature, publicKey) {
|
|
|
284
203
|
return verify(messageToHash(str), signature, publicKey)
|
|
285
204
|
}
|
|
286
205
|
|
|
287
|
-
/**
|
|
288
|
-
* æternity readable public keys are the base58-encoded public key, prepended
|
|
289
|
-
* with 'ak_'
|
|
290
|
-
* @rtype (binaryKey: Buffer) => String
|
|
291
|
-
* @param {Buffer} binaryKey - Key to encode
|
|
292
|
-
* @return {String} Encoded key
|
|
293
|
-
*/
|
|
294
|
-
export function aeEncodeKey (binaryKey) {
|
|
295
|
-
const publicKeyBuffer = Buffer.from(binaryKey, 'hex')
|
|
296
|
-
const pubKeyAddress = encodeBase58Check(publicKeyBuffer)
|
|
297
|
-
return `ak_${pubKeyAddress}`
|
|
298
|
-
}
|
|
299
|
-
|
|
300
206
|
/**
|
|
301
207
|
* Check key pair for validity
|
|
302
208
|
*
|
|
@@ -311,53 +217,3 @@ export function isValidKeypair (privateKey, publicKey) {
|
|
|
311
217
|
const signature = sign(message, privateKey)
|
|
312
218
|
return verify(message, signature, publicKey)
|
|
313
219
|
}
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* This function encrypts a message using base58check encoded and 'ak' prefixed
|
|
317
|
-
* publicKey such that only the corresponding secretKey will
|
|
318
|
-
* be able to decrypt
|
|
319
|
-
* @rtype (msg: String, publicKey: String) => Object
|
|
320
|
-
* @param {Buffer} msg - Data to encode
|
|
321
|
-
* @param {String} publicKey - Public key
|
|
322
|
-
* @return {Object}
|
|
323
|
-
*/
|
|
324
|
-
export function encryptData (msg, publicKey) {
|
|
325
|
-
const ephemeralKeyPair = nacl.box.keyPair()
|
|
326
|
-
const pubKeyUInt8Array = decode(publicKey, 'ak')
|
|
327
|
-
const nonce = nacl.randomBytes(nacl.box.nonceLength)
|
|
328
|
-
|
|
329
|
-
const encryptedMessage = nacl.box(
|
|
330
|
-
Buffer.from(msg),
|
|
331
|
-
nonce,
|
|
332
|
-
ed2curve.convertPublicKey(pubKeyUInt8Array),
|
|
333
|
-
ephemeralKeyPair.secretKey
|
|
334
|
-
)
|
|
335
|
-
|
|
336
|
-
return {
|
|
337
|
-
ciphertext: Buffer.from(encryptedMessage).toString('hex'),
|
|
338
|
-
ephemPubKey: Buffer.from(ephemeralKeyPair.publicKey).toString('hex'),
|
|
339
|
-
nonce: Buffer.from(nonce).toString('hex'),
|
|
340
|
-
version: 'x25519-xsalsa20-poly1305'
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* This function decrypt a message using secret key
|
|
346
|
-
* @rtype (secretKey: String, encryptedData: Object) => Buffer|null
|
|
347
|
-
* @param {String} secretKey - Secret key
|
|
348
|
-
* @param {Object} encryptedData - Encrypted data
|
|
349
|
-
* @return {Buffer|null}
|
|
350
|
-
*/
|
|
351
|
-
export function decryptData (secretKey, encryptedData) {
|
|
352
|
-
const receiverSecretKeyUint8Array = ed2curve.convertSecretKey(Buffer.from(secretKey, 'hex'))
|
|
353
|
-
const nonce = Buffer.from(encryptedData.nonce, 'hex')
|
|
354
|
-
const ciphertext = Buffer.from(encryptedData.ciphertext, 'hex')
|
|
355
|
-
const ephemPubKey = Buffer.from(encryptedData.ephemPubKey, 'hex')
|
|
356
|
-
const decrypted = nacl.box.open(
|
|
357
|
-
ciphertext,
|
|
358
|
-
nonce,
|
|
359
|
-
ephemPubKey,
|
|
360
|
-
receiverSecretKeyUint8Array
|
|
361
|
-
)
|
|
362
|
-
return decrypted ? Buffer.from(decrypted) : decrypted
|
|
363
|
-
}
|
|
@@ -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
|
+
}
|