@aeternity/aepp-sdk 8.2.1 → 10.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.
Files changed (159) hide show
  1. package/CHANGELOG.md +216 -337
  2. package/README.md +16 -9
  3. package/dist/aepp-sdk.browser-script.js +1 -1
  4. package/dist/aepp-sdk.browser-script.js.LICENSE.txt +0 -9
  5. package/dist/aepp-sdk.browser-script.js.map +1 -1
  6. package/dist/aepp-sdk.browser.js +1 -1
  7. package/dist/aepp-sdk.browser.js.map +1 -1
  8. package/dist/aepp-sdk.js +1 -1
  9. package/dist/aepp-sdk.js.map +1 -1
  10. package/es/account/base.js +36 -120
  11. package/es/account/base.js.map +1 -1
  12. package/es/account/memory.js +17 -19
  13. package/es/account/memory.js.map +1 -1
  14. package/es/account/multiple.js +79 -156
  15. package/es/account/multiple.js.map +1 -1
  16. package/es/ae/aens.js +205 -626
  17. package/es/ae/aens.js.map +1 -1
  18. package/es/ae/contract.js +114 -918
  19. package/es/ae/contract.js.map +1 -1
  20. package/es/ae/index.js +90 -250
  21. package/es/ae/index.js.map +1 -1
  22. package/es/ae/oracle.js +136 -504
  23. package/es/ae/oracle.js.map +1 -1
  24. package/es/ae/universal.js.map +1 -1
  25. package/es/chain/index.js +12 -27
  26. package/es/chain/index.js.map +1 -1
  27. package/es/chain/node.js +289 -1020
  28. package/es/chain/node.js.map +1 -1
  29. package/es/channel/handlers.js +597 -1086
  30. package/es/channel/handlers.js.map +1 -1
  31. package/es/channel/index.js +238 -465
  32. package/es/channel/index.js.map +1 -1
  33. package/es/channel/internal.js +129 -301
  34. package/es/channel/internal.js.map +1 -1
  35. package/es/contract/aci/index.js +328 -495
  36. package/es/contract/aci/index.js.map +1 -1
  37. package/es/contract/aci/transformation.js +35 -711
  38. package/es/contract/aci/transformation.js.map +1 -1
  39. package/es/contract/compiler.js +131 -203
  40. package/es/contract/compiler.js.map +1 -1
  41. package/es/contract/ga/helpers.js +17 -122
  42. package/es/contract/ga/helpers.js.map +1 -1
  43. package/es/contract/ga/index.js +108 -249
  44. package/es/contract/ga/index.js.map +1 -1
  45. package/es/contract/index.js +7 -3
  46. package/es/contract/index.js.map +1 -1
  47. package/es/index.js +3 -3
  48. package/es/index.js.map +1 -1
  49. package/es/node-pool/index.js +57 -74
  50. package/es/node-pool/index.js.map +1 -1
  51. package/es/node.js +65 -161
  52. package/es/node.js.map +1 -1
  53. package/es/other.d.ts +1 -1
  54. package/es/tx/builder/helpers.js +62 -115
  55. package/es/tx/builder/helpers.js.map +1 -1
  56. package/es/tx/builder/index.js +218 -309
  57. package/es/tx/builder/index.js.map +1 -1
  58. package/es/tx/builder/schema.js +586 -573
  59. package/es/tx/builder/schema.js.map +1 -1
  60. package/es/tx/index.js +102 -13
  61. package/es/tx/index.js.map +1 -1
  62. package/es/tx/tx-object.js +114 -138
  63. package/es/tx/tx-object.js.map +1 -1
  64. package/es/tx/tx.js +810 -1586
  65. package/es/tx/tx.js.map +1 -1
  66. package/es/tx/validator.js +191 -426
  67. package/es/tx/validator.js.map +1 -1
  68. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js +20 -23
  69. package/es/utils/aepp-wallet-communication/connection/browser-runtime.js.map +1 -1
  70. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js +53 -80
  71. package/es/utils/aepp-wallet-communication/connection/browser-window-message.js.map +1 -1
  72. package/es/utils/aepp-wallet-communication/connection/index.js +2 -1
  73. package/es/utils/aepp-wallet-communication/connection/index.js.map +1 -1
  74. package/es/utils/aepp-wallet-communication/content-script-bridge.js +24 -19
  75. package/es/utils/aepp-wallet-communication/content-script-bridge.js.map +1 -1
  76. package/es/utils/aepp-wallet-communication/helpers.js +60 -105
  77. package/es/utils/aepp-wallet-communication/helpers.js.map +1 -1
  78. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js +191 -514
  79. package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +1 -1
  80. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js +78 -93
  81. package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +1 -1
  82. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js +381 -613
  83. package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +1 -1
  84. package/es/utils/aepp-wallet-communication/schema.js +43 -39
  85. package/es/utils/aepp-wallet-communication/schema.js.map +1 -1
  86. package/es/utils/aepp-wallet-communication/wallet-detector.js +32 -58
  87. package/es/utils/aepp-wallet-communication/wallet-detector.js.map +1 -1
  88. package/es/utils/amount-formatter.js +41 -61
  89. package/es/utils/amount-formatter.js.map +1 -1
  90. package/es/utils/async-init.js +28 -64
  91. package/es/utils/async-init.js.map +1 -1
  92. package/es/utils/bignumber.js +3 -11
  93. package/es/utils/bignumber.js.map +1 -1
  94. package/es/utils/bytes.js +5 -9
  95. package/es/utils/bytes.js.map +1 -1
  96. package/es/utils/crypto.js +41 -106
  97. package/es/utils/crypto.js.map +1 -1
  98. package/es/utils/hd-wallet.js +45 -81
  99. package/es/utils/hd-wallet.js.map +1 -1
  100. package/es/utils/json-big.js +16 -49
  101. package/es/utils/json-big.js.map +1 -1
  102. package/es/utils/keystore.js +81 -246
  103. package/es/utils/keystore.js.map +1 -1
  104. package/es/utils/mptree.js +88 -134
  105. package/es/utils/mptree.js.map +1 -1
  106. package/es/utils/other.js +10 -60
  107. package/es/utils/other.js.map +1 -1
  108. package/es/utils/semver-satisfies.js +9 -28
  109. package/es/utils/semver-satisfies.js.map +1 -1
  110. package/es/utils/string.js +4 -13
  111. package/es/utils/string.js.map +1 -1
  112. package/es/utils/swagger.js +87 -224
  113. package/es/utils/swagger.js.map +1 -1
  114. package/package.json +51 -44
  115. package/src/account/base.js +11 -10
  116. package/src/account/multiple.js +24 -18
  117. package/src/ae/aens.js +64 -61
  118. package/src/ae/contract.js +71 -248
  119. package/src/ae/index.js +34 -9
  120. package/src/ae/oracle.js +30 -22
  121. package/src/ae/universal.js +3 -1
  122. package/src/chain/index.js +10 -16
  123. package/src/chain/node.js +38 -38
  124. package/src/channel/handlers.js +10 -4
  125. package/src/channel/index.js +44 -20
  126. package/src/contract/aci/index.js +234 -112
  127. package/src/contract/aci/transformation.js +30 -422
  128. package/src/contract/compiler.js +50 -5
  129. package/src/contract/ga/helpers.js +6 -14
  130. package/src/contract/ga/index.js +28 -28
  131. package/src/contract/index.js +6 -2
  132. package/src/index.js +4 -4
  133. package/src/node-pool/index.js +24 -13
  134. package/src/node.js +29 -30
  135. package/src/tx/builder/helpers.js +31 -56
  136. package/src/tx/builder/index.js +67 -28
  137. package/src/tx/builder/schema.js +79 -121
  138. package/src/tx/index.js +101 -12
  139. package/src/tx/tx-object.js +25 -8
  140. package/src/tx/tx.js +229 -150
  141. package/src/tx/validator.js +134 -202
  142. package/src/utils/aepp-wallet-communication/connection/browser-runtime.js +3 -2
  143. package/src/utils/aepp-wallet-communication/connection/browser-window-message.js +25 -7
  144. package/src/utils/aepp-wallet-communication/connection/index.js +2 -1
  145. package/src/utils/aepp-wallet-communication/content-script-bridge.js +10 -4
  146. package/src/utils/aepp-wallet-communication/helpers.js +4 -2
  147. package/src/utils/aepp-wallet-communication/rpc/aepp-rpc.js +33 -12
  148. package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +9 -5
  149. package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +59 -29
  150. package/src/utils/aepp-wallet-communication/schema.js +1 -3
  151. package/src/utils/aepp-wallet-communication/wallet-detector.js +4 -1
  152. package/src/utils/amount-formatter.ts +1 -1
  153. package/src/utils/bignumber.ts +2 -1
  154. package/src/utils/crypto.js +4 -68
  155. package/src/utils/keystore.js +10 -2
  156. package/src/utils/other.ts +1 -1
  157. package/src/utils/semver-satisfies.ts +1 -1
  158. package/src/utils/swagger.js +22 -10
  159. package/src/contract/aci/helpers.js +0 -140
