@alephium/web3 0.2.0-rc.3 → 0.2.0-rc.31
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/.eslintignore +2 -2
- package/README.md +2 -135
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.LICENSE.txt +0 -17
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +117 -17
- package/dist/src/api/api-alephium.js +145 -79
- package/dist/src/api/api-explorer.d.ts +163 -48
- package/dist/src/api/api-explorer.js +157 -34
- package/dist/src/api/index.d.ts +14 -2
- package/dist/src/api/index.js +46 -4
- package/dist/src/api/types.d.ts +23 -0
- package/dist/src/api/types.js +240 -0
- package/dist/src/api/utils.d.ts +6 -0
- package/dist/{scripts/rename-gitignore.js → src/api/utils.js} +11 -6
- package/dist/src/contract/contract.d.ts +110 -73
- package/dist/src/contract/contract.js +395 -451
- package/dist/src/contract/events.d.ts +4 -4
- package/dist/src/contract/events.js +2 -1
- package/dist/src/contract/index.js +5 -1
- package/dist/src/contract/ralph.d.ts +5 -4
- package/dist/src/contract/ralph.js +27 -1
- package/dist/src/global.d.ts +4 -0
- package/dist/{scripts/stop-devnet.js → src/global.js} +17 -11
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +23 -1
- package/dist/src/signer/index.d.ts +0 -1
- package/dist/src/signer/index.js +5 -2
- package/dist/src/signer/signer.d.ts +35 -28
- package/dist/src/signer/signer.js +79 -47
- package/dist/src/transaction/index.d.ts +0 -1
- package/dist/src/transaction/index.js +5 -2
- package/dist/src/transaction/status.d.ts +2 -1
- package/dist/src/transaction/status.js +2 -1
- package/dist/src/utils/bs58.d.ts +1 -0
- package/dist/src/utils/bs58.js +13 -1
- package/dist/src/utils/index.d.ts +0 -1
- package/dist/src/utils/index.js +5 -2
- package/dist/src/utils/subscription.d.ts +0 -3
- package/dist/src/utils/subscription.js +0 -1
- package/dist/src/utils/utils.d.ts +4 -9
- package/dist/src/utils/utils.js +20 -24
- package/jest-config.json +11 -0
- package/package.json +11 -47
- package/src/api/api-alephium.ts +169 -25
- package/src/api/api-explorer.ts +234 -51
- package/src/api/index.ts +53 -3
- package/src/api/types.ts +233 -0
- package/{scripts/rename-gitignore.js → src/api/utils.ts} +7 -6
- package/src/contract/contract.ts +582 -550
- package/src/contract/events.ts +6 -5
- package/src/contract/ralph.ts +29 -4
- package/src/{transaction/sign-verify.ts → global.ts} +14 -15
- package/src/index.ts +7 -0
- package/src/signer/index.ts +0 -1
- package/src/signer/signer.ts +122 -85
- package/src/transaction/index.ts +0 -1
- package/src/transaction/status.ts +5 -2
- package/src/utils/bs58.ts +11 -0
- package/src/utils/index.ts +0 -1
- package/src/utils/subscription.ts +0 -4
- package/src/utils/utils.ts +11 -19
- package/.eslintrc.json +0 -21
- package/LICENSE +0 -165
- package/contracts/add/add.ral +0 -16
- package/contracts/greeter/greeter.ral +0 -7
- package/contracts/greeter/greeter_interface.ral +0 -3
- package/contracts/greeter_main.ral +0 -9
- package/contracts/main.ral +0 -6
- package/contracts/sub/sub.ral +0 -9
- package/contracts/test/metadata.ral +0 -17
- package/contracts/test/warnings.ral +0 -5
- package/dev/user.conf +0 -29
- package/dist/scripts/create-project.d.ts +0 -2
- package/dist/scripts/create-project.js +0 -125
- package/dist/scripts/rename-gitignore.d.ts +0 -1
- package/dist/scripts/start-devnet.d.ts +0 -1
- package/dist/scripts/start-devnet.js +0 -131
- package/dist/scripts/stop-devnet.d.ts +0 -1
- package/dist/src/signer/node-wallet.d.ts +0 -13
- package/dist/src/signer/node-wallet.js +0 -60
- package/dist/src/test/index.d.ts +0 -7
- package/dist/src/test/index.js +0 -41
- package/dist/src/test/privatekey-wallet.d.ts +0 -12
- package/dist/src/test/privatekey-wallet.js +0 -68
- package/dist/src/transaction/sign-verify.d.ts +0 -2
- package/dist/src/transaction/sign-verify.js +0 -58
- package/dist/src/utils/password-crypto.d.ts +0 -2
- package/dist/src/utils/password-crypto.js +0 -69
- package/gitignore +0 -10
- package/scripts/create-project.ts +0 -137
- package/scripts/start-devnet.js +0 -141
- package/scripts/stop-devnet.js +0 -32
- package/src/contract/ralph.test.ts +0 -178
- package/src/fixtures/address.json +0 -36
- package/src/fixtures/balance.json +0 -9
- package/src/fixtures/self-clique.json +0 -19
- package/src/fixtures/transaction.json +0 -13
- package/src/fixtures/transactions.json +0 -179
- package/src/signer/fixtures/genesis.json +0 -26
- package/src/signer/fixtures/wallets.json +0 -26
- package/src/signer/node-wallet.ts +0 -74
- package/src/test/index.ts +0 -32
- package/src/test/privatekey-wallet.ts +0 -58
- package/src/transaction/sign-verify.test.ts +0 -50
- package/src/utils/address.test.ts +0 -47
- package/src/utils/djb2.test.ts +0 -35
- package/src/utils/password-crypto.test.ts +0 -27
- package/src/utils/password-crypto.ts +0 -77
- package/src/utils/utils.test.ts +0 -161
- package/templates/base/README.md +0 -34
- package/templates/base/package.json +0 -35
- package/templates/base/src/greeter.ts +0 -41
- package/templates/base/tsconfig.json +0 -19
- package/templates/react/README.md +0 -34
- package/templates/react/config-overrides.js +0 -18
- package/templates/react/package.json +0 -66
- package/templates/react/src/App.tsx +0 -42
- package/templates/react/src/artifacts/greeter.ral.json +0 -26
- package/templates/react/src/artifacts/greeter_main.ral.json +0 -22
- package/templates/shared/.eslintrc.json +0 -12
- package/templates/shared/scripts/header.js +0 -0
- package/test/contract.test.ts +0 -197
- package/test/events.test.ts +0 -138
- package/test/transaction.test.ts +0 -72
package/src/contract/events.ts
CHANGED
|
@@ -16,14 +16,15 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import {
|
|
19
|
+
import { web3 } from '..'
|
|
20
|
+
import { node } from '../api'
|
|
20
21
|
import { Subscription, SubscribeOptions } from '../utils'
|
|
21
22
|
|
|
22
|
-
export class EventSubscription extends Subscription<ContractEvent> {
|
|
23
|
+
export class EventSubscription extends Subscription<node.ContractEvent> {
|
|
23
24
|
readonly contractAddress: string
|
|
24
25
|
private fromCount: number
|
|
25
26
|
|
|
26
|
-
constructor(options: SubscribeOptions<ContractEvent>, contractAddress: string, fromCount?: number) {
|
|
27
|
+
constructor(options: SubscribeOptions<node.ContractEvent>, contractAddress: string, fromCount?: number) {
|
|
27
28
|
super(options)
|
|
28
29
|
this.contractAddress = contractAddress
|
|
29
30
|
this.fromCount = typeof fromCount === 'undefined' ? 0 : fromCount
|
|
@@ -44,7 +45,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
|
|
|
44
45
|
|
|
45
46
|
override async polling(): Promise<void> {
|
|
46
47
|
try {
|
|
47
|
-
const events = await
|
|
48
|
+
const events = await web3.getCurrentNodeProvider().events.getEventsContractContractaddress(this.contractAddress, {
|
|
48
49
|
start: this.fromCount
|
|
49
50
|
})
|
|
50
51
|
if (this.cancelled) {
|
|
@@ -67,7 +68,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
|
|
|
67
68
|
}
|
|
68
69
|
|
|
69
70
|
export function subscribeToEvents(
|
|
70
|
-
options: SubscribeOptions<ContractEvent>,
|
|
71
|
+
options: SubscribeOptions<node.ContractEvent>,
|
|
71
72
|
contractAddress: string,
|
|
72
73
|
fromCount?: number
|
|
73
74
|
): EventSubscription {
|
package/src/contract/ralph.ts
CHANGED
|
@@ -17,9 +17,9 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { Buffer } from 'buffer/'
|
|
20
|
+
import { Val } from '../api'
|
|
20
21
|
import { bs58, isHexString } from '../utils'
|
|
21
|
-
import {
|
|
22
|
-
import { Fields, Val } from './contract'
|
|
22
|
+
import { Fields, FieldsSig } from './contract'
|
|
23
23
|
|
|
24
24
|
const bigIntZero = BigInt(0)
|
|
25
25
|
|
|
@@ -236,7 +236,7 @@ export function encodeScriptField(tpe: string, value: Val): Uint8Array {
|
|
|
236
236
|
|
|
237
237
|
const scriptFieldRegex = /\{([0-9]*)\}/g
|
|
238
238
|
|
|
239
|
-
export function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fieldsSig:
|
|
239
|
+
export function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fieldsSig: FieldsSig): string {
|
|
240
240
|
return bytecodeTemplate.replace(scriptFieldRegex, (_, fieldIndex: string) => {
|
|
241
241
|
const fieldName = fieldsSig.names[`${fieldIndex}`]
|
|
242
242
|
const fieldType = fieldsSig.types[`${fieldIndex}`]
|
|
@@ -249,7 +249,7 @@ export function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fi
|
|
|
249
249
|
})
|
|
250
250
|
}
|
|
251
251
|
|
|
252
|
-
export function buildContractByteCode(bytecode: string, fields: Fields, fieldsSig:
|
|
252
|
+
export function buildContractByteCode(bytecode: string, fields: Fields, fieldsSig: FieldsSig): string {
|
|
253
253
|
const fieldsEncoded = fieldsSig.names.flatMap((fieldName, fieldIndex) => {
|
|
254
254
|
const fieldType = fieldsSig.types[`${fieldIndex}`]
|
|
255
255
|
if (fieldName in fields) {
|
|
@@ -343,6 +343,31 @@ function invalidVal(tpe: string, value: Val): Error {
|
|
|
343
343
|
return Error(`Invalid API value ${value} for type ${tpe}`)
|
|
344
344
|
}
|
|
345
345
|
|
|
346
|
+
export function buildDebugBytecode(bytecode: string, bytecodePatch: string): string {
|
|
347
|
+
if (bytecodePatch === '') {
|
|
348
|
+
return bytecode
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const pattern = /[=+-][0-9a-f]*/g
|
|
352
|
+
let result = ''
|
|
353
|
+
let index = 0
|
|
354
|
+
for (const parts of bytecodePatch.matchAll(pattern)) {
|
|
355
|
+
const part = parts[0]
|
|
356
|
+
const diffType = part[0]
|
|
357
|
+
if (diffType === '=') {
|
|
358
|
+
const length = parseInt(part.substring(1))
|
|
359
|
+
result = result + bytecode.slice(index, index + length)
|
|
360
|
+
index = index + length
|
|
361
|
+
} else if (diffType === '+') {
|
|
362
|
+
result = result + part.substring(1)
|
|
363
|
+
} else {
|
|
364
|
+
const length = parseInt(part.substring(1))
|
|
365
|
+
index = index + length
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return result
|
|
369
|
+
}
|
|
370
|
+
|
|
346
371
|
// export function buildContractByteCode(
|
|
347
372
|
// compiled: node.TemplateContractByteCode,
|
|
348
373
|
// templateVariables: TemplateVariables
|
|
@@ -16,24 +16,23 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import {
|
|
19
|
+
import { NodeProvider } from './api'
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
let _currentNodeProvider: NodeProvider | undefined = undefined
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
export function
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
23
|
+
export function setCurrentNodeProvider(provider: NodeProvider): void
|
|
24
|
+
export function setCurrentNodeProvider(baseUrl: string, apiKey?: string): void
|
|
25
|
+
export function setCurrentNodeProvider(provider: NodeProvider | string, apiKey?: string): void {
|
|
26
|
+
if (typeof provider == 'string') {
|
|
27
|
+
_currentNodeProvider = new NodeProvider(provider, apiKey)
|
|
28
|
+
} else {
|
|
29
|
+
_currentNodeProvider = provider
|
|
30
|
+
}
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
export function
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return key.verify(txHash, utils.signatureDecode(ec, signature))
|
|
36
|
-
} catch (error) {
|
|
37
|
-
return false
|
|
33
|
+
export function getCurrentNodeProvider(): NodeProvider {
|
|
34
|
+
if (typeof _currentNodeProvider === 'undefined') {
|
|
35
|
+
throw Error('No node provider is set.')
|
|
38
36
|
}
|
|
37
|
+
return _currentNodeProvider
|
|
39
38
|
}
|
package/src/index.ts
CHANGED
|
@@ -16,6 +16,10 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
+
BigInt.prototype['toJSON'] = function () {
|
|
20
|
+
return this.toString()
|
|
21
|
+
}
|
|
22
|
+
|
|
19
23
|
export * from './api'
|
|
20
24
|
export * from './contract'
|
|
21
25
|
export * from './signer'
|
|
@@ -23,3 +27,6 @@ export * from './utils'
|
|
|
23
27
|
export * from './transaction'
|
|
24
28
|
|
|
25
29
|
export * from './constants'
|
|
30
|
+
export * as web3 from './global'
|
|
31
|
+
|
|
32
|
+
export * as utils from './utils'
|
package/src/signer/index.ts
CHANGED
package/src/signer/signer.ts
CHANGED
|
@@ -17,12 +17,22 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { ec as EC } from 'elliptic'
|
|
20
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
fromApiNumber256,
|
|
22
|
+
fromApiTokens,
|
|
23
|
+
NodeProvider,
|
|
24
|
+
Number256,
|
|
25
|
+
toApiNumber256,
|
|
26
|
+
toApiNumber256Optional,
|
|
27
|
+
toApiTokens,
|
|
28
|
+
Token
|
|
29
|
+
} from '../api'
|
|
21
30
|
import { node } from '../api'
|
|
22
31
|
import * as utils from '../utils'
|
|
23
32
|
import { Eq, assertType } from '../utils'
|
|
24
33
|
import blake from 'blakejs'
|
|
25
|
-
|
|
34
|
+
|
|
35
|
+
export type OutputRef = node.OutputRef
|
|
26
36
|
|
|
27
37
|
const ec = new EC('secp256k1')
|
|
28
38
|
|
|
@@ -40,22 +50,20 @@ export interface Account {
|
|
|
40
50
|
publicKey: string
|
|
41
51
|
}
|
|
42
52
|
|
|
43
|
-
export type SubmitTx = { submitTx?: boolean }
|
|
44
53
|
export type SignerAddress = { signerAddress: string }
|
|
45
|
-
type TxBuildParams<T> = Omit<T, 'fromPublicKey' | 'targetBlockHash'> & SignerAddress
|
|
54
|
+
type TxBuildParams<T> = Omit<T, 'fromPublicKey' | 'targetBlockHash'> & SignerAddress
|
|
46
55
|
|
|
47
56
|
export type GetAccountsParams = undefined
|
|
48
57
|
export type GetAccountsResult = Account[]
|
|
49
58
|
|
|
50
59
|
export interface SignTransferTxParams {
|
|
51
60
|
signerAddress: string
|
|
52
|
-
destinations:
|
|
53
|
-
utxos?:
|
|
61
|
+
destinations: Destination[]
|
|
62
|
+
utxos?: OutputRef[]
|
|
54
63
|
gasAmount?: number
|
|
55
|
-
gasPrice?:
|
|
56
|
-
submitTx?: boolean
|
|
64
|
+
gasPrice?: Number256
|
|
57
65
|
}
|
|
58
|
-
assertType<Eq<SignTransferTxParams, TxBuildParams<node.BuildTransaction>>>()
|
|
66
|
+
assertType<Eq<keyof SignTransferTxParams, keyof TxBuildParams<node.BuildTransaction>>>()
|
|
59
67
|
export interface SignTransferTxResult {
|
|
60
68
|
fromGroup: number
|
|
61
69
|
toGroup: number
|
|
@@ -68,14 +76,13 @@ assertType<Eq<SignTransferTxResult, SignResult>>()
|
|
|
68
76
|
export interface SignDeployContractTxParams {
|
|
69
77
|
signerAddress: string
|
|
70
78
|
bytecode: string
|
|
71
|
-
initialAttoAlphAmount?:
|
|
79
|
+
initialAttoAlphAmount?: Number256
|
|
72
80
|
initialTokenAmounts?: Token[]
|
|
73
|
-
issueTokenAmount?:
|
|
81
|
+
issueTokenAmount?: Number256
|
|
74
82
|
gasAmount?: number
|
|
75
|
-
gasPrice?:
|
|
76
|
-
submitTx?: boolean
|
|
83
|
+
gasPrice?: Number256
|
|
77
84
|
}
|
|
78
|
-
assertType<Eq<SignDeployContractTxParams, TxBuildParams<node.BuildDeployContractTx>>>()
|
|
85
|
+
assertType<Eq<keyof SignDeployContractTxParams, keyof TxBuildParams<node.BuildDeployContractTx>>>()
|
|
79
86
|
export interface SignDeployContractTxResult {
|
|
80
87
|
fromGroup: number
|
|
81
88
|
toGroup: number
|
|
@@ -91,12 +98,11 @@ export interface SignExecuteScriptTxParams {
|
|
|
91
98
|
signerAddress: string
|
|
92
99
|
bytecode: string
|
|
93
100
|
attoAlphAmount?: string
|
|
94
|
-
tokens?:
|
|
101
|
+
tokens?: Token[]
|
|
95
102
|
gasAmount?: number
|
|
96
103
|
gasPrice?: string
|
|
97
|
-
submitTx?: boolean
|
|
98
104
|
}
|
|
99
|
-
assertType<Eq<SignExecuteScriptTxParams, TxBuildParams<node.BuildExecuteScriptTx>>>()
|
|
105
|
+
assertType<Eq<keyof SignExecuteScriptTxParams, keyof TxBuildParams<node.BuildExecuteScriptTx>>>()
|
|
100
106
|
export interface SignExecuteScriptTxResult {
|
|
101
107
|
fromGroup: number
|
|
102
108
|
toGroup: number
|
|
@@ -109,9 +115,8 @@ assertType<Eq<SignExecuteScriptTxResult, SignResult>>()
|
|
|
109
115
|
export interface SignUnsignedTxParams {
|
|
110
116
|
signerAddress: string
|
|
111
117
|
unsignedTx: string
|
|
112
|
-
submitTx?: boolean
|
|
113
118
|
}
|
|
114
|
-
assertType<Eq<SignUnsignedTxParams, { unsignedTx: string } &
|
|
119
|
+
assertType<Eq<SignUnsignedTxParams, { unsignedTx: string } & SignerAddress>>()
|
|
115
120
|
export interface SignUnsignedTxResult {
|
|
116
121
|
fromGroup: number
|
|
117
122
|
toGroup: number
|
|
@@ -141,8 +146,8 @@ export interface SignMessageResult {
|
|
|
141
146
|
}
|
|
142
147
|
assertType<Eq<SignMessageResult, Pick<SignResult, 'signature'>>>()
|
|
143
148
|
|
|
144
|
-
export interface
|
|
145
|
-
|
|
149
|
+
export interface SignerProviderWithoutNodeProvider {
|
|
150
|
+
getSelectedAccount(): Promise<Account>
|
|
146
151
|
signTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>
|
|
147
152
|
signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>
|
|
148
153
|
signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>
|
|
@@ -151,117 +156,112 @@ export interface SignerProvider {
|
|
|
151
156
|
signMessage(params: SignMessageParams): Promise<SignMessageResult>
|
|
152
157
|
}
|
|
153
158
|
|
|
154
|
-
export abstract class
|
|
155
|
-
|
|
156
|
-
|
|
159
|
+
export abstract class SignerProvider implements SignerProviderWithoutNodeProvider {
|
|
160
|
+
abstract get nodeProvider(): NodeProvider
|
|
161
|
+
abstract getSelectedAccount(): Promise<Account>
|
|
157
162
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const accounts = await this.getAccounts()
|
|
162
|
-
const account = accounts.find((a) => a.address === signerAddress)
|
|
163
|
-
if (typeof account === 'undefined') {
|
|
164
|
-
throw new Error('Unmatched signerAddress')
|
|
165
|
-
} else {
|
|
166
|
-
return account
|
|
167
|
-
}
|
|
163
|
+
async submitTransaction(unsignedTx: string, signature: string): Promise<SubmissionResult> {
|
|
164
|
+
const params: node.SubmitTransaction = { unsignedTx: unsignedTx, signature: signature }
|
|
165
|
+
return this.nodeProvider.transactions.postTransactionsSubmit(params)
|
|
168
166
|
}
|
|
169
167
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
this.
|
|
168
|
+
async signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SubmissionResult> {
|
|
169
|
+
const signResult = await this.signTransferTx(params)
|
|
170
|
+
return this.submitTransaction(signResult.unsignedTx, signResult.signature)
|
|
173
171
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
return (
|
|
172
|
+
async signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SubmissionResult> {
|
|
173
|
+
const signResult = await this.signDeployContractTx(params)
|
|
174
|
+
return this.submitTransaction(signResult.unsignedTx, signResult.signature)
|
|
177
175
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
const signature = await this.signRaw(address, txHash)
|
|
182
|
-
const params: node.SubmitTransaction = { unsignedTx: unsignedTx, signature: signature }
|
|
183
|
-
return this.provider.transactions.postTransactionsSubmit(params)
|
|
176
|
+
async signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SubmissionResult> {
|
|
177
|
+
const signResult = await this.signExecuteScriptTx(params)
|
|
178
|
+
return this.submitTransaction(signResult.unsignedTx, signResult.signature)
|
|
184
179
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
return this.
|
|
180
|
+
async signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SubmissionResult> {
|
|
181
|
+
const signResult = await this.signUnsignedTx(params)
|
|
182
|
+
return this.submitTransaction(signResult.unsignedTx, signResult.signature)
|
|
188
183
|
}
|
|
189
184
|
|
|
190
185
|
private async usePublicKey<T extends SignerAddress>(
|
|
191
186
|
params: T
|
|
192
187
|
): Promise<Omit<T, 'signerAddress'> & { fromPublicKey: string }> {
|
|
193
188
|
const { signerAddress, ...restParams } = params
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
throw new Error('Unknown signer address')
|
|
189
|
+
const selectedAccount = await this.getSelectedAccount()
|
|
190
|
+
if (signerAddress !== selectedAccount.address) {
|
|
191
|
+
throw new Error('The signer address is not the selected address')
|
|
198
192
|
} else {
|
|
199
|
-
return { fromPublicKey:
|
|
193
|
+
return { fromPublicKey: selectedAccount.publicKey, ...restParams }
|
|
200
194
|
}
|
|
201
195
|
}
|
|
202
196
|
|
|
203
197
|
async signTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult> {
|
|
204
198
|
const response = await this.buildTransferTx(params)
|
|
205
|
-
return this.handleSign({ signerAddress: params.signerAddress, ...response }
|
|
199
|
+
return this.handleSign({ signerAddress: params.signerAddress, ...response })
|
|
206
200
|
}
|
|
207
201
|
|
|
208
202
|
async buildTransferTx(params: SignTransferTxParams): Promise<node.BuildTransactionResult> {
|
|
209
|
-
|
|
203
|
+
const data: node.BuildTransaction = {
|
|
204
|
+
...(await this.usePublicKey(params)),
|
|
205
|
+
destinations: toApiDestinations(params.destinations),
|
|
206
|
+
gasPrice: toApiNumber256Optional(params.gasPrice)
|
|
207
|
+
}
|
|
208
|
+
return this.nodeProvider.transactions.postTransactionsBuild(data)
|
|
210
209
|
}
|
|
211
210
|
|
|
212
211
|
async signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult> {
|
|
213
212
|
const response = await this.buildContractCreationTx(params)
|
|
214
|
-
const result = await this.handleSign(
|
|
215
|
-
{ signerAddress: params.signerAddress, ...response },
|
|
216
|
-
this.shouldSubmitTx(params)
|
|
217
|
-
)
|
|
213
|
+
const result = await this.handleSign({ signerAddress: params.signerAddress, ...response })
|
|
218
214
|
const contractId = utils.binToHex(utils.contractIdFromAddress(response.contractAddress))
|
|
219
215
|
return { ...result, contractId: contractId, contractAddress: response.contractAddress }
|
|
220
216
|
}
|
|
221
217
|
|
|
222
218
|
async buildContractCreationTx(params: SignDeployContractTxParams): Promise<node.BuildDeployContractTxResult> {
|
|
223
|
-
|
|
219
|
+
const data: node.BuildDeployContractTx = {
|
|
220
|
+
...(await this.usePublicKey(params)),
|
|
221
|
+
initialAttoAlphAmount: toApiNumber256Optional(params.initialAttoAlphAmount),
|
|
222
|
+
initialTokenAmounts: toApiTokens(params.initialTokenAmounts),
|
|
223
|
+
issueTokenAmount: toApiNumber256Optional(params.issueTokenAmount),
|
|
224
|
+
gasPrice: toApiNumber256Optional(params.gasPrice)
|
|
225
|
+
}
|
|
226
|
+
return this.nodeProvider.contracts.postContractsUnsignedTxDeployContract(data)
|
|
224
227
|
}
|
|
225
228
|
|
|
226
229
|
async signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult> {
|
|
227
230
|
const response = await this.buildScriptTx(params)
|
|
228
|
-
return this.handleSign({ signerAddress: params.signerAddress, ...response }
|
|
231
|
+
return this.handleSign({ signerAddress: params.signerAddress, ...response })
|
|
229
232
|
}
|
|
230
233
|
|
|
231
234
|
async buildScriptTx(params: SignExecuteScriptTxParams): Promise<node.BuildExecuteScriptTxResult> {
|
|
232
|
-
|
|
235
|
+
const data: node.BuildExecuteScriptTx = {
|
|
236
|
+
...(await this.usePublicKey(params)),
|
|
237
|
+
tokens: toApiTokens(params.tokens)
|
|
238
|
+
}
|
|
239
|
+
return this.nodeProvider.contracts.postContractsUnsignedTxExecuteScript(data)
|
|
233
240
|
}
|
|
234
241
|
|
|
235
242
|
// in general, wallet should show the decoded information to user for confirmation
|
|
236
243
|
// please overwrite this function for real wallet
|
|
237
244
|
async signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult> {
|
|
238
245
|
const data = { unsignedTx: params.unsignedTx }
|
|
239
|
-
const decoded = await this.
|
|
240
|
-
return this.handleSign(
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
},
|
|
248
|
-
params.submitTx ? params.submitTx : true // we don't consider `alwaysSubmitTx` as the tx might needs multiple signatures
|
|
249
|
-
)
|
|
246
|
+
const decoded = await this.nodeProvider.transactions.postTransactionsDecodeUnsignedTx(data)
|
|
247
|
+
return this.handleSign({
|
|
248
|
+
fromGroup: decoded.fromGroup,
|
|
249
|
+
toGroup: decoded.toGroup,
|
|
250
|
+
signerAddress: params.signerAddress,
|
|
251
|
+
unsignedTx: params.unsignedTx,
|
|
252
|
+
txId: decoded.unsignedTx.txId
|
|
253
|
+
})
|
|
250
254
|
}
|
|
251
255
|
|
|
252
|
-
protected async handleSign(
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
+
protected async handleSign(response: {
|
|
257
|
+
fromGroup: number
|
|
258
|
+
toGroup: number
|
|
259
|
+
signerAddress: string
|
|
260
|
+
unsignedTx: string
|
|
261
|
+
txId: string
|
|
262
|
+
}): Promise<SignResult> {
|
|
256
263
|
// sign the tx
|
|
257
264
|
const signature = await this.signRaw(response.signerAddress, response.txId)
|
|
258
|
-
// submit the tx if required
|
|
259
|
-
if (submitTx) {
|
|
260
|
-
await this.provider.transactions.postTransactionsSubmit({
|
|
261
|
-
unsignedTx: response.unsignedTx,
|
|
262
|
-
signature: signature
|
|
263
|
-
})
|
|
264
|
-
}
|
|
265
265
|
// return the signature back to the provider
|
|
266
266
|
return {
|
|
267
267
|
fromGroup: response.fromGroup,
|
|
@@ -287,6 +287,22 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
|
|
|
287
287
|
abstract signRaw(signerAddress: string, hexString: string): Promise<string>
|
|
288
288
|
}
|
|
289
289
|
|
|
290
|
+
export abstract class SignerProviderWithMultipleAccounts extends SignerProvider {
|
|
291
|
+
abstract getAccounts(): Promise<Account[]>
|
|
292
|
+
|
|
293
|
+
async getAccount(signerAddress: string): Promise<Account> {
|
|
294
|
+
const accounts = await this.getAccounts()
|
|
295
|
+
const account = accounts.find((a) => a.address === signerAddress)
|
|
296
|
+
if (typeof account === 'undefined') {
|
|
297
|
+
throw new Error('Unmatched signerAddress')
|
|
298
|
+
} else {
|
|
299
|
+
return account
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
abstract setSelectedAccount(address: string): Promise<void>
|
|
304
|
+
}
|
|
305
|
+
|
|
290
306
|
export interface SubmissionResult {
|
|
291
307
|
txId: string
|
|
292
308
|
fromGroup: number
|
|
@@ -311,3 +327,24 @@ export function verifySignedMessage(message: string, publicKey: string, signatur
|
|
|
311
327
|
const messageHash = blake.blake2b(extendedMessage, undefined, 32)
|
|
312
328
|
return verifyHexString(utils.binToHex(messageHash), publicKey, signature)
|
|
313
329
|
}
|
|
330
|
+
|
|
331
|
+
export interface Destination {
|
|
332
|
+
address: string
|
|
333
|
+
attoAlphAmount: Number256
|
|
334
|
+
tokens?: Token[]
|
|
335
|
+
lockTime?: number
|
|
336
|
+
message?: string
|
|
337
|
+
}
|
|
338
|
+
assertType<Eq<keyof Destination, keyof node.Destination>>
|
|
339
|
+
|
|
340
|
+
export function toApiDestination(data: Destination): node.Destination {
|
|
341
|
+
return { ...data, attoAlphAmount: toApiNumber256(data.attoAlphAmount), tokens: toApiTokens(data.tokens) }
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export function toApiDestinations(data: Destination[]): node.Destination[] {
|
|
345
|
+
return data.map(toApiDestination)
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export function fromApiDestination(data: node.Destination): Destination {
|
|
349
|
+
return { ...data, attoAlphAmount: fromApiNumber256(data.attoAlphAmount), tokens: fromApiTokens(data.tokens) }
|
|
350
|
+
}
|
package/src/transaction/index.ts
CHANGED
|
@@ -16,9 +16,12 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import {
|
|
19
|
+
import { web3 } from '..'
|
|
20
|
+
import { node } from '../api'
|
|
20
21
|
import { Subscription, SubscribeOptions } from '../utils'
|
|
21
22
|
|
|
23
|
+
export type TxStatus = node.TxStatus
|
|
24
|
+
|
|
22
25
|
export class TxStatusSubscription extends Subscription<TxStatus> {
|
|
23
26
|
readonly txId: string
|
|
24
27
|
readonly fromGroup?: number
|
|
@@ -35,7 +38,7 @@ export class TxStatusSubscription extends Subscription<TxStatus> {
|
|
|
35
38
|
|
|
36
39
|
override async polling(): Promise<void> {
|
|
37
40
|
try {
|
|
38
|
-
const txStatus = await
|
|
41
|
+
const txStatus = await web3.getCurrentNodeProvider().transactions.getTransactionsStatus({
|
|
39
42
|
txId: this.txId,
|
|
40
43
|
fromGroup: this.fromGroup,
|
|
41
44
|
toGroup: this.toGroup
|
package/src/utils/bs58.ts
CHANGED
|
@@ -23,4 +23,15 @@ const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
|
|
23
23
|
|
|
24
24
|
export const bs58 = basex(ALPHABET)
|
|
25
25
|
|
|
26
|
+
export function isBase58(s: string): boolean {
|
|
27
|
+
if (s === '' || s.trim() === '') {
|
|
28
|
+
return false
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
return bs58.encode(bs58.decode(s)) === s
|
|
32
|
+
} catch (err) {
|
|
33
|
+
return false
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
26
37
|
export default bs58
|
package/src/utils/index.ts
CHANGED
|
@@ -17,20 +17,17 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import EventEmitter from 'eventemitter3'
|
|
20
|
-
import { NodeProvider } from '../api'
|
|
21
20
|
|
|
22
21
|
type MessageCallback<Message> = (message: Message) => Promise<void>
|
|
23
22
|
type ErrorCallback<Message> = (error: any, subscription: Subscription<Message>) => Promise<void>
|
|
24
23
|
|
|
25
24
|
export interface SubscribeOptions<Message> {
|
|
26
|
-
provider: NodeProvider
|
|
27
25
|
pollingInterval: number
|
|
28
26
|
messageCallback: MessageCallback<Message>
|
|
29
27
|
errorCallback: ErrorCallback<Message>
|
|
30
28
|
}
|
|
31
29
|
|
|
32
30
|
export abstract class Subscription<Message> {
|
|
33
|
-
provider: NodeProvider
|
|
34
31
|
pollingInterval: number
|
|
35
32
|
|
|
36
33
|
protected messageCallback: MessageCallback<Message>
|
|
@@ -40,7 +37,6 @@ export abstract class Subscription<Message> {
|
|
|
40
37
|
protected cancelled: boolean
|
|
41
38
|
|
|
42
39
|
constructor(options: SubscribeOptions<Message>) {
|
|
43
|
-
this.provider = options.provider
|
|
44
40
|
this.pollingInterval = options.pollingInterval
|
|
45
41
|
this.messageCallback = options.messageCallback
|
|
46
42
|
this.errorCallback = options.errorCallback
|