@aeternity/aepp-sdk 8.2.0 → 10.0.0-beta.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 (145) hide show
  1. package/README.md +16 -9
  2. package/dist/aepp-sdk.browser-script.js +1 -1
  3. package/dist/aepp-sdk.browser-script.js.LICENSE.txt +0 -9
  4. package/dist/aepp-sdk.browser-script.js.map +1 -1
  5. package/dist/aepp-sdk.browser.js +1 -1
  6. package/dist/aepp-sdk.browser.js.map +1 -1
  7. package/dist/aepp-sdk.js +1 -1
  8. package/dist/aepp-sdk.js.map +1 -1
  9. package/es/account/base.js +33 -119
  10. package/es/account/base.js.map +1 -1
  11. package/es/account/memory.js +17 -19
  12. package/es/account/memory.js.map +1 -1
  13. package/es/account/multiple.js +75 -154
  14. package/es/account/multiple.js.map +1 -1
  15. package/es/ae/aens.js +164 -599
  16. package/es/ae/aens.js.map +1 -1
  17. package/es/ae/contract.js +108 -916
  18. package/es/ae/contract.js.map +1 -1
  19. package/es/ae/index.js +89 -250
  20. package/es/ae/index.js.map +1 -1
  21. package/es/ae/oracle.js +132 -500
  22. package/es/ae/oracle.js.map +1 -1
  23. package/es/chain/index.js +4 -23
  24. package/es/chain/index.js.map +1 -1
  25. package/es/chain/node.js +292 -1018
  26. package/es/chain/node.js.map +1 -1
  27. package/es/channel/handlers.js +597 -1086
  28. package/es/channel/handlers.js.map +1 -1
  29. package/es/channel/index.js +203 -451
  30. package/es/channel/index.js.map +1 -1
  31. package/es/channel/internal.js +129 -301
  32. package/es/channel/internal.js.map +1 -1
  33. package/es/contract/aci/index.js +319 -491
  34. package/es/contract/aci/index.js.map +1 -1
  35. package/es/contract/aci/transformation.js +35 -711
  36. package/es/contract/aci/transformation.js.map +1 -1
  37. package/es/contract/compiler.js +129 -203
  38. package/es/contract/compiler.js.map +1 -1
  39. package/es/contract/ga/helpers.js +17 -122
  40. package/es/contract/ga/helpers.js.map +1 -1
  41. package/es/contract/ga/index.js +102 -247
  42. package/es/contract/ga/index.js.map +1 -1
  43. package/es/contract/index.js +1 -1
  44. package/es/contract/index.js.map +1 -1
  45. package/es/index.js +2 -3
  46. package/es/index.js.map +1 -1
  47. package/es/node-pool/index.js +54 -73
  48. package/es/node-pool/index.js.map +1 -1
  49. package/es/node.js +56 -157
  50. package/es/node.js.map +1 -1
  51. package/es/other.d.ts +1 -1
  52. package/es/tx/builder/helpers.js +49 -89
  53. package/es/tx/builder/helpers.js.map +1 -1
  54. package/es/tx/builder/index.js +205 -303
  55. package/es/tx/builder/index.js.map +1 -1
  56. package/es/tx/builder/schema.js +585 -573
  57. package/es/tx/builder/schema.js.map +1 -1
  58. package/es/tx/index.js +1 -1
  59. package/es/tx/index.js.map +1 -1
  60. package/es/tx/tx-object.js +105 -135
  61. package/es/tx/tx-object.js.map +1 -1
  62. package/es/tx/tx.js +810 -1586
  63. package/es/tx/tx.js.map +1 -1
  64. package/es/tx/validator.js +191 -426
  65. package/es/tx/validator.js.map +1 -1
  66. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js +17 -21
  67. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +1 -1
  68. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js +38 -74
  69. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +1 -1
  70. package/es/utils/aepp-wallet-communication/content-script-bridge.js +14 -15
  71. package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +1 -1
  72. package/es/utils/aepp-wallet-communication/helpers.js +60 -105
  73. package/es/utils/aepp-wallet-communication/helpers.js.map +1 -1
  74. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js +179 -510
  75. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +1 -1
  76. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js +70 -89
  77. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +1 -1
  78. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js +354 -603
  79. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +1 -1
  80. package/es/utils/aepp-wallet-communication/schema.js +43 -39
  81. package/es/utils/aepp-wallet-communication/schema.js.map +1 -1
  82. package/es/utils/aepp-wallet-communication/wallet-detector.js +32 -58
  83. package/es/utils/aepp-wallet-communication/wallet-detector.js.map +1 -1
  84. package/es/utils/amount-formatter.js +41 -61
  85. package/es/utils/amount-formatter.js.map +1 -1
  86. package/es/utils/async-init.js +28 -64
  87. package/es/utils/async-init.js.map +1 -1
  88. package/es/utils/bignumber.js +3 -11
  89. package/es/utils/bignumber.js.map +1 -1
  90. package/es/utils/bytes.js +5 -9
  91. package/es/utils/bytes.js.map +1 -1
  92. package/es/utils/crypto.js +49 -75
  93. package/es/utils/crypto.js.map +1 -1
  94. package/es/utils/hd-wallet.js +45 -81
  95. package/es/utils/hd-wallet.js.map +1 -1
  96. package/es/utils/json-big.js +16 -49
  97. package/es/utils/json-big.js.map +1 -1
  98. package/es/utils/keystore.js +81 -246
  99. package/es/utils/keystore.js.map +1 -1
  100. package/es/utils/mptree.js +88 -134
  101. package/es/utils/mptree.js.map +1 -1
  102. package/es/utils/other.js +10 -60
  103. package/es/utils/other.js.map +1 -1
  104. package/es/utils/semver-satisfies.js +9 -28
  105. package/es/utils/semver-satisfies.js.map +1 -1
  106. package/es/utils/string.js +4 -13
  107. package/es/utils/string.js.map +1 -1
  108. package/es/utils/swagger.js +85 -218
  109. package/es/utils/swagger.js.map +1 -1
  110. package/package.json +52 -43
  111. package/src/account/base.js +8 -9
  112. package/src/account/multiple.js +20 -16
  113. package/src/ae/aens.js +25 -21
  114. package/src/ae/contract.js +65 -246
  115. package/src/ae/index.js +31 -7
  116. package/src/ae/oracle.js +21 -17
  117. package/src/chain/index.js +2 -12
  118. package/src/chain/node.js +26 -29
  119. package/src/channel/handlers.js +0 -1
  120. package/src/channel/index.js +4 -5
  121. package/src/contract/aci/index.js +221 -106
  122. package/src/contract/aci/transformation.js +30 -422
  123. package/src/contract/compiler.js +45 -4
  124. package/src/contract/ga/helpers.js +2 -12
  125. package/src/contract/ga/index.js +22 -26
  126. package/src/index.js +2 -4
  127. package/src/node-pool/index.js +21 -12
  128. package/src/node.js +22 -26
  129. package/src/tx/builder/helpers.js +12 -24
  130. package/src/tx/builder/index.js +18 -15
  131. package/src/tx/builder/schema.js +43 -108
  132. package/src/tx/tx-object.js +0 -2
  133. package/src/tx/tx.js +157 -144
  134. package/src/tx/validator.js +134 -202
  135. package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +1 -3
  136. package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +9 -10
  137. package/src/utils/aepp-wallet-communication/schema.js +1 -3
  138. package/src/utils/amount-formatter.ts +1 -1
  139. package/src/utils/bignumber.ts +2 -1
  140. package/src/utils/crypto.js +4 -17
  141. package/src/utils/other.ts +1 -1
  142. package/src/utils/semver-satisfies.ts +1 -1
  143. package/src/utils/swagger.js +24 -7
  144. package/CHANGELOG.md +0 -1716
  145. package/src/contract/aci/helpers.js +0 -140