@@ -22,13 +22,11 @@
22
22
  * @export GeneralizeAccount
23
23
  * @example import { GeneralizeAccount } from '@aeternity/aepp-sdk'
24
24
  */
25
- import * as R from 'ramda'
26
-
27
25
  import { ContractAPI } from '../../ae/contract'
28
26
  import { TX_TYPE } from '../../tx/builder/schema'
29
- import { buildTx } from '../../tx/builder'
30
- import { getContractAuthFan, prepareGaParams, wrapInEmptySignedTx } from './helpers'
31
- import { assertedType, decodeBase64Check } from '../../utils/crypto'
27
+ import { buildTx, unpackTx } from '../../tx/builder'
28
+ import { prepareGaParams } from './helpers'
29
+ import { hash } from '../../utils/crypto'
32
30
 
33
31
  /**
34
32
  * GeneralizeAccount Stamp
@@ -47,7 +45,9 @@ import { assertedType, decodeBase64Check } from '../../utils/crypto'
47
45
  * const callData = 'cb_...' // encoded call data for auth contract
48
46
  * await client.spend(10000, receiverPub, { authData: { callData } })
49
47
  * // or
50
- * await client.spend(10000, receiverPub, { authData: { source: authContract, args: [...authContractArgs] } }) // sdk will prepare callData itself
48
+ * await client.spend(10000, receiverPub, {
49
+ * authData: { source: authContract, args: [...authContractArgs] }
50
+ * }) // sdk will prepare callData itself
51
51
  */
