@aeternity/aepp-sdk 9.0.1 → 11.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.
- package/README.md +15 -14
- package/dist/aepp-sdk.browser-script.js +1 -1
- package/dist/aepp-sdk.browser-script.js.LICENSE.txt +7 -36
- 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.mjs +179 -0
- package/es/account/base.mjs.map +1 -0
- package/es/account/memory.mjs +99 -0
- package/es/account/memory.mjs.map +1 -0
- package/es/account/multiple.mjs +179 -0
- package/es/account/multiple.mjs.map +1 -0
- package/es/ae/aens.mjs +404 -0
- 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.mjs +207 -0
- package/es/ae/index.mjs.map +1 -0
- package/es/ae/oracle.mjs +339 -0
- 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} +29 -9
- package/es/chain/index.mjs.map +1 -0
- package/es/chain/node.mjs +442 -0
- package/es/chain/node.mjs.map +1 -0
- package/es/channel/handlers.mjs +1110 -0
- package/es/channel/handlers.mjs.map +1 -0
- package/es/channel/{index.js → index.mjs} +245 -472
- package/es/channel/index.mjs.map +1 -0
- package/es/channel/internal.mjs +283 -0
- 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.mjs +178 -0
- package/es/contract/ga/index.mjs.map +1 -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.mjs +188 -0
- package/es/node-pool/index.mjs.map +1 -0
- package/es/node.mjs +133 -0
- package/es/node.mjs.map +1 -0
- package/es/other.d.ts +1 -1
- 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 +363 -0
- package/es/tx/builder/helpers.mjs.map +1 -0
- package/es/tx/builder/index.mjs +606 -0
- package/es/tx/builder/index.mjs.map +1 -0
- package/es/tx/builder/schema.mjs +760 -0
- package/es/tx/builder/schema.mjs.map +1 -0
- package/es/tx/{index.js → index.mjs} +107 -15
- package/es/tx/index.mjs.map +1 -0
- package/es/tx/tx-object.mjs +283 -0
- package/es/tx/tx-object.mjs.map +1 -0
- package/es/tx/tx.mjs +938 -0
- package/es/tx/tx.mjs.map +1 -0
- package/es/tx/validator.mjs +247 -0
- 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} +27 -29
- package/es/utils/aepp-wallet-communication/connection/browser-runtime.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/connection/browser-window-message.mjs +183 -0
- 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} +29 -23
- package/es/utils/aepp-wallet-communication/content-script-bridge.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/helpers.mjs +92 -0
- package/es/utils/aepp-wallet-communication/helpers.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs +375 -0
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs +275 -0
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.mjs.map +1 -0
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.mjs +541 -0
- 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.mjs +110 -0
- package/es/utils/aepp-wallet-communication/wallet-detector.mjs.map +1 -0
- package/es/utils/amount-formatter.mjs +139 -0
- package/es/utils/amount-formatter.mjs.map +1 -0
- package/es/utils/async-init.mjs +64 -0
- package/es/utils/async-init.mjs.map +1 -0
- package/es/utils/bignumber.mjs +21 -0
- package/es/utils/bignumber.mjs.map +1 -0
- package/es/utils/{bytes.js → bytes.mjs} +16 -12
- 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.mjs +239 -0
- package/es/utils/crypto.mjs.map +1 -0
- package/es/utils/errors.mjs +689 -0
- package/es/utils/errors.mjs.map +1 -0
- package/es/utils/hd-wallet.mjs +111 -0
- package/es/utils/hd-wallet.mjs.map +1 -0
- package/es/utils/json-big.mjs +45 -0
- package/es/utils/json-big.mjs.map +1 -0
- package/es/utils/keystore.mjs +240 -0
- package/es/utils/keystore.mjs.map +1 -0
- package/es/utils/mptree.mjs +161 -0
- package/es/utils/mptree.mjs.map +1 -0
- package/es/utils/other.mjs +43 -0
- package/es/utils/other.mjs.map +1 -0
- package/es/utils/semver-satisfies.mjs +16 -0
- package/es/utils/semver-satisfies.mjs.map +1 -0
- package/es/utils/{string.js → string.mjs} +5 -14
- package/es/utils/string.mjs.map +1 -0
- package/es/utils/swagger.mjs +164 -0
- package/es/utils/swagger.mjs.map +1 -0
- package/package.json +55 -62
- package/src/account/base.js +3 -1
- package/src/account/memory.js +13 -8
- package/src/account/multiple.js +31 -22
- package/src/ae/aens.js +75 -77
- package/src/ae/aepp.js +2 -2
- package/src/ae/contract.js +27 -291
- package/src/ae/index.js +9 -7
- package/src/ae/oracle.js +51 -28
- 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 -67
- package/src/channel/handlers.js +30 -21
- package/src/channel/index.js +44 -20
- package/src/channel/internal.js +30 -24
- package/src/contract/aci.js +376 -0
- package/src/contract/compiler.js +41 -91
- package/src/contract/ga/helpers.js +10 -13
- package/src/contract/ga/index.js +42 -33
- package/src/index.js +9 -10
- package/src/node-pool/index.js +29 -17
- package/src/node.js +33 -36
- package/src/tx/builder/field-types.js +61 -0
- package/src/tx/builder/helpers.js +126 -84
- package/src/tx/builder/index.js +83 -41
- package/src/tx/builder/schema.js +50 -40
- package/src/tx/index.js +101 -12
- package/src/tx/tx-object.js +35 -15
- package/src/tx/tx.js +232 -153
- package/src/tx/validator.js +34 -12
- 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 +107 -60
- package/src/utils/aepp-wallet-communication/rpc/rpc-client.js +24 -11
- package/src/utils/aepp-wallet-communication/rpc/wallet-rpc.js +98 -56
- 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 +3 -10
- package/src/utils/bytes.ts +3 -2
- package/src/utils/crypto-ts.ts +2 -2
- package/src/utils/crypto.js +10 -144
- 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/other.ts +1 -1
- package/src/utils/semver-satisfies.ts +1 -1
- package/src/utils/swagger.js +75 -45
- package/es/account/base.js +0 -259
- package/es/account/base.js.map +0 -1
- package/es/account/memory.js +0 -86
- package/es/account/memory.js.map +0 -1
- package/es/account/multiple.js +0 -267
- package/es/account/multiple.js.map +0 -1
- package/es/ae/aens.js +0 -829
- package/es/ae/aens.js.map +0 -1
- package/es/ae/aepp.js.map +0 -1
- package/es/ae/contract.js +0 -985
- package/es/ae/contract.js.map +0 -1
- package/es/ae/index.js +0 -442
- package/es/ae/index.js.map +0 -1
- package/es/ae/oracle.js +0 -690
- 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 +0 -1119
- package/es/chain/node.js.map +0 -1
- package/es/channel/handlers.js +0 -1601
- package/es/channel/handlers.js.map +0 -1
- package/es/channel/index.js.map +0 -1
- package/es/channel/internal.js +0 -438
- 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 -517
- package/es/contract/aci/index.js.map +0 -1
- package/es/contract/aci/transformation.js +0 -742
- package/es/contract/aci/transformation.js.map +0 -1
- package/es/contract/compiler.js +0 -316
- package/es/contract/compiler.js.map +0 -1
- package/es/contract/ga/helpers.js +0 -120
- package/es/contract/ga/helpers.js.map +0 -1
- package/es/contract/ga/index.js +0 -307
- 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 -57
- package/es/index.js.map +0 -1
- package/es/node-pool/index.js +0 -204
- package/es/node-pool/index.js.map +0 -1
- package/es/node.js +0 -228
- package/es/node.js.map +0 -1
- package/es/tx/builder/helpers.js +0 -355
- package/es/tx/builder/helpers.js.map +0 -1
- package/es/tx/builder/index.js +0 -686
- package/es/tx/builder/index.js.map +0 -1
- package/es/tx/builder/schema.js +0 -695
- package/es/tx/builder/schema.js.map +0 -1
- package/es/tx/index.js.map +0 -1
- package/es/tx/tx-object.js +0 -297
- package/es/tx/tx-object.js.map +0 -1
- package/es/tx/tx.js +0 -1757
- package/es/tx/tx.js.map +0 -1
- package/es/tx/validator.js +0 -344
- 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 +0 -211
- 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 +0 -148
- package/es/utils/aepp-wallet-communication/helpers.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js +0 -652
- package/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.js +0 -285
- package/es/utils/aepp-wallet-communication/rpc/rpc-client.js.map +0 -1
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js +0 -763
- package/es/utils/aepp-wallet-communication/rpc/wallet-rpc.js.map +0 -1
- package/es/utils/aepp-wallet-communication/schema.js +0 -103
- package/es/utils/aepp-wallet-communication/schema.js.map +0 -1
- package/es/utils/aepp-wallet-communication/wallet-detector.js +0 -135
- package/es/utils/aepp-wallet-communication/wallet-detector.js.map +0 -1
- package/es/utils/amount-formatter.js +0 -152
- package/es/utils/amount-formatter.js.map +0 -1
- package/es/utils/async-init.js +0 -103
- package/es/utils/async-init.js.map +0 -1
- package/es/utils/bignumber.js +0 -36
- 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 +0 -358
- package/es/utils/crypto.js.map +0 -1
- package/es/utils/hd-wallet.js +0 -148
- package/es/utils/hd-wallet.js.map +0 -1
- package/es/utils/json-big.js +0 -78
- package/es/utils/json-big.js.map +0 -1
- package/es/utils/keystore.js +0 -393
- package/es/utils/keystore.js.map +0 -1
- package/es/utils/mptree.js +0 -198
- package/es/utils/mptree.js.map +0 -1
- package/es/utils/other.js +0 -93
- package/es/utils/other.js.map +0 -1
- package/es/utils/semver-satisfies.js +0 -35
- package/es/utils/semver-satisfies.js.map +0 -1
- package/es/utils/string.js.map +0 -1
- package/es/utils/swagger.js +0 -269
- package/es/utils/swagger.js.map +0 -1
- package/src/contract/aci/helpers.js +0 -140
- package/src/contract/aci/index.js +0 -191
- package/src/contract/aci/transformation.js +0 -464
- package/src/contract/index.js +0 -159
- package/src/utils/aepp-wallet-communication/schema.js +0 -110
package/src/tx/validator.js
CHANGED
|
@@ -4,6 +4,7 @@ import { encode, decode } from './builder/helpers'
|
|
|
4
4
|
import BigNumber from 'bignumber.js'
|
|
5
5
|
import { MIN_GAS_PRICE, PROTOCOL_VM_ABI, TX_TYPE } from './builder/schema'
|
|
6
6
|
import { calculateFee, unpackTx } from './builder'
|
|
7
|
+
import { UnsupportedProtocolError } from '../utils/errors'
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Transaction validator
|
|
@@ -13,7 +14,7 @@ import { calculateFee, unpackTx } from './builder'
|
|
|
13
14
|
*/
|
|
14
15
|
|
|
15
16
|
const validators = [
|
|
16
|
-
|
|
17
|
+
({ encodedTx, signatures }, { account, node, parentTxTypes }) => {
|
|
17
18
|
if ((encodedTx ?? signatures) === undefined) return []
|
|
18
19
|
if (signatures.length !== 1) return [] // TODO: Support multisignature?
|
|
19
20
|
const prefix = Buffer.from([
|
|
@@ -32,7 +33,7 @@ const validators = [
|
|
|
32
33
|
checkedKeys: ['encodedTx', 'signatures']
|
|
33
34
|
}]
|
|
34
35
|
},
|
|
35
|
-
|
|
36
|
+
({ encodedTx, tx }, { node, parentTxTypes, txType }) => {
|
|
36
37
|
if ((encodedTx ?? tx) === undefined) return []
|
|
37
38
|
return verifyTransaction(
|
|
38
39
|
encode((encodedTx ?? tx).rlpEncoded, 'tx'),
|
|
@@ -80,13 +81,15 @@ const validators = [
|
|
|
80
81
|
const validNonce = account.nonce + 1
|
|
81
82
|
if (nonce === validNonce) return []
|
|
82
83
|
return [{
|
|
83
|
-
...nonce < validNonce
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
84
|
+
...nonce < validNonce
|
|
85
|
+
? {
|
|
86
|
+
message: `Nonce ${nonce} is already used, valid nonce is ${validNonce}`,
|
|
87
|
+
key: 'NonceAlreadyUsed'
|
|
88
|
+
}
|
|
89
|
+
: {
|
|
90
|
+
message: `Nonce ${nonce} is too high, valid nonce is ${validNonce}`,
|
|
91
|
+
key: 'NonceHigh'
|
|
92
|
+
},
|
|
90
93
|
checkedKeys: ['nonce']
|
|
91
94
|
}]
|
|
92
95
|
},
|
|
@@ -102,7 +105,7 @@ const validators = [
|
|
|
102
105
|
({ ctVersion, abiVersion }, { txType, node }) => {
|
|
103
106
|
const { consensusProtocolVersion } = node.getNodeInfo()
|
|
104
107
|
const protocol = PROTOCOL_VM_ABI[consensusProtocolVersion]
|
|
105
|
-
if (!protocol) throw new
|
|
108
|
+
if (!protocol) throw new UnsupportedProtocolError(`Unsupported protocol: ${consensusProtocolVersion}`)
|
|
106
109
|
// If not contract create tx
|
|
107
110
|
if (!ctVersion) ctVersion = { abiVersion }
|
|
108
111
|
const txProtocol = protocol[txType]
|
|
@@ -115,6 +118,25 @@ const validators = [
|
|
|
115
118
|
}]
|
|
116
119
|
}
|
|
117
120
|
return []
|
|
121
|
+
},
|
|
122
|
+
async ({ contractId }, { txType, node }) => {
|
|
123
|
+
if (TX_TYPE.contractCall !== txType) return []
|
|
124
|
+
try {
|
|
125
|
+
const { active } = await node.api.getContract(contractId)
|
|
126
|
+
if (active) return []
|
|
127
|
+
return [{
|
|
128
|
+
message: `Contract ${contractId} is not active`,
|
|
129
|
+
key: 'ContractNotActive',
|
|
130
|
+
checkedKeys: ['contractId']
|
|
131
|
+
}]
|
|
132
|
+
} catch (error) {
|
|
133
|
+
if (!error.response?.body?.reason) throw error
|
|
134
|
+
return [{
|
|
135
|
+
message: error.response.body.reason,
|
|
136
|
+
key: 'ContractNotFound',
|
|
137
|
+
checkedKeys: ['contractId']
|
|
138
|
+
}]
|
|
139
|
+
}
|
|
118
140
|
}
|
|
119
141
|
]
|
|
120
142
|
|
|
@@ -144,13 +166,13 @@ export default async function verifyTransaction (transaction, node, parentTxType
|
|
|
144
166
|
|
|
145
167
|
const address = getSenderAddress(tx) ??
|
|
146
168
|
(txType === TX_TYPE.signed ? getSenderAddress(tx.encodedTx.tx) : null)
|
|
147
|
-
const [account, height] = await Promise.all([
|
|
169
|
+
const [account, { height }] = await Promise.all([
|
|
148
170
|
address && node.api.getAccountByPubkey(address).catch(() => ({
|
|
149
171
|
id: address,
|
|
150
172
|
balance: new BigNumber(0),
|
|
151
173
|
nonce: 0
|
|
152
174
|
})),
|
|
153
|
-
node.api.getCurrentKeyBlockHeight()
|
|
175
|
+
node.api.getCurrentKeyBlockHeight()
|
|
154
176
|
])
|
|
155
177
|
|
|
156
178
|
return (await Promise.all(
|
|
@@ -18,15 +18,21 @@
|
|
|
18
18
|
/**
|
|
19
19
|
* Browser runtime connector module
|
|
20
20
|
*
|
|
21
|
-
* This is the complement to
|
|
21
|
+
* This is the complement to
|
|
22
|
+
* {@link module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection}.
|
|
22
23
|
* @module @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection/browser-runtime
|
|
23
24
|
* @export BrowserRuntimeConnection
|
|
24
|
-
* @example import BrowserRuntimeConnection from '@aeternity/aepp-sdk
|
|
25
|
+
* @example import { BrowserRuntimeConnection } from '@aeternity/aepp-sdk'
|
|
25
26
|
*/
|
|
26
27
|
import stampit from '@stamp/it'
|
|
27
28
|
|
|
28
29
|
import WalletConnection from '.'
|
|
29
30
|
import { getBrowserAPI } from '../helpers'
|
|
31
|
+
import {
|
|
32
|
+
RpcConnectionError,
|
|
33
|
+
AlreadyConnectedError,
|
|
34
|
+
NoWalletConnectedError
|
|
35
|
+
} from '../../errors'
|
|
30
36
|
|
|
31
37
|
/**
|
|
32
38
|
* Disconnect
|
|
@@ -53,7 +59,7 @@ function disconnect () {
|
|
|
53
59
|
* @return {void}
|
|
54
60
|
*/
|
|
55
61
|
function connect (onMessage, onDisconnect) {
|
|
56
|
-
if (this.isConnected()) throw new
|
|
62
|
+
if (this.isConnected()) throw new AlreadyConnectedError('You already connected')
|
|
57
63
|
this.handler = (msg, source) => {
|
|
58
64
|
if (this.debug) console.log('Receive message: ', msg)
|
|
59
65
|
onMessage(msg, source)
|
|
@@ -74,7 +80,7 @@ function connect (onMessage, onDisconnect) {
|
|
|
74
80
|
* @return {void}
|
|
75
81
|
*/
|
|
76
82
|
function sendMessage (msg) {
|
|
77
|
-
if (!this.port) throw new
|
|
83
|
+
if (!this.port) throw new NoWalletConnectedError('You dont have connection. Please connect before')
|
|
78
84
|
if (this.debug) console.log('Send message: ', msg)
|
|
79
85
|
this.port.postMessage(msg)
|
|
80
86
|
}
|
|
@@ -104,7 +110,7 @@ function isConnected () {
|
|
|
104
110
|
*/
|
|
105
111
|
export default stampit({
|
|
106
112
|
init ({ connectionInfo = {}, port, debug = false }) {
|
|
107
|
-
if (!getBrowserAPI().runtime) throw new
|
|
113
|
+
if (!getBrowserAPI().runtime) throw new RpcConnectionError('Runtime is not accessible in your environment')
|
|
108
114
|
this.debug = debug
|
|
109
115
|
this.connectionInfo = connectionInfo
|
|
110
116
|
this.port = port || getBrowserAPI().runtime.connect(...[connectionInfo.id || undefined])
|
|
@@ -18,16 +18,24 @@
|
|
|
18
18
|
/**
|
|
19
19
|
* Browser window Post Message connector module
|
|
20
20
|
*
|
|
21
|
-
* This is the complement to
|
|
21
|
+
* This is the complement to
|
|
22
|
+
* {@link module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection}.
|
|
22
23
|
* @module @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection/browser-window-message
|
|
23
24
|
* @export BrowserWindowMessageConnection
|
|
24
|
-
* @example
|
|
25
|
+
* @example
|
|
26
|
+
* import BrowserWindowMessageConnection
|
|
27
|
+
* from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection/browser-window-message'
|
|
25
28
|
*/
|
|
26
29
|
import stampit from '@stamp/it'
|
|
27
30
|
import WalletConnection from '.'
|
|
28
|
-
import { v4 as uuid } from 'uuid'
|
|
31
|
+
import { v4 as uuid } from '@aeternity/uuid'
|
|
29
32
|
import { MESSAGE_DIRECTION } from '../schema'
|
|
30
33
|
import { getBrowserAPI, isInIframe } from '../helpers'
|
|
34
|
+
import {
|
|
35
|
+
AlreadyConnectedError,
|
|
36
|
+
NoWalletConnectedError,
|
|
37
|
+
MessageDirectionError
|
|
38
|
+
} from '../../errors'
|
|
31
39
|
|
|
32
40
|
/**
|
|
33
41
|
* Check if connected
|
|
@@ -53,7 +61,7 @@ function connect (onMessage) {
|
|
|
53
61
|
const receiveDirection = this.receiveDirection
|
|
54
62
|
const debug = this.debug
|
|
55
63
|
const forceOrigin = this.forceOrigin
|
|
56
|
-
if (this.listener) throw new
|
|
64
|
+
if (this.listener) throw new AlreadyConnectedError('You already connected')
|
|
57
65
|
|
|
58
66
|
this.listener = (msg, source) => {
|
|
59
67
|
if (!msg || typeof msg.data !== 'object') return
|
|
@@ -77,7 +85,7 @@ function connect (onMessage) {
|
|
|
77
85
|
* @return {void}
|
|
78
86
|
*/
|
|
79
87
|
function disconnect () {
|
|
80
|
-
if (!this.listener) throw new
|
|
88
|
+
if (!this.listener) throw new NoWalletConnectedError('You dont have connection. Please connect before')
|
|
81
89
|
this.unsubscribeFn(this.listener)
|
|
82
90
|
this.listener = null
|
|
83
91
|
}
|
|
@@ -102,29 +110,44 @@ const getTarget = () => {
|
|
|
102
110
|
const isContentScript = isExtensionContext && isWeb
|
|
103
111
|
if (isContentScript) return window
|
|
104
112
|
// When we is the main page we need to decide the target by our self
|
|
105
|
-
// Probably can be implemented some algo for checking DOM for Iframes and somehow decide which
|
|
113
|
+
// Probably can be implemented some algo for checking DOM for Iframes and somehow decide which
|
|
114
|
+
// Iframe to talk
|
|
106
115
|
return isInIframe() ? window.parent : undefined
|
|
107
116
|
}
|
|
108
117
|
|
|
109
118
|
/**
|
|
110
119
|
* BrowserWindowMessageConnection
|
|
111
120
|
* @function
|
|
112
|
-
* @alias module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication
|
|
121
|
+
* @alias module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication\
|
|
122
|
+
* /connection/browser-window-message
|
|
113
123
|
* @rtype Stamp
|
|
114
124
|
* @param {Object} [params={}] - Initializer object
|
|
115
125
|
* @param {Object} [params.target=window.parent] - Target window for message
|
|
116
126
|
* @param {Object} [params.self=window] - Host window for message
|
|
117
127
|
* @param {Object} [params.origin] - Origin of receiver
|
|
118
|
-
* @param {
|
|
119
|
-
*
|
|
128
|
+
* @param {String} [params.sendDirection] Wrapping messages into additional struct
|
|
129
|
+
* ({ type: 'to_aepp' || 'to_waellet', data })
|
|
130
|
+
* Used for handling messages between content script and page
|
|
131
|
+
* @param {String} [params.receiveDirection='to_aepp'] Unwrapping messages from additional struct
|
|
132
|
+
* ({ type: 'to_aepp' || 'to_waellet', data })
|
|
133
|
+
* Used for handling messages between content script and page
|
|
120
134
|
* @param {Object} [params.connectionInfo={}] - Connection info object
|
|
121
135
|
* @param {Boolean} [params.debug=false] - Debug flag
|
|
122
136
|
* @return {Object}
|
|
123
137
|
*/
|
|
124
138
|
export default stampit({
|
|
125
|
-
init ({
|
|
126
|
-
|
|
127
|
-
|
|
139
|
+
init ({
|
|
140
|
+
connectionInfo = {},
|
|
141
|
+
target = getTarget(),
|
|
142
|
+
self = window,
|
|
143
|
+
origin,
|
|
144
|
+
sendDirection,
|
|
145
|
+
receiveDirection = MESSAGE_DIRECTION.to_aepp,
|
|
146
|
+
debug = false,
|
|
147
|
+
forceOrigin = false
|
|
148
|
+
} = {}) {
|
|
149
|
+
if (sendDirection && !Object.keys(MESSAGE_DIRECTION).includes(sendDirection)) throw new MessageDirectionError(`sendDirection must be one of [${Object.keys(MESSAGE_DIRECTION)}]`)
|
|
150
|
+
if (!Object.keys(MESSAGE_DIRECTION).includes(receiveDirection)) throw new MessageDirectionError(`receiveDirection must be one of [${Object.keys(MESSAGE_DIRECTION)}]`)
|
|
128
151
|
this.connectionInfo = { id: uuid(), ...connectionInfo }
|
|
129
152
|
|
|
130
153
|
const selfP = self
|
|
@@ -137,7 +160,6 @@ export default stampit({
|
|
|
137
160
|
this.subscribeFn = (listener) => selfP.addEventListener('message', listener, false)
|
|
138
161
|
this.unsubscribeFn = (listener) => selfP.removeEventListener('message', listener, false)
|
|
139
162
|
this.postFn = (msg) => targetP.postMessage(msg, this.origin || '*')
|
|
140
|
-
if (!this.connectionInfo.id) throw new Error('ID required.')
|
|
141
163
|
},
|
|
142
164
|
methods: { connect, sendMessage, disconnect, isConnected }
|
|
143
165
|
}, WalletConnection)
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
* Wallet Connection base module
|
|
20
20
|
* @module @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection
|
|
21
21
|
* @export WalletConnection
|
|
22
|
-
* @example
|
|
22
|
+
* @example
|
|
23
|
+
* import WalletConnection from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection'
|
|
23
24
|
*/
|
|
24
25
|
|
|
25
26
|
import { required } from '@stamp/required'
|
|
@@ -20,9 +20,12 @@
|
|
|
20
20
|
*
|
|
21
21
|
* @module @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/content-script-bridge
|
|
22
22
|
* @export ContentScriptBridge
|
|
23
|
-
* @example
|
|
23
|
+
* @example
|
|
24
|
+
* import ContentScriptBridge
|
|
25
|
+
* from '@aeternity/aepp-sdk/es/utils/wallet-communication/content-script-bridge
|
|
24
26
|
*/
|
|
25
27
|
import stampit from '@stamp/it'
|
|
28
|
+
import { UnsupportedPlatformError, MissingParamError } from '../errors'
|
|
26
29
|
|
|
27
30
|
/**
|
|
28
31
|
* Start message proxy
|
|
@@ -56,21 +59,25 @@ function stop () {
|
|
|
56
59
|
|
|
57
60
|
/**
|
|
58
61
|
* ContentScriptBridge stamp
|
|
59
|
-
* Provide functionality to easly redirect messages from page to extension and from extension to
|
|
62
|
+
* Provide functionality to easly redirect messages from page to extension and from extension to
|
|
63
|
+
* page through content script
|
|
60
64
|
* Using Runtime(Extension) and WindowPostMessage(Web-Page) connections
|
|
61
65
|
* @function
|
|
62
66
|
* @alias module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/content-script-bridge
|
|
63
67
|
* @rtype Stamp
|
|
64
68
|
* @param {Object} params - Initializer object
|
|
65
|
-
* @param {Object} params.pageConnection - Page connection object
|
|
66
|
-
* @
|
|
69
|
+
* @param {Object} params.pageConnection - Page connection object
|
|
70
|
+
* (@link module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection/
|
|
71
|
+
* browser-window-message)
|
|
72
|
+
* @param {Object} params.extConnection - Extension connection object
|
|
73
|
+
* (@link module:@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/connection/browser-runtime)
|
|
67
74
|
* @return {Object}
|
|
68
75
|
*/
|
|
69
76
|
export default stampit({
|
|
70
77
|
init ({ pageConnection, extConnection, allowCrossOrigin = false }) {
|
|
71
|
-
if (!window) throw new
|
|
72
|
-
if (!pageConnection) throw new
|
|
73
|
-
if (!extConnection) throw new
|
|
78
|
+
if (!window) throw new UnsupportedPlatformError('Window object not found, you can run bridge only in browser')
|
|
79
|
+
if (!pageConnection) throw new MissingParamError('pageConnection required')
|
|
80
|
+
if (!extConnection) throw new MissingParamError('extConnection required')
|
|
74
81
|
this.allowCrossOrigin = allowCrossOrigin
|
|
75
82
|
this.pageConnection = pageConnection
|
|
76
83
|
this.extConnection = extConnection
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Browser helper functions
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import { NoBrowserFoundError } from '../errors'
|
|
5
5
|
|
|
6
6
|
export const getBrowserAPI = (force = false) => {
|
|
7
7
|
const { chrome, browser } = window
|
|
@@ -9,7 +9,7 @@ export const getBrowserAPI = (force = false) => {
|
|
|
9
9
|
if (typeof chrome !== 'undefined' && chrome === Object(chrome)) return chrome
|
|
10
10
|
// Firefox support
|
|
11
11
|
if (typeof browser !== 'undefined' && browser === Object(browser)) return browser
|
|
12
|
-
if (!force) throw new
|
|
12
|
+
if (!force) throw new NoBrowserFoundError()
|
|
13
13
|
return {}
|
|
14
14
|
}
|
|
15
15
|
|
|
@@ -51,18 +51,10 @@ export const getHandler = (schema, msg, { debug = false } = {}) => {
|
|
|
51
51
|
|
|
52
52
|
export const message = (method, params = {}) => ({ method, params })
|
|
53
53
|
|
|
54
|
-
export const responseMessage = (id, method, { error, result } = {}) =>
|
|
54
|
+
export const responseMessage = (id, method, { error, result } = {}) =>
|
|
55
|
+
({ id, method, ...(error ? { error } : { result }) })
|
|
55
56
|
|
|
56
|
-
export const sendResponseMessage = (client) => (id, method, data) =>
|
|
57
|
-
|
|
58
|
-
export const voidFn = () => undefined
|
|
57
|
+
export const sendResponseMessage = (client) => (id, method, data) =>
|
|
58
|
+
client.sendMessage(responseMessage(id, method, data), true)
|
|
59
59
|
|
|
60
60
|
export const isValidAccounts = (accounts) => (['', 'connected', 'current'].filter(k => typeof (k ? accounts[k] : accounts) === 'object')).length === 3
|
|
61
|
-
|
|
62
|
-
export const resolveOnAccount = (addresses, onAccount, opt = {}) => {
|
|
63
|
-
if (!addresses.find(a => a === onAccount)) {
|
|
64
|
-
if (typeof opt.onAccount !== 'object' || !isAccountBase(opt.onAccount)) throw new Error('Provided onAccount should be an AccountBase')
|
|
65
|
-
onAccount = opt.onAccount
|
|
66
|
-
}
|
|
67
|
-
return onAccount
|
|
68
|
-
}
|
|
@@ -3,26 +3,37 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module @aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/aepp-rpc
|
|
5
5
|
* @export AeppRpc
|
|
6
|
-
* @example
|
|
6
|
+
* @example
|
|
7
|
+
* import ContentScriptBridge
|
|
8
|
+
* from '@aeternity/aepp-sdk/es/utils/aepp-wallet-communication/rpc/aepp-rpc'
|
|
7
9
|
*/
|
|
8
|
-
import
|
|
9
|
-
import { v4 as uuid } from 'uuid'
|
|
10
|
-
|
|
10
|
+
import { v4 as uuid } from '@aeternity/uuid'
|
|
11
11
|
import Ae from '../../../ae'
|
|
12
12
|
import RpcClient from './rpc-client'
|
|
13
|
-
import { getHandler, message
|
|
13
|
+
import { getHandler, message } from '../helpers'
|
|
14
14
|
import { METHODS, RPC_STATUS, VERSION } from '../schema'
|
|
15
|
+
import {
|
|
16
|
+
AlreadyConnectedError,
|
|
17
|
+
NoWalletConnectedError,
|
|
18
|
+
UnsubscribedAccountError,
|
|
19
|
+
UnAuthorizedAccountError,
|
|
20
|
+
ArgumentError,
|
|
21
|
+
RpcConnectionError
|
|
22
|
+
} from '../../errors'
|
|
23
|
+
import { Node } from '../../../index'
|
|
15
24
|
|
|
16
25
|
const NOTIFICATIONS = {
|
|
17
|
-
[METHODS.
|
|
26
|
+
[METHODS.updateAddress]: (instance) =>
|
|
18
27
|
({ params }) => {
|
|
19
28
|
instance.rpcClient.accounts = params
|
|
20
29
|
instance.onAddressChange(params)
|
|
21
30
|
},
|
|
22
31
|
[METHODS.updateNetwork]: (instance) =>
|
|
23
|
-
(
|
|
24
|
-
|
|
25
|
-
instance.
|
|
32
|
+
async ({ params }) => {
|
|
33
|
+
const { networkId, node } = params
|
|
34
|
+
instance.rpcClient.info.networkId = networkId
|
|
35
|
+
node && instance.addNode(node.name, await Node(node), true)
|
|
36
|
+
instance.onNetworkChange(params)
|
|
26
37
|
},
|
|
27
38
|
[METHODS.closeConnection]: (instance) =>
|
|
28
39
|
(msg) => {
|
|
@@ -32,27 +43,33 @@ const NOTIFICATIONS = {
|
|
|
32
43
|
}
|
|
33
44
|
|
|
34
45
|
const RESPONSES = {
|
|
35
|
-
[METHODS.
|
|
46
|
+
[METHODS.address]: (instance) =>
|
|
36
47
|
(msg) => instance.rpcClient.processResponse(msg),
|
|
37
|
-
[METHODS.
|
|
48
|
+
[METHODS.connect]: (instance) =>
|
|
38
49
|
(msg) => {
|
|
39
50
|
if (msg.result) instance.rpcClient.info.status = RPC_STATUS.CONNECTED
|
|
40
51
|
instance.rpcClient.processResponse(msg)
|
|
41
52
|
},
|
|
42
|
-
[METHODS.
|
|
53
|
+
[METHODS.subscribeAddress]: (instance) =>
|
|
43
54
|
(msg) => {
|
|
44
55
|
if (msg.result) {
|
|
45
|
-
|
|
46
|
-
|
|
56
|
+
if (msg.result.address) {
|
|
57
|
+
instance.rpcClient.accounts = msg.result.address
|
|
58
|
+
}
|
|
59
|
+
if (msg.result.subscription) {
|
|
60
|
+
instance.rpcClient.addressSubscription = msg.result.subscription
|
|
61
|
+
}
|
|
47
62
|
}
|
|
48
63
|
|
|
49
64
|
instance.rpcClient.processResponse(msg, ({ id, result }) => [result])
|
|
50
65
|
},
|
|
51
|
-
[METHODS.
|
|
66
|
+
[METHODS.sign]: (instance) =>
|
|
52
67
|
(msg) => {
|
|
53
|
-
instance.rpcClient.processResponse(
|
|
68
|
+
instance.rpcClient.processResponse(
|
|
69
|
+
msg, ({ id, result }) => [result.signedTransaction || result.transactionHash]
|
|
70
|
+
)
|
|
54
71
|
},
|
|
55
|
-
[METHODS.
|
|
72
|
+
[METHODS.signMessage]: (instance) =>
|
|
56
73
|
(msg) => {
|
|
57
74
|
instance.rpcClient.processResponse(msg, ({ id, result }) => [result.signature])
|
|
58
75
|
}
|
|
@@ -84,8 +101,18 @@ const handleMessage = (instance) => async (msg) => {
|
|
|
84
101
|
* @return {Object}
|
|
85
102
|
*/
|
|
86
103
|
export default Ae.compose({
|
|
87
|
-
async init ({
|
|
88
|
-
|
|
104
|
+
async init ({
|
|
105
|
+
name,
|
|
106
|
+
connection,
|
|
107
|
+
debug = false,
|
|
108
|
+
...other
|
|
109
|
+
}) {
|
|
110
|
+
['onAddressChange', 'onDisconnect', 'onNetworkChange'].forEach(event => {
|
|
111
|
+
const handler = other[event] ?? (() => {})
|
|
112
|
+
if (typeof handler !== 'function') throw new ArgumentError(event, 'a function', handler)
|
|
113
|
+
this[event] = handler
|
|
114
|
+
})
|
|
115
|
+
|
|
89
116
|
this.connection = connection
|
|
90
117
|
this.name = name
|
|
91
118
|
this.debug = debug
|
|
@@ -94,21 +121,13 @@ export default Ae.compose({
|
|
|
94
121
|
// Init RPCClient
|
|
95
122
|
await this.connectToWallet(connection)
|
|
96
123
|
}
|
|
97
|
-
// Event callbacks
|
|
98
|
-
this.onDisconnect = onDisconnect
|
|
99
|
-
this.onAddressChange = onAddressChange
|
|
100
|
-
this.onNetworkChange = onNetworkChange
|
|
101
|
-
// validation
|
|
102
|
-
eventsHandlers.forEach(event => {
|
|
103
|
-
if (!forceValidation && typeof this[event] !== 'function') throw new Error(`Call-back for ${event} must be an function!`)
|
|
104
|
-
})
|
|
105
124
|
},
|
|
106
125
|
deepProps: { Ae: { defaults: { walletBroadcast: true } } },
|
|
107
126
|
methods: {
|
|
108
127
|
sign () {
|
|
109
128
|
},
|
|
110
129
|
addresses () {
|
|
111
|
-
if (!this.
|
|
130
|
+
if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()
|
|
112
131
|
return [this.rpcClient.currentAccount, ...Object.keys(this.rpcClient.accounts.connected)]
|
|
113
132
|
},
|
|
114
133
|
/**
|
|
@@ -117,10 +136,14 @@ export default Ae.compose({
|
|
|
117
136
|
* @instance
|
|
118
137
|
* @rtype (connection: Object) => void
|
|
119
138
|
* @param {Object} connection Wallet connection object
|
|
120
|
-
* @
|
|
139
|
+
* @param {Object} [options={}]
|
|
140
|
+
* @param {Boolean} [options.connectNode=true] - Request wallet to bind node
|
|
141
|
+
* @param {String} [options.name=wallet-node] - Node name
|
|
142
|
+
* @param {Boolean} [options.select=false] - Select this node as current
|
|
143
|
+
* @return {Object}
|
|
121
144
|
*/
|
|
122
|
-
async connectToWallet (connection) {
|
|
123
|
-
if (this.rpcClient && this.rpcClient.isConnected()) throw new
|
|
145
|
+
async connectToWallet (connection, { connectNode = false, name = 'wallet-node', select = false } = {}) {
|
|
146
|
+
if (this.rpcClient && this.rpcClient.isConnected()) throw new AlreadyConnectedError('You are already connected to wallet ' + this.rpcClient)
|
|
124
147
|
this.rpcClient = RpcClient({
|
|
125
148
|
connection,
|
|
126
149
|
networkId: this.getNetworkId({ force: true }),
|
|
@@ -128,7 +151,12 @@ export default Ae.compose({
|
|
|
128
151
|
id: uuid(),
|
|
129
152
|
handlers: [handleMessage(this), this.onDisconnect]
|
|
130
153
|
})
|
|
131
|
-
|
|
154
|
+
const walletInfo = await this.sendConnectRequest(connectNode)
|
|
155
|
+
if (connectNode && !Object.prototype.hasOwnProperty.call(walletInfo, 'node')) {
|
|
156
|
+
throw new RpcConnectionError('Missing URLs of the Node')
|
|
157
|
+
}
|
|
158
|
+
connectNode && this.addNode(name, await Node(walletInfo.node), select)
|
|
159
|
+
return walletInfo
|
|
132
160
|
},
|
|
133
161
|
/**
|
|
134
162
|
* Disconnect from wallet
|
|
@@ -139,16 +167,18 @@ export default Ae.compose({
|
|
|
139
167
|
* @return {void}
|
|
140
168
|
*/
|
|
141
169
|
async disconnectWallet (sendDisconnect = true) {
|
|
142
|
-
if (!this.
|
|
170
|
+
if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')
|
|
143
171
|
sendDisconnect && this.rpcClient.sendMessage(message(METHODS.closeConnection, { reason: 'bye' }), true)
|
|
144
172
|
this.rpcClient.disconnect()
|
|
145
173
|
this.rpcClient = null
|
|
146
174
|
},
|
|
147
175
|
async address ({ onAccount } = {}) {
|
|
148
|
-
if (!this.
|
|
176
|
+
if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')
|
|
149
177
|
const current = this.rpcClient.currentAccount
|
|
150
|
-
if (!current) throw new
|
|
151
|
-
if (onAccount && !this.rpcClient.hasAccessToAccount(onAccount))
|
|
178
|
+
if (!current) throw new UnsubscribedAccountError()
|
|
179
|
+
if (onAccount && !this.rpcClient.hasAccessToAccount(onAccount)) {
|
|
180
|
+
throw new UnAuthorizedAccountError(onAccount)
|
|
181
|
+
}
|
|
152
182
|
return onAccount || current
|
|
153
183
|
},
|
|
154
184
|
/**
|
|
@@ -159,22 +189,22 @@ export default Ae.compose({
|
|
|
159
189
|
* @return {Promise} Address from wallet
|
|
160
190
|
*/
|
|
161
191
|
async askAddresses () {
|
|
162
|
-
if (!this.
|
|
163
|
-
if (!this.
|
|
164
|
-
return this.rpcClient.request(METHODS.
|
|
192
|
+
if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')
|
|
193
|
+
if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()
|
|
194
|
+
return this.rpcClient.request(METHODS.address)
|
|
165
195
|
},
|
|
166
196
|
/**
|
|
167
197
|
* Subscribe for addresses from wallet
|
|
168
198
|
* @function subscribeAddress
|
|
169
199
|
* @instance
|
|
170
200
|
* @rtype (type: String, value: String) => Promise
|
|
171
|
-
* @param {String} type
|
|
201
|
+
* @param {String} type Should be one of 'current' (the selected account), 'connected' (all)
|
|
172
202
|
* @param {String} value Subscription action('subscribe'|'unsubscribe')
|
|
173
203
|
* @return {Promise} Address from wallet
|
|
174
204
|
*/
|
|
175
205
|
async subscribeAddress (type, value) {
|
|
176
|
-
if (!this.
|
|
177
|
-
return this.rpcClient.request(METHODS.
|
|
206
|
+
if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')
|
|
207
|
+
return this.rpcClient.request(METHODS.subscribeAddress, { type, value })
|
|
178
208
|
},
|
|
179
209
|
/**
|
|
180
210
|
* Overwriting of `signTransaction` AE method
|
|
@@ -185,10 +215,15 @@ export default Ae.compose({
|
|
|
185
215
|
* @return {Promise<String>} Signed transaction
|
|
186
216
|
*/
|
|
187
217
|
async signTransaction (tx, opt = {}) {
|
|
188
|
-
if (!this.
|
|
189
|
-
if (!this.
|
|
190
|
-
if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount))
|
|
191
|
-
|
|
218
|
+
if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')
|
|
219
|
+
if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()
|
|
220
|
+
if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) {
|
|
221
|
+
throw new UnAuthorizedAccountError(opt.onAccount)
|
|
222
|
+
}
|
|
223
|
+
return this.rpcClient.request(
|
|
224
|
+
METHODS.sign,
|
|
225
|
+
{ ...opt, tx, returnSigned: true, networkId: this.getNetworkId() }
|
|
226
|
+
)
|
|
192
227
|
},
|
|
193
228
|
/**
|
|
194
229
|
* Overwriting of `signMessage` AE method
|
|
@@ -199,31 +234,36 @@ export default Ae.compose({
|
|
|
199
234
|
* @return {Promise<String>} Signed transaction
|
|
200
235
|
*/
|
|
201
236
|
async signMessage (msg, opt = {}) {
|
|
202
|
-
if (!this.
|
|
203
|
-
if (!this.
|
|
204
|
-
if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount))
|
|
205
|
-
|
|
237
|
+
if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')
|
|
238
|
+
if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()
|
|
239
|
+
if (opt.onAccount && !this.rpcClient.hasAccessToAccount(opt.onAccount)) {
|
|
240
|
+
throw new UnAuthorizedAccountError(opt.onAccount)
|
|
241
|
+
}
|
|
242
|
+
return this.rpcClient.request(METHODS.signMessage, { ...opt, message: msg })
|
|
206
243
|
},
|
|
207
244
|
/**
|
|
208
245
|
* Send connection request to wallet
|
|
209
246
|
* @function sendConnectRequest
|
|
210
247
|
* @instance
|
|
248
|
+
* @param {Boolean} connectNode - Request wallet to bind node
|
|
211
249
|
* @rtype () => Promise
|
|
212
250
|
* @return {Promise} Connection response
|
|
213
251
|
*/
|
|
214
|
-
async sendConnectRequest () {
|
|
252
|
+
async sendConnectRequest (connectNode) {
|
|
215
253
|
return this.rpcClient.request(
|
|
216
|
-
METHODS.
|
|
254
|
+
METHODS.connect, {
|
|
217
255
|
name: this.name,
|
|
218
256
|
version: VERSION,
|
|
219
|
-
networkId: this.getNetworkId({ force: true })
|
|
257
|
+
networkId: this.getNetworkId({ force: true }),
|
|
258
|
+
connectNode
|
|
220
259
|
}
|
|
221
260
|
)
|
|
222
261
|
},
|
|
223
262
|
/**
|
|
224
263
|
* Overwriting of `send` AE method
|
|
225
264
|
* All sdk API which use it will be send notification to wallet and wait for callBack
|
|
226
|
-
* This method will sign, broadcast and wait until transaction will be accepted using rpc
|
|
265
|
+
* This method will sign, broadcast and wait until transaction will be accepted using rpc
|
|
266
|
+
* communication with wallet
|
|
227
267
|
* @function send
|
|
228
268
|
* @instance
|
|
229
269
|
* @rtype (tx: String, options = {}) => Promise
|
|
@@ -233,15 +273,22 @@ export default Ae.compose({
|
|
|
233
273
|
* @return {Promise<Object>} Transaction broadcast result
|
|
234
274
|
*/
|
|
235
275
|
async send (tx, options = {}) {
|
|
236
|
-
if (!this.
|
|
237
|
-
if (!this.
|
|
238
|
-
if (options.onAccount && !this.rpcClient.hasAccessToAccount(options.onAccount))
|
|
239
|
-
|
|
276
|
+
if (!this.isConnected()) throw new NoWalletConnectedError('You are not connected to Wallet')
|
|
277
|
+
if (!this.isSubscribedAccount()) throw new UnsubscribedAccountError()
|
|
278
|
+
if (options.onAccount && !this.rpcClient.hasAccessToAccount(options.onAccount)) {
|
|
279
|
+
throw new UnAuthorizedAccountError(options.onAccount)
|
|
280
|
+
}
|
|
281
|
+
const opt = { ...this.Ae.defaults, ...options }
|
|
240
282
|
if (!opt.walletBroadcast) {
|
|
241
283
|
const signed = await this.signTransaction(tx, { onAccount: opt.onAccount })
|
|
242
284
|
return this.sendTransaction(signed, opt)
|
|
243
285
|
}
|
|
244
|
-
return this.rpcClient.request(
|
|
245
|
-
|
|
286
|
+
return this.rpcClient.request(
|
|
287
|
+
METHODS.sign,
|
|
288
|
+
{ onAccount: opt.onAccount, tx, returnSigned: false, networkId: this.getNetworkId() }
|
|
289
|
+
)
|
|
290
|
+
},
|
|
291
|
+
isConnected () { return (this.rpcClient && this.rpcClient.isConnected()) },
|
|
292
|
+
isSubscribedAccount () { return typeof this.rpcClient.currentAccount !== 'undefined' }
|
|
246
293
|
}
|
|
247
294
|
})
|