@alephium/web3 0.2.0-rc.32 → 0.2.0-rc.34
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/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/index.d.ts +43 -12
- package/dist/src/api/index.js +27 -16
- package/dist/src/contract/contract.d.ts +4 -4
- package/dist/src/contract/contract.js +10 -20
- package/dist/src/signer/signer.d.ts +32 -42
- package/dist/src/signer/signer.js +38 -42
- package/package.json +1 -1
- package/src/api/index.ts +69 -27
- package/src/contract/contract.ts +16 -21
- package/src/signer/signer.ts +75 -86
package/src/contract/contract.ts
CHANGED
|
@@ -808,10 +808,13 @@ export class Contract extends Artifact {
|
|
|
808
808
|
}
|
|
809
809
|
}
|
|
810
810
|
|
|
811
|
-
async
|
|
811
|
+
async txParamsForDeployment(
|
|
812
|
+
signer: SignerProvider,
|
|
813
|
+
params: Omit<BuildDeployContractTx, 'signerAddress'>
|
|
814
|
+
): Promise<SignDeployContractTxParams> {
|
|
812
815
|
const bytecode = this.buildByteCodeToDeploy(params.initialFields ? params.initialFields : {})
|
|
813
816
|
const signerParams: SignDeployContractTxParams = {
|
|
814
|
-
signerAddress:
|
|
817
|
+
signerAddress: (await signer.getSelectedAccount()).address,
|
|
815
818
|
bytecode: bytecode,
|
|
816
819
|
initialAttoAlphAmount: extractOptionalNumber256(params.initialAttoAlphAmount),
|
|
817
820
|
issueTokenAmount: extractOptionalNumber256(params.issueTokenAmount),
|
|
@@ -822,16 +825,12 @@ export class Contract extends Artifact {
|
|
|
822
825
|
return signerParams
|
|
823
826
|
}
|
|
824
827
|
|
|
825
|
-
async
|
|
828
|
+
async deploy(
|
|
826
829
|
signer: SignerProvider,
|
|
827
830
|
params: Omit<BuildDeployContractTx, 'signerAddress'>
|
|
828
831
|
): Promise<DeployContractTransaction> {
|
|
829
|
-
const signerParams = await this.
|
|
830
|
-
|
|
831
|
-
signerAddress: (await signer.getSelectedAccount()).address
|
|
832
|
-
})
|
|
833
|
-
const response = await signer.buildContractCreationTx(signerParams)
|
|
834
|
-
return fromApiDeployContractUnsignedTx(response)
|
|
832
|
+
const signerParams = await this.txParamsForDeployment(signer, params)
|
|
833
|
+
return signer.signAndSubmitDeployContractTx(signerParams)
|
|
835
834
|
}
|
|
836
835
|
|
|
837
836
|
buildByteCodeToDeploy(initialFields: Fields): string {
|
|
@@ -896,9 +895,12 @@ export class Script extends Artifact {
|
|
|
896
895
|
return JSON.stringify(object, null, 2)
|
|
897
896
|
}
|
|
898
897
|
|
|
899
|
-
async
|
|
898
|
+
async txParamsForExecution(
|
|
899
|
+
signer: SignerProvider,
|
|
900
|
+
params: Omit<BuildExecuteScriptTx, 'signerAddress'>
|
|
901
|
+
): Promise<SignExecuteScriptTxParams> {
|
|
900
902
|
const signerParams: SignExecuteScriptTxParams = {
|
|
901
|
-
signerAddress:
|
|
903
|
+
signerAddress: (await signer.getSelectedAccount()).address,
|
|
902
904
|
bytecode: this.buildByteCodeToDeploy(params.initialFields ? params.initialFields : {}),
|
|
903
905
|
attoAlphAmount: extractOptionalNumber256(params.attoAlphAmount),
|
|
904
906
|
tokens: toApiTokens(params.tokens),
|
|
@@ -908,15 +910,12 @@ export class Script extends Artifact {
|
|
|
908
910
|
return signerParams
|
|
909
911
|
}
|
|
910
912
|
|
|
911
|
-
async
|
|
913
|
+
async execute(
|
|
912
914
|
signer: SignerProvider,
|
|
913
915
|
params: Omit<BuildExecuteScriptTx, 'signerAddress'>
|
|
914
916
|
): Promise<BuildScriptTxResult> {
|
|
915
|
-
const signerParams = await this.
|
|
916
|
-
|
|
917
|
-
signerAddress: (await signer.getSelectedAccount()).address
|
|
918
|
-
})
|
|
919
|
-
return await signer.buildScriptTx(signerParams)
|
|
917
|
+
const signerParams = await this.txParamsForExecution(signer, params)
|
|
918
|
+
return await signer.signAndSubmitExecuteScriptTx(signerParams)
|
|
920
919
|
}
|
|
921
920
|
|
|
922
921
|
buildByteCodeToDeploy(initialFields: Fields): string {
|
|
@@ -1098,10 +1097,6 @@ export interface DeployContractTransaction {
|
|
|
1098
1097
|
contractId: string
|
|
1099
1098
|
}
|
|
1100
1099
|
|
|
1101
|
-
function fromApiDeployContractUnsignedTx(result: node.BuildDeployContractTxResult): DeployContractTransaction {
|
|
1102
|
-
return { ...result, contractId: binToHex(contractIdFromAddress(result.contractAddress)) }
|
|
1103
|
-
}
|
|
1104
|
-
|
|
1105
1100
|
type BuildTxParams<T> = Omit<T, 'bytecode'> & { initialFields?: Val[] }
|
|
1106
1101
|
|
|
1107
1102
|
export interface BuildDeployContractTx {
|
package/src/signer/signer.ts
CHANGED
|
@@ -36,14 +36,6 @@ export type OutputRef = node.OutputRef
|
|
|
36
36
|
|
|
37
37
|
const ec = new EC('secp256k1')
|
|
38
38
|
|
|
39
|
-
export interface SignResult {
|
|
40
|
-
fromGroup: number
|
|
41
|
-
toGroup: number
|
|
42
|
-
unsignedTx: string
|
|
43
|
-
txId: string
|
|
44
|
-
signature: string
|
|
45
|
-
}
|
|
46
|
-
|
|
47
39
|
export interface Account {
|
|
48
40
|
address: string
|
|
49
41
|
group: number
|
|
@@ -52,9 +44,7 @@ export interface Account {
|
|
|
52
44
|
|
|
53
45
|
export type SignerAddress = { signerAddress: string }
|
|
54
46
|
type TxBuildParams<T> = Omit<T, 'fromPublicKey' | 'targetBlockHash'> & SignerAddress
|
|
55
|
-
|
|
56
|
-
export type GetAccountsParams = undefined
|
|
57
|
-
export type GetAccountsResult = Account[]
|
|
47
|
+
type SignResult<T> = Omit<T, 'gasPrice'> & { signature: string; gasPrice: Number256 }
|
|
58
48
|
|
|
59
49
|
export interface SignTransferTxParams {
|
|
60
50
|
signerAddress: string
|
|
@@ -70,8 +60,10 @@ export interface SignTransferTxResult {
|
|
|
70
60
|
unsignedTx: string
|
|
71
61
|
txId: string
|
|
72
62
|
signature: string
|
|
63
|
+
gasAmount: number
|
|
64
|
+
gasPrice: Number256
|
|
73
65
|
}
|
|
74
|
-
assertType<Eq<SignTransferTxResult, SignResult
|
|
66
|
+
assertType<Eq<SignTransferTxResult, SignResult<node.BuildTransactionResult>>>()
|
|
75
67
|
|
|
76
68
|
export interface SignDeployContractTxParams {
|
|
77
69
|
signerAddress: string
|
|
@@ -91,8 +83,10 @@ export interface SignDeployContractTxResult {
|
|
|
91
83
|
signature: string
|
|
92
84
|
contractId: string
|
|
93
85
|
contractAddress: string
|
|
86
|
+
gasAmount: number
|
|
87
|
+
gasPrice: Number256
|
|
94
88
|
}
|
|
95
|
-
assertType<Eq<SignDeployContractTxResult, SignResult & { contractId: string
|
|
89
|
+
assertType<Eq<SignDeployContractTxResult, SignResult<node.BuildDeployContractTxResult> & { contractId: string }>>()
|
|
96
90
|
|
|
97
91
|
export interface SignExecuteScriptTxParams {
|
|
98
92
|
signerAddress: string
|
|
@@ -109,8 +103,10 @@ export interface SignExecuteScriptTxResult {
|
|
|
109
103
|
unsignedTx: string
|
|
110
104
|
txId: string
|
|
111
105
|
signature: string
|
|
106
|
+
gasAmount: number
|
|
107
|
+
gasPrice: Number256
|
|
112
108
|
}
|
|
113
|
-
assertType<Eq<SignExecuteScriptTxResult, SignResult
|
|
109
|
+
assertType<Eq<SignExecuteScriptTxResult, SignResult<node.BuildExecuteScriptTxResult>>>()
|
|
114
110
|
|
|
115
111
|
export interface SignUnsignedTxParams {
|
|
116
112
|
signerAddress: string
|
|
@@ -123,18 +119,10 @@ export interface SignUnsignedTxResult {
|
|
|
123
119
|
unsignedTx: string
|
|
124
120
|
txId: string
|
|
125
121
|
signature: string
|
|
122
|
+
gasAmount: number
|
|
123
|
+
gasPrice: Number256
|
|
126
124
|
}
|
|
127
|
-
assertType<Eq<SignUnsignedTxResult,
|
|
128
|
-
|
|
129
|
-
export interface SignHexStringParams {
|
|
130
|
-
signerAddress: string
|
|
131
|
-
hexString: string
|
|
132
|
-
}
|
|
133
|
-
assertType<Eq<SignHexStringParams, { hexString: string } & SignerAddress>>()
|
|
134
|
-
export interface SignHexStringResult {
|
|
135
|
-
signature: string
|
|
136
|
-
}
|
|
137
|
-
assertType<Eq<SignHexStringResult, Pick<SignResult, 'signature'>>>()
|
|
125
|
+
assertType<Eq<SignUnsignedTxResult, SignTransferTxResult>>
|
|
138
126
|
|
|
139
127
|
export interface SignMessageParams {
|
|
140
128
|
signerAddress: string
|
|
@@ -144,42 +132,68 @@ assertType<Eq<SignMessageParams, { message: string } & SignerAddress>>()
|
|
|
144
132
|
export interface SignMessageResult {
|
|
145
133
|
signature: string
|
|
146
134
|
}
|
|
147
|
-
assertType<Eq<SignMessageResult, Pick<SignResult, 'signature'>>>()
|
|
148
135
|
|
|
149
|
-
export interface
|
|
136
|
+
export interface SubmitTransactionParams {
|
|
137
|
+
unsignedTx: string
|
|
138
|
+
signature: string
|
|
139
|
+
}
|
|
140
|
+
export interface SubmissionResult {
|
|
141
|
+
txId: string
|
|
142
|
+
fromGroup: number
|
|
143
|
+
toGroup: number
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface SignerProvider {
|
|
147
|
+
get nodeProvider(): NodeProvider | undefined
|
|
148
|
+
|
|
150
149
|
getSelectedAccount(): Promise<Account>
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
150
|
+
|
|
151
|
+
signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>
|
|
152
|
+
signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>
|
|
153
|
+
signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>
|
|
154
|
+
signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>
|
|
155
|
+
|
|
154
156
|
signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>
|
|
155
|
-
|
|
157
|
+
// The message will be prefixed with 'Alephium Signed Message: ' before signing
|
|
158
|
+
// so that the resulted signature cannot be reused for building transactions.
|
|
156
159
|
signMessage(params: SignMessageParams): Promise<SignMessageResult>
|
|
157
160
|
}
|
|
158
161
|
|
|
159
|
-
export abstract class
|
|
160
|
-
abstract get nodeProvider(): NodeProvider
|
|
162
|
+
export abstract class SignerProviderSimple implements SignerProvider {
|
|
163
|
+
abstract get nodeProvider(): NodeProvider | undefined
|
|
161
164
|
abstract getSelectedAccount(): Promise<Account>
|
|
162
165
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
+
private getNodeProvider(): NodeProvider {
|
|
167
|
+
if (this.nodeProvider === undefined) {
|
|
168
|
+
throw Error('The signer does not contain a node provider')
|
|
169
|
+
}
|
|
170
|
+
return this.nodeProvider
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async submitTransaction(params: SubmitTransactionParams): Promise<SubmissionResult> {
|
|
174
|
+
const data: node.SubmitTransaction = { unsignedTx: params.unsignedTx, signature: params.signature }
|
|
175
|
+
return this.getNodeProvider().transactions.postTransactionsSubmit(data)
|
|
166
176
|
}
|
|
167
177
|
|
|
168
|
-
async signAndSubmitTransferTx(params: SignTransferTxParams): Promise<
|
|
178
|
+
async signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult> {
|
|
169
179
|
const signResult = await this.signTransferTx(params)
|
|
170
|
-
|
|
180
|
+
await this.submitTransaction(signResult)
|
|
181
|
+
return signResult
|
|
171
182
|
}
|
|
172
|
-
async signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<
|
|
183
|
+
async signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult> {
|
|
173
184
|
const signResult = await this.signDeployContractTx(params)
|
|
174
|
-
|
|
185
|
+
await this.submitTransaction(signResult)
|
|
186
|
+
return signResult
|
|
175
187
|
}
|
|
176
|
-
async signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<
|
|
188
|
+
async signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult> {
|
|
177
189
|
const signResult = await this.signExecuteScriptTx(params)
|
|
178
|
-
|
|
190
|
+
await this.submitTransaction(signResult)
|
|
191
|
+
return signResult
|
|
179
192
|
}
|
|
180
|
-
async signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<
|
|
193
|
+
async signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult> {
|
|
181
194
|
const signResult = await this.signUnsignedTx(params)
|
|
182
|
-
|
|
195
|
+
await this.submitTransaction(signResult)
|
|
196
|
+
return signResult
|
|
183
197
|
}
|
|
184
198
|
|
|
185
199
|
private async usePublicKey<T extends SignerAddress>(
|
|
@@ -196,7 +210,8 @@ export abstract class SignerProvider implements SignerProviderWithoutNodeProvide
|
|
|
196
210
|
|
|
197
211
|
async signTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult> {
|
|
198
212
|
const response = await this.buildTransferTx(params)
|
|
199
|
-
|
|
213
|
+
const signature = await this.signRaw(params.signerAddress, response.txId)
|
|
214
|
+
return { ...response, signature, gasPrice: fromApiNumber256(response.gasPrice) }
|
|
200
215
|
}
|
|
201
216
|
|
|
202
217
|
async buildTransferTx(params: SignTransferTxParams): Promise<node.BuildTransactionResult> {
|
|
@@ -205,14 +220,14 @@ export abstract class SignerProvider implements SignerProviderWithoutNodeProvide
|
|
|
205
220
|
destinations: toApiDestinations(params.destinations),
|
|
206
221
|
gasPrice: toApiNumber256Optional(params.gasPrice)
|
|
207
222
|
}
|
|
208
|
-
return this.
|
|
223
|
+
return this.getNodeProvider().transactions.postTransactionsBuild(data)
|
|
209
224
|
}
|
|
210
225
|
|
|
211
226
|
async signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult> {
|
|
212
227
|
const response = await this.buildContractCreationTx(params)
|
|
213
|
-
const
|
|
228
|
+
const signature = await this.signRaw(params.signerAddress, response.txId)
|
|
214
229
|
const contractId = utils.binToHex(utils.contractIdFromAddress(response.contractAddress))
|
|
215
|
-
return { ...
|
|
230
|
+
return { ...response, contractId, signature, gasPrice: fromApiNumber256(response.gasPrice) }
|
|
216
231
|
}
|
|
217
232
|
|
|
218
233
|
async buildContractCreationTx(params: SignDeployContractTxParams): Promise<node.BuildDeployContractTxResult> {
|
|
@@ -223,12 +238,13 @@ export abstract class SignerProvider implements SignerProviderWithoutNodeProvide
|
|
|
223
238
|
issueTokenAmount: toApiNumber256Optional(params.issueTokenAmount),
|
|
224
239
|
gasPrice: toApiNumber256Optional(params.gasPrice)
|
|
225
240
|
}
|
|
226
|
-
return this.
|
|
241
|
+
return this.getNodeProvider().contracts.postContractsUnsignedTxDeployContract(data)
|
|
227
242
|
}
|
|
228
243
|
|
|
229
244
|
async signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult> {
|
|
230
245
|
const response = await this.buildScriptTx(params)
|
|
231
|
-
|
|
246
|
+
const signature = await this.signRaw(params.signerAddress, response.txId)
|
|
247
|
+
return { ...response, signature, gasPrice: fromApiNumber256(response.gasPrice) }
|
|
232
248
|
}
|
|
233
249
|
|
|
234
250
|
async buildScriptTx(params: SignExecuteScriptTxParams): Promise<node.BuildExecuteScriptTxResult> {
|
|
@@ -236,47 +252,26 @@ export abstract class SignerProvider implements SignerProviderWithoutNodeProvide
|
|
|
236
252
|
...(await this.usePublicKey(params)),
|
|
237
253
|
tokens: toApiTokens(params.tokens)
|
|
238
254
|
}
|
|
239
|
-
return this.
|
|
255
|
+
return this.getNodeProvider().contracts.postContractsUnsignedTxExecuteScript(data)
|
|
240
256
|
}
|
|
241
257
|
|
|
242
258
|
// in general, wallet should show the decoded information to user for confirmation
|
|
243
259
|
// please overwrite this function for real wallet
|
|
244
260
|
async signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult> {
|
|
245
261
|
const data = { unsignedTx: params.unsignedTx }
|
|
246
|
-
const decoded = await this.
|
|
247
|
-
|
|
262
|
+
const decoded = await this.getNodeProvider().transactions.postTransactionsDecodeUnsignedTx(data)
|
|
263
|
+
const signature = await this.signRaw(params.signerAddress, decoded.unsignedTx.txId)
|
|
264
|
+
return {
|
|
248
265
|
fromGroup: decoded.fromGroup,
|
|
249
266
|
toGroup: decoded.toGroup,
|
|
250
|
-
signerAddress: params.signerAddress,
|
|
251
267
|
unsignedTx: params.unsignedTx,
|
|
252
|
-
txId: decoded.unsignedTx.txId
|
|
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> {
|
|
263
|
-
// sign the tx
|
|
264
|
-
const signature = await this.signRaw(response.signerAddress, response.txId)
|
|
265
|
-
// return the signature back to the provider
|
|
266
|
-
return {
|
|
267
|
-
fromGroup: response.fromGroup,
|
|
268
|
-
toGroup: response.toGroup,
|
|
269
|
-
unsignedTx: response.unsignedTx,
|
|
270
|
-
txId: response.txId,
|
|
271
|
-
signature: signature
|
|
268
|
+
txId: decoded.unsignedTx.txId,
|
|
269
|
+
signature,
|
|
270
|
+
gasAmount: decoded.unsignedTx.gasAmount,
|
|
271
|
+
gasPrice: fromApiNumber256(decoded.unsignedTx.gasPrice)
|
|
272
272
|
}
|
|
273
273
|
}
|
|
274
274
|
|
|
275
|
-
async signHexString(params: SignHexStringParams): Promise<SignHexStringResult> {
|
|
276
|
-
const signature = await this.signRaw(params.signerAddress, params.hexString)
|
|
277
|
-
return { signature: signature }
|
|
278
|
-
}
|
|
279
|
-
|
|
280
275
|
async signMessage(params: SignMessageParams): Promise<SignMessageResult> {
|
|
281
276
|
const extendedMessage = extendMessage(params.message)
|
|
282
277
|
const messageHash = blake.blake2b(extendedMessage, undefined, 32)
|
|
@@ -287,7 +282,7 @@ export abstract class SignerProvider implements SignerProviderWithoutNodeProvide
|
|
|
287
282
|
abstract signRaw(signerAddress: string, hexString: string): Promise<string>
|
|
288
283
|
}
|
|
289
284
|
|
|
290
|
-
export abstract class SignerProviderWithMultipleAccounts extends
|
|
285
|
+
export abstract class SignerProviderWithMultipleAccounts extends SignerProviderSimple {
|
|
291
286
|
abstract getAccounts(): Promise<Account[]>
|
|
292
287
|
|
|
293
288
|
async getAccount(signerAddress: string): Promise<Account> {
|
|
@@ -303,12 +298,6 @@ export abstract class SignerProviderWithMultipleAccounts extends SignerProvider
|
|
|
303
298
|
abstract setSelectedAccount(address: string): Promise<void>
|
|
304
299
|
}
|
|
305
300
|
|
|
306
|
-
export interface SubmissionResult {
|
|
307
|
-
txId: string
|
|
308
|
-
fromGroup: number
|
|
309
|
-
toGroup: number
|
|
310
|
-
}
|
|
311
|
-
|
|
312
301
|
export function verifyHexString(hexString: string, publicKey: string, signature: string): boolean {
|
|
313
302
|
try {
|
|
314
303
|
const key = ec.keyFromPublic(publicKey, 'hex')
|