52
52
  export const GeneralizeAccount = ContractAPI.compose({
53
53
  methods: {
@@ -78,29 +78,36 @@ async function isGA (address) {
78
78
  * @param {String} source - Auth contract source code
79
79
  * @param {Array} [args] - init arguments
80
80
  * @param {Object} [options] - Options
81
- * @return {Promise<Readonly<{result: *, owner: *, createdAt: Date, address, rawTx: *, transaction: *}>>}
81
+ * @return {Promise<Readonly<{
82
+ * result: *, owner: *, createdAt: Date, address, rawTx: *, transaction: *
83
+ * }>>}
82
84
  */
83
85
  async function createGeneralizeAccount (authFnName, source, args = [], options = {}) {
84
- const opt = R.merge(this.Ae.defaults, options)
86
+ const opt = { ...this.Ae.defaults, ...options }
85
87
  const ownerId = await this.address(opt)
86
88
 
87
89
  if (await this.isGA(ownerId)) throw new Error(`Account ${ownerId} is already GA`)
88
90
 
89
- const { authFun, bytecode } = await getContractAuthFan(this)(source, authFnName)
90
- const callData = await this.contractEncodeCall(source, 'init', args)
91
-
92
- const { tx, contractId } = await this.gaAttachTx(R.merge(opt, { ownerId, code: bytecode, callData, authFun }))
91
+ const { tx, contractId } = await this.gaAttachTx({
92
+ ...opt,
93
+ ownerId,
94
+ code: (await this.contractCompile(source)).bytecode,
95
+ callData: await this.contractEncodeCallDataAPI(source, 'init', args),
96
+ authFun: hash(authFnName)
97
+ })
93
98
 
94
- const { hash, rawTx } = await this.send(tx, opt)
99
+ const { hash: transaction, rawTx } = await this.send(tx, opt)
95
100
 
96
101
  return Object.freeze({
97
102
  owner: ownerId,
98
- transaction: hash,
103
+ transaction,
99
104
  rawTx,
100
105
  gaContractId: contractId
101
106
  })
102
107
  }
103
108
 
109
+ const wrapInEmptySignedTx = (tx) => buildTx({ encodedTx: tx, signatures: [] }, TX_TYPE.signed)
110
+
104
111
  /**
105
112
  * Create a metaTx transaction
106
113
  * @alias module:@aeternity/aepp-sdk/es/contract/ga
@@ -115,33 +122,26 @@ async function createMetaTx (rawTransaction, authData, authFnName, options = {})
115
122
  if (!authData) throw new Error('authData is required')
116
123
  // Check if authData is callData or if it's an object prepare a callData from source and args
117
124
  const { authCallData, gas } = await prepareGaParams(this)(authData, authFnName)
118
- const opt = R.merge(this.Ae.defaults, options)
119
- // Get transaction rlp binary
120
- const rlpBinaryTx = decodeBase64Check(assertedType(rawTransaction, 'tx'))
121
- // Wrap in SIGNED tx with empty signatures
122
- const { rlpEncoded } = wrapInEmptySignedTx(rlpBinaryTx)
123
- // Get abi
125
+ const opt = { ...this.Ae.defaults, ...options }
124
126
  const { abiVersion } = await this.getVmVersion(TX_TYPE.contractCall)
125
- // Prepare params for META tx
127
+ const wrappedTx = wrapInEmptySignedTx(unpackTx(rawTransaction))
126
128
  const params = {
127
129
  ...opt,
128
- tx: rlpEncoded,
130
+ tx: {
131
+ ...wrappedTx,
132
+ tx: wrappedTx.txObject
133
+ },
129
134
  gaId: await this.address(opt),
130
135
  abiVersion: abiVersion,
131
136
  authData: authCallData,
132
137
  gas,
133
138
  vsn: 2
134
139
  }
135
- // Calculate fee, get absolute ttl (ttl + height), get account nonce
136
140
  const { fee } = await this.prepareTxParams(TX_TYPE.gaMeta, params)
137
- // Build META tx
138
141
  const { rlpEncoded: metaTxRlp } = buildTx(
139
142
  { ...params, fee: `${fee}` },
140
143
  TX_TYPE.gaMeta,
141
144
  { vsn: 2 }
142
145
  )
143
- // Wrap in empty signed tx
144
- const { tx } = wrapInEmptySignedTx(metaTxRlp)
145
- // Send tx to the chain
146
- return tx
146
+ return wrapInEmptySignedTx(metaTxRlp).tx
147
147
  }
@@ -85,7 +85,9 @@ const ContractBase = stampit(required({
85
85
  * @instance
86
86
  * @abstract
87
87
  * @category async
88
- * @rtype (source: String, fn: String, callValue: String, callResult: String, options: Array) => decodedResult: Promise[String]
88
+ * @rtype (
89
+ * source: String, fn: String, callValue: String, callResult: String, options: Array
90
+ * ) => decodedResult: Promise[String]
89
91
  * @param {String} source - Contract source
90
92
  * @param {String} fn - Fn name
91
93
  * @param {String} callValue - result data (cb_das...)
@@ -101,7 +103,9 @@ const ContractBase = stampit(required({
101
103
  * @instance
102
104
  * @abstract
103
105
  * @category async
104
- * @rtype (source: String, function: String, callData: String, options: Array) => decodedResult: Promise[String]
106
+ * @rtype (
107
+ * source: String, function: String, callData: String, options: Array
108
+ * ) => decodedResult: Promise[String]
105
109
  * @param {String} source - contract source
106
110
  * @param {String} function - function name
107
111
  * @param {String} callData - Encoded contract call data
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'
@@ -55,6 +54,8 @@ export { default as WalletDetector } from './utils/aepp-wallet-communication/wal
55
54
  export { default as BrowserRuntimeConnection } from './utils/aepp-wallet-communication/connection/browser-runtime'
56
55
  export { default as BrowserWindowMessageConnection } from './utils/aepp-wallet-communication/connection/browser-window-message'
57
56
 
57
+ export const getDefaultPointerKey = TxBuilderHelper.getDefaultPointerKey
58
+
58
59
  export {
59
60
  AmountFormatter,
60
61
  AccountBase,
@@ -64,7 +65,6 @@ export {
64
65
  Bytes,
65
66
  Contract,
66
67
  ContractCompilerAPI,
67
- ACIHelpers,
68
68
  ACITransformation,
69
69
  ChainNode,
70
70
  RpcAepp,
@@ -81,7 +81,7 @@ export {
81
81
  Oracle,
82
82
  genSwaggerClient,
83
83
  Transaction,
84
- TransactionValidator,
84
+ verifyTransaction,
85
85
  Tx,
86
86
  TxBuilder,
87
87
  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
  /**
@@ -52,7 +51,8 @@ export default stampit({
52
51
  * @param {Boolean} select - Select this node as current
53
52
  * @return {void}
54
53
  * @example
55
- * nodePool.addNode('testNode', awaitNode({ url, internalUrl }), true) // add and select new node with name 'testNode'
54
+ * // add and select new node with name 'testNode'
55
+ * nodePool.addNode('testNode', awaitNode({ url, internalUrl }), true)
56
56
  */
57
57
  addNode (name, node, select = false) {
58
58
  if (this.pool.has(name)) throw new Error(`Node with name ${name} already exist`)
@@ -141,17 +141,28 @@ export default stampit({
141
141
  }))
142
142
  },
143
143
  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
144
+ // TODO: validate it on TypeScript level instead
145
+ // (to speedup development, save runtime resources)
146
+ if (!Array.isArray(nodes)) throw new Error('"nodes" should be an array')
147
+ const notObject = nodes.map(n => typeof n).find(t => t !== 'object')
148
+ if (notObject) throw new Error(`Each node should be an object, got ${notObject} instead`)
149
+ const wrongFields = nodes.find(n => typeof n.name !== 'string' || typeof n.instance !== 'object')
150
+ if (notObject) {
151
+ throw new Error(
152
+ 'Each node should have name (string), instance (object) ' +
153
+ `fields, got ${JSON.stringify(wrongFields)} instead`
154
+ )
155
+ }
156
+ const wrongInstanceFields = nodes
157
+ .map(n => n.instance)
158
+ .find(i => typeof i.api !== 'object' || typeof i.genesisHash !== 'string' ||
159
+ typeof i.consensusProtocolVersion !== 'number')
160
+ if (wrongInstanceFields) {
161
+ throw new Error(
162
+ 'Each node instance should have api (object), consensusProtocolVersion (number), ' +
163
+ `genesisHash (string) fields, got ${JSON.stringify(wrongInstanceFields)} instead`
164
+ )
165
+ }
155
166
  }
156
167
  },
157
168
  props: {
package/src/node.js CHANGED
@@ -34,29 +34,10 @@ import semverSatisfies from './utils/semver-satisfies'
34
34
  * @return {String} NetworkId
35
35
  */
36
36
  export function getNetworkId ({ networkId, force = false } = {}) {
37
- if (!force && !networkId && !this.networkId && (!this.selectedNode || !this.selectedNode.networkId)) throw new Error('networkId is not provided')
38
- if (force && !networkId && !this.networkId && (!this.selectedNode || !this.selectedNode.networkId)) return null
39
- return networkId || this.networkId || this.selectedNode.networkId
40
- }
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
37
+ const res = networkId || this.networkId || this.selectedNode?.networkId
38
+ if (!force && !res) throw new Error('networkId is not provided')
39
+ if (force && !res) return null
40
+ return res
60
41
  }
61
42
 
62
43
  /**
@@ -67,7 +48,9 @@ async function getConsensusProtocolVersion (protocols = [], height) {
67
48
  * @param {Object} [options={}] - Options
68
49
  * @param {String} options.url - Base URL for Node
69
50
  * @param {String} options.internalUrl - Base URL for internal requests
70
- * @param {String} options.axiosConfig - Object with axios configuration. Example { config: {}, errorHandler: (err) => throw err }
51
+ * @param {Object} options.axiosConfig - Object with axios configuration
52
+ * @param {Object} options.axiosConfig.config
53
+ * @param {Function} options.axiosConfig.errorHandler - (err) => throw err
71
54
  * @return {Object} Node client
72
55
  * @example Node({url: 'https://testnet.aeternity.io'})
73
56
  */
@@ -76,7 +59,15 @@ const Node = AsyncInit.compose({
76
59
  if (!url) throw new Error('"url" required')
77
60
  this.url = url.replace(/\/$/, '')
78
61
  this.internalUrl = internalUrl ? internalUrl.replace(/\/$/, '') : this.url
79
- const client = await genSwaggerClient(`${this.url}/api?oas3`, { internalUrl: this.internalUrl })
62
+ const client = await genSwaggerClient(`${this.url}/api?oas3`, {
63
+ internalUrl: this.internalUrl,
64
+ responseInterceptor: response => {
65
+ if (response.ok) return
66
+ return Object.assign(response, {
67
+ statusText: `${new URL(response.url).pathname.slice(1)} error: ` + response.body.reason
68
+ })
69
+ }
70
+ })
80
71
  this.version = client.spec.info.version
81
72
  if (
82
73
  !semverSatisfies(this.version, NODE_GE_VERSION, NODE_LT_VERSION) &&
@@ -89,8 +80,17 @@ const Node = AsyncInit.compose({
89
80
  }
90
81
  this.api = client.api
91
82
 
92
- const { nodeRevision: revision, genesisKeyBlockHash: genesisHash, networkId, protocols } = await this.api.getStatus()
93
- this.consensusProtocolVersion = await this.getConsensusProtocolVersion(protocols)
83
+ const {
84
+ nodeRevision: revision, genesisKeyBlockHash: genesisHash, networkId,
85
+ protocols, topBlockHeight
86
+ } = await this.api.getStatus()
87
+ this.consensusProtocolVersion = protocols
88
+ .filter(({ effectiveAtHeight }) => topBlockHeight >= effectiveAtHeight)
89
+ .reduce(
90
+ (acc, p) => p.effectiveAtHeight > acc.effectiveAtHeight ? p : acc,
91
+ { effectiveAtHeight: -1, version: 0 }
92
+ )
93
+ .version
94
94
  this.nodeNetworkId = networkId
95
95
  return Object.assign(this, { revision, genesisHash })
96
96
  },
@@ -103,8 +103,7 @@ const Node = AsyncInit.compose({
103
103
  version: this.version,
104
104
  consensusProtocolVersion: this.consensusProtocolVersion
105
105
  }
106
- },
107
- getConsensusProtocolVersion
106
+ }
108
107
  },
109
108
  props: {
110
109
  version: null,
@@ -113,7 +112,7 @@ const Node = AsyncInit.compose({
113
112
  }
114
113
  })
115
114
 
116
- const NODE_GE_VERSION = '6.0.0'
115
+ const NODE_GE_VERSION = '6.2.0'
117
116
  const NODE_LT_VERSION = '7.0.0'
118
117
 
119
118
  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,7 +17,8 @@ import {
18
17
  NAME_FEE,
19
18
  NAME_FEE_BID_INCREMENT,
20
19
  NAME_BID_TIMEOUTS,
21
- NAME_ID_KEY
20
+ NAME_MAX_LENGTH_FEE,
21
+ POINTER_KEY_BY_PREFIX
22
22
  } from './schema'
23
23
  import { ceil } from '../../utils/bignumber'
24
24
 
@@ -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,18 @@ 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 Base58/64check 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 (!payload) throw new Error(`Encoded string missing payload: ${data}`)
120
+ if (extra) throw new Error(`Encoded string have extra parts: ${data}`)
121
+ if (requiredPrefix && requiredPrefix !== prefix) {
122
+ throw new Error(`Encoded string have a wrong type: ${prefix} (expected: ${requiredPrefix})`)
123
+ }
124
+ return (base64Types.includes(prefix) ? decodeBase64Check : decodeBase58Check)(payload)
136
125
  }
137
126
 
138
127
  /**
@@ -157,6 +146,9 @@ export function encode (data, type) {
157
146
  * @return {Buffer} Buffer Buffer with ID tag and decoded HASh
158
147
  */
159
148
  export function writeId (hashId) {
149
+ if (typeof hashId !== 'string') {
150
+ throw new Error(`Address should be a string, got ${hashId} instead`)
151
+ }
160
152
  const prefix = hashId.slice(0, 2)
161
153
  const idTag = PREFIX_ID_TAG[prefix]
162
154
  if (!idTag) throw new Error(`Id tag for prefix ${prefix} not found.`)
@@ -203,7 +195,8 @@ export function readInt (buf = Buffer.from([])) {
203
195
  * Helper function to build pointers for name update TX
204
196
  * @function
205
197
  * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
206
- * @param {Array} pointers - Array of pointers ([ { key: 'account_pubkey', id: 'ak_32klj5j23k23j5423l434l2j3423'} ])
198
+ * @param {Array} pointers - Array of pointers
199
+ * ([ { key: 'account_pubkey', id: 'ak_32klj5j23k23j5423l434l2j3423'} ])
207
200
  * @return {Array} Serialized pointers array
208
201
  */
209
202
  export function buildPointers (pointers) {
@@ -219,7 +212,8 @@ export function buildPointers (pointers) {
219
212
  * Helper function to read pointers from name update TX
220
213
  * @function
221
214
  * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
222
- * @param {Array} pointers - Array of pointers ([ { key: 'account_pubkey', id: 'ak_32klj5j23k23j5423l434l2j3423'} ])
215
+ * @param {Array} pointers - Array of pointers
216
+ * ([ { key: 'account_pubkey', id: 'ak_32klj5j23k23j5423l434l2j3423'} ])
223
217
  * @return {Array} Deserialize pointer array
224
218
  */
225
219
  export function readPointers (pointers) {
@@ -261,35 +255,15 @@ export function isNameValid (name) {
261
255
  }
262
256
 
263
257
  /**
264
- * What kind of a hash is this? If it begins with 'ak_' it is an
265
- * account key, if with 'ok_' it's an oracle key.
266
- *
267
- * @param s - the hash.
268
- * returns the type, or throws an exception if type not found.
269
- */
270
- export function classify (s) {
271
- if (!s.match(/^[a-z]{2}_.+/)) {
272
- throw new Error('Not a valid hash')
273
- }
274
-
275
- const klass = s.substr(0, 2)
276
- if (klass in NAME_ID_KEY) {
277
- return NAME_ID_KEY[klass]
278
- } else {
279
- throw new Error(`Unknown class ${klass}`)
280
- }
281
- }
282
-
283
- /**
284
- * Validate name pointers array
285
- * @function
286
- * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
287
- * @param {String[]} pointers Pointers array. Allowed values is: account(ak_), oracle(ok_), contract(ct_), channel(ch_)
288
- * @return {Boolean}
258
+ * @param identifier - account/oracle/contract address, or channel
259
+ * @returns {String} default AENS pointer key
260
+ * @throws exception when default key not defined
289
261
  */
290
- export function validatePointers (pointers = []) {
291
- return !pointers
292
- .find(p => !p || typeof p !== 'string' || !['ak', 'ok', 'ct', 'ch'].includes(p.split('_')[0]))
262
+ export function getDefaultPointerKey (identifier) {
263
+ decode(identifier)
264
+ const prefix = identifier.substr(0, 2)
265
+ return POINTER_KEY_BY_PREFIX[prefix] ||
266
+ (() => { throw new Error(`Default AENS pointer key is not defined for ${prefix} prefix`) })()
293
267
  }
294
268
 
295
269
  /**
@@ -301,7 +275,7 @@ export function validatePointers (pointers = []) {
301
275
  */
302
276
  export function getMinimumNameFee (domain) {
303
277
  const nameLength = domain.replace('.chain', '').length
304
- return NAME_BID_RANGES[nameLength >= NAME_BID_MAX_LENGTH ? NAME_BID_MAX_LENGTH : nameLength]
278
+ return NAME_BID_RANGES[nameLength >= NAME_MAX_LENGTH_FEE ? NAME_MAX_LENGTH_FEE : nameLength]
305
279
  }
306
280
 
307
281
  /**
@@ -317,7 +291,8 @@ export function computeBidFee (domain, startFee = NAME_FEE, increment = NAME_FEE
317
291
  if (!(Number(increment) === increment && increment % 1 !== 0)) throw new Error(`Increment must be float. Current increment ${increment}`)
318
292
  if (increment < NAME_FEE_BID_INCREMENT) throw new Error(`minimum increment percentage is ${NAME_FEE_BID_INCREMENT}`)
319
293
  return ceil(
320
- BigNumber(BigNumber(startFee).eq(NAME_FEE) ? getMinimumNameFee(domain) : startFee).times(BigNumber(NAME_FEE_BID_INCREMENT).plus(1))
294
+ BigNumber(BigNumber(startFee).eq(NAME_FEE) ? getMinimumNameFee(domain) : startFee)
295
+ .times(BigNumber(NAME_FEE_BID_INCREMENT).plus(1))
321
296
  )
322
297
  }
323
298