package/src/index.js CHANGED
@@ -21,7 +21,6 @@ import * as Bytes from './utils/bytes'
21
21
  import * as TxBuilder from './tx/builder'
22
22
  import * as TxBuilderHelper from './tx/builder/helpers'
23
23
  import * as SCHEMA from './tx/builder/schema'
24
- import * as ACIHelpers from './contract/aci/helpers'
25
24
  import * as ACITransformation from './contract/aci/transformation'
26
25
  import * as AmountFormatter from './utils/amount-formatter'
27
26
  import HdWallet from './utils/hd-wallet'
@@ -33,7 +32,7 @@ import Node from './node'
33
32
  import NodePool from './node-pool'
34
33
  import Tx from './tx'
35
34
  import Transaction from './tx/tx'
36
- import TransactionValidator from './tx/validator'
35
+ import verifyTransaction from './tx/validator'
37
36
  import AccountBase from './account/base'
38
37
  import AccountMultiple from './account/multiple'
39
38
  import MemoryAccount from './account/memory'
@@ -64,7 +63,6 @@ export {
64
63
  Bytes,
65
64
  Contract,
66
65
  ContractCompilerAPI,
67
- ACIHelpers,
68
66
  ACITransformation,
69
67
  ChainNode,
70
68
  RpcAepp,
@@ -81,7 +79,7 @@ export {
81
79
  Oracle,
82
80
  genSwaggerClient,
83
81
  Transaction,
84
- TransactionValidator,
82
+ verifyTransaction,
85
83
  Tx,
86
84
  TxBuilder,
87
85
  TxBuilderHelper,
@@ -5,7 +5,6 @@
5
5
  * @example import { NodePool } from '@aeternity/aepp-sdk'
6
6
  */
7
7
  import stampit from '@stamp/it'
8
- import Joi from 'joi-browser'
9
8
  import { getNetworkId } from '../node'
10
9
 
11
10
  /**
@@ -141,17 +140,27 @@ export default stampit({
141
140
  }))
142
141
  },
143
142
  validateNodes (nodes) {
144
- const { error } = Joi.array().items(
145
- Joi.object({
146
- name: Joi.string().required(),
147
- instance: Joi.object({
148
- api: Joi.object().required(),
149
- consensusProtocolVersion: Joi.number().required(),
150
- genesisHash: Joi.string().required()
151
- }).unknown()
152
- })
153
- ).label('node').validate(nodes)
154
- if (error) throw error
143
+ // TODO: validate it on TypeScript level instead (to speedup development, save runtime resources)
144
+ if (!Array.isArray(nodes)) throw new Error('"nodes" should be an array')
145
+ const notObject = nodes.map(n => typeof n).find(t => t !== 'object')
146
+ if (notObject) throw new Error(`Each node should be an object, got ${notObject} instead`)
147
+ const wrongFields = nodes.find(n => typeof n.name !== 'string' || typeof n.instance !== 'object')
148
+ if (notObject) {
149
+ throw new Error(
150
+ 'Each node should have name (string), instance (object) ' +
151
+ `fields, got ${JSON.stringify(wrongFields)} instead`
152
+ )
153
+ }
154
+ const wrongInstanceFields = nodes
155
+ .map(n => n.instance)
156
+ .find(i => typeof i.api !== 'object' || typeof i.genesisHash !== 'string' ||
157
+ typeof i.consensusProtocolVersion !== 'number')
158
+ if (wrongInstanceFields) {
159
+ throw new Error(
160
+ 'Each node instance should have api (object), consensusProtocolVersion (number), ' +
161
+ `genesisHash (string) fields, got ${JSON.stringify(wrongInstanceFields)} instead`
162
+ )
163
+ }
155
164
  }
156
165
  },
157
166
  props: {
package/src/node.js CHANGED
@@ -39,26 +39,6 @@ export function getNetworkId ({ networkId, force = false } = {}) {
39
39
  return networkId || this.networkId || this.selectedNode.networkId
40
40
  }
41
41
 
42
- /**
43
- * get consensus protocol version
44
- * @param {Array} protocols Array of protocols
45
- * @param {Number} height Height
46
- * @return {Number} version Protocol version
47
- */
48
- async function getConsensusProtocolVersion (protocols = [], height) {
49
- if (!protocols) throw new Error('Protocols must be an array')
50
- if (!height) height = (await this.api.getCurrentKeyBlock()).height
51
- if (height < 0) throw new Error('height must be a number >= 0')
52
-
53
- return protocols
54
- .filter(({ effectiveAtHeight }) => height >= effectiveAtHeight)
55
- .reduce(
56
- (acc, p) => p.effectiveAtHeight > acc.effectiveAtHeight ? p : acc,
57
- { effectiveAtHeight: -1, version: 0 }
58
- )
59
- .version
60
- }
61
-
62
42
  /**
63
43
  * {@link genSwaggerClient} based Node remote API Stamp
64
44
  * @function
@@ -76,7 +56,15 @@ const Node = AsyncInit.compose({
76
56
  if (!url) throw new Error('"url" required')
77
57
  this.url = url.replace(/\/$/, '')
78
58
  this.internalUrl = internalUrl ? internalUrl.replace(/\/$/, '') : this.url
79
- const client = await genSwaggerClient(`${this.url}/api?oas3`, { internalUrl: this.internalUrl })
59
+ const client = await genSwaggerClient(`${this.url}/api?oas3`, {
60
+ internalUrl: this.internalUrl,
61
+ responseInterceptor: response => {
62
+ if (response.ok) return
63
+ return Object.assign(response, {
64
+ statusText: `${new URL(response.url).pathname.slice(1)} error: ` + response.body.reason
65
+ })
66
+ }
67
+ })
80
68
  this.version = client.spec.info.version
81
69
  if (
82
70
  !semverSatisfies(this.version, NODE_GE_VERSION, NODE_LT_VERSION) &&
@@ -89,8 +77,17 @@ const Node = AsyncInit.compose({
89
77
  }
90
78
  this.api = client.api
91
79
 
92
- const { nodeRevision: revision, genesisKeyBlockHash: genesisHash, networkId, protocols } = await this.api.getStatus()
93
- this.consensusProtocolVersion = await this.getConsensusProtocolVersion(protocols)
80
+ const {
81
+ nodeRevision: revision, genesisKeyBlockHash: genesisHash, networkId,
82
+ protocols, topBlockHeight
83
+ } = await this.api.getStatus()
84
+ this.consensusProtocolVersion = protocols
85
+ .filter(({ effectiveAtHeight }) => topBlockHeight >= effectiveAtHeight)
86
+ .reduce(
87
+ (acc, p) => p.effectiveAtHeight > acc.effectiveAtHeight ? p : acc,
88
+ { effectiveAtHeight: -1, version: 0 }
89
+ )
90
+ .version
94
91
  this.nodeNetworkId = networkId
95
92
  return Object.assign(this, { revision, genesisHash })
96
93
  },
@@ -103,8 +100,7 @@ const Node = AsyncInit.compose({
103
100
  version: this.version,
104
101
  consensusProtocolVersion: this.consensusProtocolVersion
105
102
  }
106
- },
107
- getConsensusProtocolVersion
103
+ }
108
104
  },
109
105
  props: {
110
106
  version: null,
@@ -113,7 +109,7 @@ const Node = AsyncInit.compose({
113
109
  }
114
110
  })
115
111
 
116
- const NODE_GE_VERSION = '6.0.0'
112
+ const NODE_GE_VERSION = '6.2.0'
117
113
  const NODE_LT_VERSION = '7.0.0'
118
114
 
119
115
  export default Node
@@ -2,7 +2,6 @@ import * as R from 'ramda'
2
2
  import BigNumber from 'bignumber.js'
3
3
 
4
4
  import {
5
- assertedType,
6
5
  decodeBase58Check,
7
6
  decodeBase64Check,
8
7
  encodeBase58Check, encodeBase64Check,
@@ -18,6 +17,7 @@ import {
18
17
  NAME_FEE,
19
18
  NAME_FEE_BID_INCREMENT,
20
19
  NAME_BID_TIMEOUTS,
20
+ NAME_MAX_LENGTH_FEE,
21
21
  NAME_ID_KEY
22
22
  } from './schema'
23
23
  import { ceil } from '../../utils/bignumber'
@@ -47,20 +47,6 @@ export function buildContractId (ownerId, nonce) {
47
47
  return encode(b2bHash, 'ct')
48
48
  }
49
49
 
50
- /**
51
- * Build hash
52
- * @function
53
- * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
54
- * @param {String} prefix Transaction hash prefix
55
- * @param {Buffer} data Rlp encoded transaction buffer
56
- * @param {Object} options
57
- * @param {Boolean} options.raw
58
- * @return {String} Transaction hash
59
- */
60
- export function buildHash (prefix, data, options = {}) {
61
- return options.raw ? hash(data) : encode(hash(data), prefix)
62
- }
63
-
64
50
  /**
65
51
  * Build a oracle query id
66
52
  * @function
@@ -124,15 +110,17 @@ export function commitmentHash (name, salt = createSalt()) {
124
110
  * Decode data using the default encoding/decoding algorithm
125
111
  * @function
126
112
  * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
127
- * @param {string} data An encoded and prefixed string (ex tx_..., sg_..., ak_....)
128
- * @param {string} type Prefix of Transaction
129
- * @return {Buffer} Buffer of decoded Base58check or Base64check data
113
+ * @param {string} data An Base58check or Base64check encoded and prefixed string (ex tx_..., sg_..., ak_....)
114
+ * @param {string} [requiredPrefix] Ensure that data have this prefix
115
+ * @return {Buffer} Decoded data
130
116
  */
131
- export function decode (data, type = '') {
132
- if (!type) type = data.split('_')[0]
133
- return base64Types.includes(type)
134
- ? decodeBase64Check(assertedType(data, type))
135
- : decodeBase58Check(assertedType(data, type))
117
+ export function decode (data, requiredPrefix) {
118
+ const [prefix, payload, extra] = data.split('_')
119
+ if (extra) throw new Error(`Encoded string have extra parts: ${data}`)
120
+ if (requiredPrefix && requiredPrefix !== prefix) {
121
+ throw new Error(`Encoded string have a wrong type: ${prefix} (expected: ${requiredPrefix})`)
122
+ }
123
+ return (base64Types.includes(prefix) ? decodeBase64Check : decodeBase58Check)(payload)
136
124
  }
137
125
 
138
126
  /**
@@ -301,7 +289,7 @@ export function validatePointers (pointers = []) {
301
289
  */
302
290
  export function getMinimumNameFee (domain) {
303
291
  const nameLength = domain.replace('.chain', '').length
304
- return NAME_BID_RANGES[nameLength >= NAME_BID_MAX_LENGTH ? NAME_BID_MAX_LENGTH : nameLength]
292
+ return NAME_BID_RANGES[nameLength >= NAME_MAX_LENGTH_FEE ? NAME_MAX_LENGTH_FEE : nameLength]
305
293
  }
306
294
 
307
295
  /**
@@ -1,7 +1,7 @@
1
1
  import BigNumber from 'bignumber.js'
2
2
  import { decode as rlpDecode, encode as rlpEncode } from 'rlp'
3
3
  import { AE_AMOUNT_FORMATS, formatAmount } from '../../utils/amount-formatter'
4
- import { assertedType } from '../../utils/crypto'
4
+ import { hash } from '../../utils/crypto'
5
5
 
6
6
  import {
7
7
  DEFAULT_FEE,
@@ -24,8 +24,7 @@ import {
24
24
  writeInt,
25
25
  buildPointers,
26
26
  encode,
27
- decode,
28
- buildHash
27
+ decode
29
28
  } from './helpers'
30
29
  import { toBytes } from '../../utils/bytes'
31
30
  import MPTree from '../../utils/mptree'
@@ -131,6 +130,8 @@ function serializeField (value, type, prefix) {
131
130
  return toBytes(value)
132
131
  case FIELD_TYPES.pointers:
133
132
  return buildPointers(value)
133
+ case FIELD_TYPES.rlpBinary:
134
+ return value.rlpEncoded ?? value
134
135
  case FIELD_TYPES.mptrees:
135
136
  return value.map(t => t.serialize())
136
137
  case FIELD_TYPES.ctVersion:
@@ -159,12 +160,11 @@ function validateField (value, key, type, prefix) {
159
160
  const isMinusValue = (!isNaN(value) || BigNumber.isBigNumber(value)) && BigNumber(value).lt(0)
160
161
  return assert((!isNaN(value) || BigNumber.isBigNumber(value)) && BigNumber(value).gte(0), { value, isMinusValue })
161
162
  }
162
- case FIELD_TYPES.id:
163
- if (Array.isArray(prefix)) {
164
- const p = prefix.find(p => p === value.split('_')[0])
165
- return assert(p && PREFIX_ID_TAG[value.split('_')[0]], { value, prefix })
166
- }
167
- return assert(assertedType(value, prefix) && PREFIX_ID_TAG[value.split('_')[0]] && value.split('_')[0] === prefix, { value, prefix })
163
+ case FIELD_TYPES.id: {
164
+ const prefixes = Array.isArray(prefix) ? prefix : [prefix]
165
+ const p = prefixes.find(p => p === value.split('_')[0])
166
+ return assert(p && PREFIX_ID_TAG[value.split('_')[0]], { value, prefix })
167
+ }
168
168
  case FIELD_TYPES.binary:
169
169
  return assert(value.split('_')[0] === prefix, { prefix, value })
170
170
  case FIELD_TYPES.string:
@@ -253,7 +253,12 @@ function buildFee (txType, { params, gas = 0, multiplier, vsn }) {
253
253
  const { rlpEncoded: txWithOutFee } = buildTx({ ...params }, txType, { vsn })
254
254
  const txSize = txWithOutFee.length
255
255
  return TX_FEE_BASE_GAS(txType)
256
- .plus(TX_FEE_OTHER_GAS(txType)({ txSize, relativeTtl: getOracleRelativeTtl(params, txType) }))
256
+ .plus(TX_FEE_OTHER_GAS(txType, txSize, {
257
+ relativeTtl: getOracleRelativeTtl(params, txType),
258
+ innerTxSize: [TX_TYPE.gaMeta, TX_TYPE.payingFor].includes(txType)
259
+ ? params.tx.tx.encodedTx.rlpEncoded.length
260
+ : 0
261
+ }))
257
262
  .times(multiplier)
258
263
  }
259
264
 
@@ -409,13 +414,11 @@ export function unpackTx (encodedTx, fromRlpBinary = false, prefix = 'tx') {
409
414
  * @function
410
415
  * @alias module:@aeternity/aepp-sdk/es/tx/builder
411
416
  * @param {String | Buffer} rawTx base64 or rlp encoded transaction
412
- * @param {Object} options
413
- * @param {Boolean} options.raw
414
417
  * @return {String} Transaction hash
415
418
  */
416
- export function buildTxHash (rawTx, options) {
417
- if (typeof rawTx === 'string' && rawTx.indexOf('tx_') !== -1) return buildHash('th', unpackTx(rawTx).rlpEncoded, options)
418
- return buildHash('th', rawTx, options)
419
+ export function buildTxHash (rawTx) {
420
+ const data = typeof rawTx === 'string' && rawTx.startsWith('tx_') ? decode(rawTx, 'tx') : rawTx
421
+ return encode(hash(data), 'th')
419
422
  }
420
423
 
421
424
  export default { calculateMinFee, calculateFee, unpackTx, unpackRawTx, buildTx, buildRawTx, validateParams, buildTxHash }
@@ -22,7 +22,7 @@ export const RESPONSE_TTL = { type: 'delta', value: 10 }
22
22
  // # CONTRACT
23
23
  export const DEPOSIT = 0
24
24
  export const AMOUNT = 0
25
- export const GAS = 1600000 - 21000
25
+ export const GAS = 25000
26
26
  export const MIN_GAS_PRICE = 1e9
27
27
  export const MAX_AUTH_FUN_GAS = 50000
28
28
  export const DRY_RUN_ACCOUNT = { pub: 'ak_11111111111111111111111111111111273Yts', amount: '100000000000000000000000000000000000' }
@@ -36,10 +36,11 @@ export const NAME_MAX_CLIENT_TTL = 84600
36
36
  export const CLIENT_TTL = NAME_MAX_CLIENT_TTL
37
37
  export const NAME_FEE = 0
38
38
  // # see https://github.com/aeternity/aeternity/blob/72e440b8731422e335f879a31ecbbee7ac23a1cf/apps/aecore/src/aec_governance.erl#L67
39
- export const NAME_FEE_MULTIPLIER = 100000000000000
39
+ export const NAME_FEE_MULTIPLIER = 1e14 // 100000000000000
40
40
  export const NAME_FEE_BID_INCREMENT = 0.05 // # the increment is in percentage
41
41
  // # see https://github.com/aeternity/aeternity/blob/72e440b8731422e335f879a31ecbbee7ac23a1cf/apps/aecore/src/aec_governance.erl#L272
42
42
  export const NAME_BID_TIMEOUT_BLOCKS = 480 // # ~1 day
43
+ export const NAME_MAX_LENGTH_FEE = 31 // # this is the max length for a domain that requires a base fee to be paid
43
44
  export const NAME_BID_MAX_LENGTH = 12 // # this is the max length for a domain to be part of a bid
44
45
  export const NAME_ID_KEY = {
45
46
  ak: 'account_pubkey',
@@ -47,7 +48,8 @@ export const NAME_ID_KEY = {
47
48
  ct: 'contract_pubkey',
48
49
  ch: 'channel'
49
50
  }
50
- // # ref: https://github.com/aeternity/aeternity/blob/72e440b8731422e335f879a31ecbbee7ac23a1cf/apps/aecore/src/aec_governance.erl#L290
51
+ // # https://github.com/aeternity/aeternity/blob/72e440b8731422e335f879a31ecbbee7ac23a1cf/apps/aecore/src/aec_governance.erl#L290
52
+ // # https://github.com/aeternity/protocol/blob/master/AENS.md#protocol-fees-and-protection-times
51
53
  // # bid ranges:
52
54
  export const NAME_BID_RANGES = {
53
55
  31: BigNumber(3).times(NAME_FEE_MULTIPLIER),
@@ -149,6 +151,7 @@ const OBJECT_TAG_ORACLES_TREE = 625
149
151
  const OBJECT_TAG_ACCOUNTS_TREE = 626
150
152
  const OBJECT_TAG_GA_ATTACH = 80
151
153
  const OBJECT_TAG_GA_META = 81
154
+ const OBJECT_TAG_PAYING_FOR = 82
152
155
  const OBJECT_TAG_SOPHIA_BYTE_CODE = 70
153
156
 
154
157
  const TX_FIELD = (name, type, prefix) => [name, type, prefix]
@@ -225,6 +228,7 @@ export const TX_TYPE = {
225
228
  // GA ACCOUNTS
226
229
  gaAttach: 'gaAttach',
227
230
  gaMeta: 'gaMeta',
231
+ payingFor: 'payingFor',
228
232
  sophiaByteCode: 'sophiaByteCode'
229
233
  }
230
234
 
@@ -324,6 +328,7 @@ export const OBJECT_ID_TX_TYPE = {
324
328
  // GA Accounts
325
329
  [OBJECT_TAG_GA_ATTACH]: TX_TYPE.gaAttach,
326
330
  [OBJECT_TAG_GA_META]: TX_TYPE.gaMeta,
331
+ [OBJECT_TAG_PAYING_FOR]: TX_TYPE.payingFor,
327
332
  [OBJECT_TAG_SOPHIA_BYTE_CODE]: TX_TYPE.sophiaByteCode
328
333
  }
329
334
 
@@ -361,33 +366,39 @@ export const KEY_BLOCK_INTERVAL = 3
361
366
 
362
367
  // MAP WITH FEE CALCULATION https://github.com/aeternity/protocol/blob/master/consensus/consensus.md#gas
363
368
  export const TX_FEE_BASE_GAS = (txType) => {
364
- switch (txType) {
365
- // case TX_TYPE.gaMeta: // TODO investigate MetaTx calculation
366
- case TX_TYPE.gaAttach:
367
- case TX_TYPE.contractCreate:
368
- return BigNumber(5 * BASE_GAS)
369
- // Todo Implement meta tx fee calculation
370
- case TX_TYPE.gaMeta:
371
- case TX_TYPE.contractCall:
372
- return BigNumber(12 * BASE_GAS)
373
- default:
374
- return BigNumber(BASE_GAS)
375
- }
369
+ const factor = ({
370
+ [TX_TYPE.channelForceProgress]: 30,
371
+ [TX_TYPE.channelOffChain]: 0,
372
+ [TX_TYPE.channelOffChainCallContract]: 0,
373
+ [TX_TYPE.channelOffChainCreateContract]: 0,
374
+ [TX_TYPE.channelOffChainUpdateDeposit]: 0,
375
+ [TX_TYPE.channelOffChainUpdateWithdrawal]: 0,
376
+ [TX_TYPE.channelOffChainUpdateTransfer]: 0,
377
+ [TX_TYPE.contractCreate]: 5,
378
+ [TX_TYPE.contractCall]: 12,
379
+ [TX_TYPE.gaAttach]: 5,
380
+ [TX_TYPE.gaMeta]: 5,
381
+ [TX_TYPE.payingFor]: 1 / 5
382
+ })[txType] || 1
383
+ return new BigNumber(factor * BASE_GAS)
376
384
  }
377
385
 
378
- export const TX_FEE_OTHER_GAS = (txType) => ({ txSize, relativeTtl }) => {
386
+ export const TX_FEE_OTHER_GAS = (txType, txSize, { relativeTtl, innerTxSize }) => {
379
387
  switch (txType) {
380
388
  case TX_TYPE.oracleRegister:
381
389
  case TX_TYPE.oracleExtend:
382
390
  case TX_TYPE.oracleQuery:
383
391
  case TX_TYPE.oracleResponse:
384
- return BigNumber(txSize)
392
+ return new BigNumber(txSize)
385
393
  .times(GAS_PER_BYTE)
386
394
  .plus(
387
395
  Math.ceil(32000 * relativeTtl / Math.floor(60 * 24 * 365 / KEY_BLOCK_INTERVAL))
388
396
  )
397
+ case TX_TYPE.gaMeta:
398
+ case TX_TYPE.payingFor:
399
+ return new BigNumber(txSize).minus(innerTxSize).times(GAS_PER_BYTE)
389
400
  default:
390
- return BigNumber(txSize).times(GAS_PER_BYTE)
401
+ return new BigNumber(txSize).times(GAS_PER_BYTE)
391
402
  }
392
403
  }
393
404
 
@@ -547,6 +558,14 @@ const GA_META_TX_2 = [
547
558
  TX_FIELD('tx', FIELD_TYPES.rlpBinary)
548
559
  ]
549
560
 
561
+ const PAYING_FOR_TX = [
562
+ ...BASE_TX,
563
+ TX_FIELD('payerId', FIELD_TYPES.id, 'ak'),
564
+ TX_FIELD('nonce', FIELD_TYPES.int),
565
+ TX_FIELD('fee', FIELD_TYPES.int),
566
+ TX_FIELD('tx', FIELD_TYPES.rlpBinary)
567
+ ]
568
+
550
569
  const CONTRACT_CREATE_TX = [
551
570
  ...BASE_TX,
552
571
  TX_FIELD('ownerId', FIELD_TYPES.id, 'ak'),
@@ -1018,6 +1037,9 @@ export const TX_SERIALIZATION_SCHEMA = {
1018
1037
  },
1019
1038
  [TX_TYPE.gaMeta]: {
1020
1039
  2: TX_SCHEMA_FIELD(GA_META_TX_2, OBJECT_TAG_GA_META)
1040
+ },
1041
+ [TX_TYPE.payingFor]: {
1042
+ 1: TX_SCHEMA_FIELD(PAYING_FOR_TX, OBJECT_TAG_PAYING_FOR)
1021
1043
  }
1022
1044
  }
1023
1045
 
@@ -1154,6 +1176,9 @@ export const TX_DESERIALIZATION_SCHEMA = {
1154
1176
  [OBJECT_TAG_GA_META]: {
1155
1177
  2: TX_SCHEMA_FIELD(GA_META_TX_2, OBJECT_TAG_GA_META)
1156
1178
  },
1179
+ [OBJECT_TAG_PAYING_FOR]: {
1180
+ 1: TX_SCHEMA_FIELD(PAYING_FOR_TX, OBJECT_TAG_PAYING_FOR)
1181
+ },
1157
1182
  [OBJECT_TAG_CHANNEL_FORCE_PROGRESS_TX]: {
1158
1183
  1: TX_SCHEMA_FIELD(CHANNEL_FORCE_PROGRESS_TX, OBJECT_TAG_CHANNEL_FORCE_PROGRESS_TX)
1159
1184
  },
@@ -1161,93 +1186,3 @@ export const TX_DESERIALIZATION_SCHEMA = {
1161
1186
  3: TX_SCHEMA_FIELD(CONTRACT_BYTE_CODE_LIMA, OBJECT_TAG_SOPHIA_BYTE_CODE)
1162
1187
  }
1163
1188
  }
1164
-
1165
- // VERIFICATION SCHEMA
1166
-
1167
- const ERROR_TYPE = { ERROR: 'error', WARNING: 'warning' }
1168
- const VERIFICATION_FIELD = (msg, verificationFn, error) => [msg, verificationFn, error]
1169
-
1170
- const VALIDATORS = {
1171
- signature: 'signature',
1172
- insufficientFee: 'insufficientFee',
1173
- expiredTTL: 'expiredTTL',
1174
- insufficientBalanceForAmountFee: 'insufficientBalanceForAmountFee',
1175
- insufficientBalanceForAmount: 'insufficientBalanceForAmount',
1176
- nonceUsed: 'nonceUsed',
1177
- nonceHigh: 'nonceHigh',
1178
- minGasPrice: 'minGasPrice',
1179
- vmAndAbiVersion: 'vmAndAbiVersion',
1180
- insufficientBalanceForFeeNameFee: 'insufficientBalanceForFeeNameFee'
1181
- }
1182
-
1183
- const ERRORS = {
1184
- invalidSignature: { key: 'InvalidSignature', type: ERROR_TYPE.ERROR, txKey: 'signature' },
1185
- insufficientFee: { key: 'InsufficientFee', type: ERROR_TYPE.ERROR, txKey: 'fee' },
1186
- expiredTTL: { key: 'ExpiredTTL', type: ERROR_TYPE.ERROR, txKey: 'ttl' },
1187
- insufficientBalanceForAmountFee: { key: 'InsufficientBalanceForAmountFee', type: ERROR_TYPE.WARNING, txKey: 'fee' },
1188
- insufficientBalanceForAmount: { key: 'InsufficientBalanceForAmount', type: ERROR_TYPE.WARNING, txKey: 'amount' },
1189
- nonceUsed: { key: 'NonceUsed', type: ERROR_TYPE.ERROR, txKey: 'nonce' },
1190
- nonceHigh: { key: 'NonceHigh', type: ERROR_TYPE.WARNING, txKey: 'nonce' },
1191
- minGasPrice: { key: 'minGasPrice', type: ERROR_TYPE.ERROR, txKey: 'gasPrice' },
1192
- vmAndAbiVersion: { key: 'vmAndAbiVersion', type: ERROR_TYPE.ERROR, txKey: 'ctVersion' },
1193
- insufficientBalanceForFeeNameFee: { key: 'insufficientBalanceForFeeNameFee', type: ERROR_TYPE.ERROR, txKey: 'nameFee' }
1194
- }
1195
-
1196
- export const SIGNATURE_VERIFICATION_SCHEMA = [
1197
- VERIFICATION_FIELD(
1198
- () => 'The signature cannot be verified, please verify that you used the correct network id and the correct private key for the sender address',
1199
- VALIDATORS.signature,
1200
- ERRORS.invalidSignature
1201
- )
1202
- ]
1203
- export const CONTRACT_VERIFICATION_SCHEMA = [
1204
- VERIFICATION_FIELD(
1205
- ({ ctVersion, consensusProtocolVersion, txType }) => `Wrong abi/vm version, Supported is: ${PROTOCOL_VM_ABI[consensusProtocolVersion] ? JSON.stringify(PROTOCOL_VM_ABI[consensusProtocolVersion][txType]) : ' None for this protocol ' + consensusProtocolVersion}`,
1206
- VALIDATORS.vmAndAbiVersion,
1207
- ERRORS.vmAndAbiVersion
1208
- ),
1209
- VERIFICATION_FIELD(
1210
- () => `The gasPrice must be bigger then ${MIN_GAS_PRICE}`,
1211
- VALIDATORS.minGasPrice,
1212
- ERRORS.minGasPrice
1213
- )
1214
- ]
1215
- export const NAME_CLAIM_VERIFICATION_SCHEMA = [
1216
- VERIFICATION_FIELD(
1217
- ({ balance }) => `The account balance ${balance} is not enough to execute the transaction`,
1218
- VALIDATORS.insufficientBalanceForFeeNameFee,
1219
- ERRORS.insufficientBalanceForFeeNameFee
1220
- )
1221
- ]
1222
- export const BASE_VERIFICATION_SCHEMA = [
1223
- VERIFICATION_FIELD(
1224
- ({ minFee }) => `The fee for the transaction is too low, the minimum fee for this transaction is ${minFee}`,
1225
- VALIDATORS.insufficientFee,
1226
- ERRORS.insufficientFee
1227
- ),
1228
- VERIFICATION_FIELD(
1229
- ({ height }) => `The TTL is already expired, the current height is ${height}`,
1230
- VALIDATORS.expiredTTL,
1231
- ERRORS.expiredTTL
1232
- ),
1233
- VERIFICATION_FIELD(
1234
- ({ balance }) => `The account balance ${balance} is not enough to execute the transaction`,
1235
- VALIDATORS.insufficientBalanceForAmountFee,
1236
- ERRORS.insufficientBalanceForAmountFee
1237
- ),
1238
- VERIFICATION_FIELD(
1239
- ({ balance }) => `The account balance ${balance} is not enough to execute the transaction`,
1240
- VALIDATORS.insufficientBalanceForAmount,
1241
- ERRORS.insufficientBalanceForAmount
1242
- ),
1243
- VERIFICATION_FIELD(
1244
- ({ accountNonce }) => `The nonce is invalid (already used). Next valid nonce is ${accountNonce + 1})`,
1245
- VALIDATORS.nonceUsed,
1246
- ERRORS.nonceUsed
1247
- ),
1248
- VERIFICATION_FIELD(
1249
- ({ accountNonce }) => `The nonce is technically valid but will not be processed immediately by the node (next valid nonce is ${accountNonce + 1})`,
1250
- VALIDATORS.nonceHigh,
1251
- ERRORS.nonceHigh
1252
- )
1253
- ]
@@ -5,7 +5,6 @@
5
5
  * @example import TxObject from '@aeternity/aepp-sdk/es/tx/tx-object'
6
6
  */
7
7
  import stampit from '@stamp/it'
8
- import { assertedType } from '../utils/crypto'
9
8
  import { buildTx, calculateFee, unpackTx } from './builder'
10
9
  import { TX_TYPE } from './builder/schema'
11
10
  import { encode } from './builder/helpers'
@@ -38,7 +37,6 @@ const buildTransaction = (type, params, options = {}) => {
38
37
  const unpackTransaction = (tx) => {
39
38
  if (!tx) throw new Error(`Invalid transaction: ${tx}`)
40
39
  if (typeof tx === 'string') {
41
- if (!assertedType(tx, 'tx', true)) throw new Error('Invalid transaction string. Tx should be `tx` prefixed base58c string')
42
40
  const { txType: type, tx: params, rlpEncoded, binary } = unpackTx(tx)
43
41
  return { encodedTx: tx, type, params, rlpEncoded, binary }
44
42
  }