@alephium/web3 0.2.0-rc.33 → 0.2.0-rc.35

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.
@@ -32,7 +32,6 @@ import {
32
32
  toApiVal,
33
33
  Token,
34
34
  Val,
35
- toApiTokens,
36
35
  fromApiTokens,
37
36
  fromApiVals
38
37
  } from '../api'
@@ -808,30 +807,29 @@ export class Contract extends Artifact {
808
807
  }
809
808
  }
810
809
 
811
- async paramsForDeployment(params: BuildDeployContractTx): Promise<SignDeployContractTxParams> {
810
+ async txParamsForDeployment(
811
+ signer: SignerProvider,
812
+ params: Omit<BuildDeployContractTx, 'signerAddress'>
813
+ ): Promise<SignDeployContractTxParams> {
812
814
  const bytecode = this.buildByteCodeToDeploy(params.initialFields ? params.initialFields : {})
813
815
  const signerParams: SignDeployContractTxParams = {
814
- signerAddress: params.signerAddress,
816
+ signerAddress: (await signer.getSelectedAccount()).address,
815
817
  bytecode: bytecode,
816
- initialAttoAlphAmount: extractOptionalNumber256(params.initialAttoAlphAmount),
817
- issueTokenAmount: extractOptionalNumber256(params.issueTokenAmount),
818
- initialTokenAmounts: toApiTokens(params.initialTokenAmounts),
818
+ initialAttoAlphAmount: params.initialAttoAlphAmount,
819
+ issueTokenAmount: params.issueTokenAmount,
820
+ initialTokenAmounts: params.initialTokenAmounts,
819
821
  gasAmount: params.gasAmount,
820
- gasPrice: extractOptionalNumber256(params.gasPrice)
822
+ gasPrice: params.gasPrice
821
823
  }
822
824
  return signerParams
823
825
  }
824
826
 
825
- async transactionForDeployment(
827
+ async deploy(
826
828
  signer: SignerProvider,
827
829
  params: Omit<BuildDeployContractTx, 'signerAddress'>
828
830
  ): Promise<DeployContractTransaction> {
829
- const signerParams = await this.paramsForDeployment({
830
- ...params,
831
- signerAddress: (await signer.getSelectedAccount()).address
832
- })
833
- const response = await signer.buildContractCreationTx(signerParams)
834
- return fromApiDeployContractUnsignedTx(response)
831
+ const signerParams = await this.txParamsForDeployment(signer, params)
832
+ return signer.signAndSubmitDeployContractTx(signerParams)
835
833
  }
836
834
 
837
835
  buildByteCodeToDeploy(initialFields: Fields): string {
@@ -896,27 +894,27 @@ export class Script extends Artifact {
896
894
  return JSON.stringify(object, null, 2)
897
895
  }
898
896
 
899
- async paramsForDeployment(params: BuildExecuteScriptTx): Promise<SignExecuteScriptTxParams> {
897
+ async txParamsForExecution(
898
+ signer: SignerProvider,
899
+ params: Omit<BuildExecuteScriptTx, 'signerAddress'>
900
+ ): Promise<SignExecuteScriptTxParams> {
900
901
  const signerParams: SignExecuteScriptTxParams = {
901
- signerAddress: params.signerAddress,
902
+ signerAddress: (await signer.getSelectedAccount()).address,
902
903
  bytecode: this.buildByteCodeToDeploy(params.initialFields ? params.initialFields : {}),
903
- attoAlphAmount: extractOptionalNumber256(params.attoAlphAmount),
904
- tokens: toApiTokens(params.tokens),
904
+ attoAlphAmount: params.attoAlphAmount,
905
+ tokens: params.tokens,
905
906
  gasAmount: params.gasAmount,
906
- gasPrice: extractOptionalNumber256(params.gasPrice)
907
+ gasPrice: params.gasPrice
907
908
  }
908
909
  return signerParams
909
910
  }
910
911
 
911
- async transactionForDeployment(
912
+ async execute(
912
913
  signer: SignerProvider,
913
914
  params: Omit<BuildExecuteScriptTx, 'signerAddress'>
914
915
  ): Promise<BuildScriptTxResult> {
915
- const signerParams = await this.paramsForDeployment({
916
- ...params,
917
- signerAddress: (await signer.getSelectedAccount()).address
918
- })
919
- return await signer.buildScriptTx(signerParams)
916
+ const signerParams = await this.txParamsForExecution(signer, params)
917
+ return await signer.signAndSubmitExecuteScriptTx(signerParams)
920
918
  }
921
919
 
922
920
  buildByteCodeToDeploy(initialFields: Fields): string {
@@ -924,10 +922,6 @@ export class Script extends Artifact {
924
922
  }
925
923
  }
926
924
 
927
- function extractOptionalNumber256(v?: Val): string | undefined {
928
- return typeof v !== 'undefined' ? toApiNumber256(v) : undefined
929
- }
930
-
931
925
  function fromApiFields(vals: node.Val[], fieldsSig: node.FieldsSig): Fields {
932
926
  return fromApiVals(vals, fieldsSig.names, fieldsSig.types)
933
927
  }
@@ -1098,16 +1092,12 @@ export interface DeployContractTransaction {
1098
1092
  contractId: string
1099
1093
  }
1100
1094
 
1101
- function fromApiDeployContractUnsignedTx(result: node.BuildDeployContractTxResult): DeployContractTransaction {
1102
- return { ...result, contractId: binToHex(contractIdFromAddress(result.contractAddress)) }
1103
- }
1104
-
1105
1095
  type BuildTxParams<T> = Omit<T, 'bytecode'> & { initialFields?: Val[] }
1106
1096
 
1107
1097
  export interface BuildDeployContractTx {
1108
1098
  signerAddress: string
1109
1099
  initialFields?: Fields
1110
- initialAttoAlphAmount?: string
1100
+ initialAttoAlphAmount?: Number256
1111
1101
  initialTokenAmounts?: Token[]
1112
1102
  issueTokenAmount?: Number256
1113
1103
  gasAmount?: number
@@ -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,16 +83,18 @@ 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; contractAddress: string }>>()
89
+ assertType<Eq<SignDeployContractTxResult, SignResult<node.BuildDeployContractTxResult> & { contractId: string }>>()
96
90
 
97
91
  export interface SignExecuteScriptTxParams {
98
92
  signerAddress: string
99
93
  bytecode: string
100
- attoAlphAmount?: string
94
+ attoAlphAmount?: Number256
101
95
  tokens?: Token[]
102
96
  gasAmount?: number
103
- gasPrice?: string
97
+ gasPrice?: Number256
104
98
  }
105
99
  assertType<Eq<keyof SignExecuteScriptTxParams, keyof TxBuildParams<node.BuildExecuteScriptTx>>>()
106
100
  export interface SignExecuteScriptTxResult {
@@ -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, SignResult>>()
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,9 +132,34 @@ 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 abstract class SignerProvider {
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
+
149
+ getSelectedAccount(): Promise<Account>
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
+
156
+ signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>
157
+ // The message will be prefixed with 'Alephium Signed Message: ' before signing
158
+ // so that the resulted signature cannot be reused for building transactions.
159
+ signMessage(params: SignMessageParams): Promise<SignMessageResult>
160
+ }
161
+
162
+ export abstract class SignerProviderSimple implements SignerProvider {
150
163
  abstract get nodeProvider(): NodeProvider | undefined
151
164
  abstract getSelectedAccount(): Promise<Account>
152
165
 
@@ -157,26 +170,30 @@ export abstract class SignerProvider {
157
170
  return this.nodeProvider
158
171
  }
159
172
 
160
- async submitTransaction(unsignedTx: string, signature: string): Promise<SubmissionResult> {
161
- const params: node.SubmitTransaction = { unsignedTx: unsignedTx, signature: signature }
162
- return this.getNodeProvider().transactions.postTransactionsSubmit(params)
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)
163
176
  }
164
177
 
165
- async signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SubmissionResult> {
178
+ async signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult> {
166
179
  const signResult = await this.signTransferTx(params)
167
- return this.submitTransaction(signResult.unsignedTx, signResult.signature)
180
+ await this.submitTransaction(signResult)
181
+ return signResult
168
182
  }
169
- async signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SubmissionResult> {
183
+ async signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult> {
170
184
  const signResult = await this.signDeployContractTx(params)
171
- return this.submitTransaction(signResult.unsignedTx, signResult.signature)
185
+ await this.submitTransaction(signResult)
186
+ return signResult
172
187
  }
173
- async signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SubmissionResult> {
188
+ async signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult> {
174
189
  const signResult = await this.signExecuteScriptTx(params)
175
- return this.submitTransaction(signResult.unsignedTx, signResult.signature)
190
+ await this.submitTransaction(signResult)
191
+ return signResult
176
192
  }
177
- async signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SubmissionResult> {
193
+ async signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult> {
178
194
  const signResult = await this.signUnsignedTx(params)
179
- return this.submitTransaction(signResult.unsignedTx, signResult.signature)
195
+ await this.submitTransaction(signResult)
196
+ return signResult
180
197
  }
181
198
 
182
199
  private async usePublicKey<T extends SignerAddress>(
@@ -193,7 +210,8 @@ export abstract class SignerProvider {
193
210
 
194
211
  async signTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult> {
195
212
  const response = await this.buildTransferTx(params)
196
- return this.handleSign({ signerAddress: params.signerAddress, ...response })
213
+ const signature = await this.signRaw(params.signerAddress, response.txId)
214
+ return { ...response, signature, gasPrice: fromApiNumber256(response.gasPrice) }
197
215
  }
198
216
 
199
217
  async buildTransferTx(params: SignTransferTxParams): Promise<node.BuildTransactionResult> {
@@ -207,9 +225,9 @@ export abstract class SignerProvider {
207
225
 
208
226
  async signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult> {
209
227
  const response = await this.buildContractCreationTx(params)
210
- const result = await this.handleSign({ signerAddress: params.signerAddress, ...response })
228
+ const signature = await this.signRaw(params.signerAddress, response.txId)
211
229
  const contractId = utils.binToHex(utils.contractIdFromAddress(response.contractAddress))
212
- return { ...result, contractId: contractId, contractAddress: response.contractAddress }
230
+ return { ...response, contractId, signature, gasPrice: fromApiNumber256(response.gasPrice) }
213
231
  }
214
232
 
215
233
  async buildContractCreationTx(params: SignDeployContractTxParams): Promise<node.BuildDeployContractTxResult> {
@@ -225,13 +243,16 @@ export abstract class SignerProvider {
225
243
 
226
244
  async signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult> {
227
245
  const response = await this.buildScriptTx(params)
228
- return this.handleSign({ signerAddress: params.signerAddress, ...response })
246
+ const signature = await this.signRaw(params.signerAddress, response.txId)
247
+ return { ...response, signature, gasPrice: fromApiNumber256(response.gasPrice) }
229
248
  }
230
249
 
231
250
  async buildScriptTx(params: SignExecuteScriptTxParams): Promise<node.BuildExecuteScriptTxResult> {
232
251
  const data: node.BuildExecuteScriptTx = {
233
252
  ...(await this.usePublicKey(params)),
234
- tokens: toApiTokens(params.tokens)
253
+ attoAlphAmount: toApiNumber256Optional(params.attoAlphAmount),
254
+ tokens: toApiTokens(params.tokens),
255
+ gasPrice: toApiNumber256Optional(params.gasPrice)
235
256
  }
236
257
  return this.getNodeProvider().contracts.postContractsUnsignedTxExecuteScript(data)
237
258
  }
@@ -241,39 +262,18 @@ export abstract class SignerProvider {
241
262
  async signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult> {
242
263
  const data = { unsignedTx: params.unsignedTx }
243
264
  const decoded = await this.getNodeProvider().transactions.postTransactionsDecodeUnsignedTx(data)
244
- return this.handleSign({
265
+ const signature = await this.signRaw(params.signerAddress, decoded.unsignedTx.txId)
266
+ return {
245
267
  fromGroup: decoded.fromGroup,
246
268
  toGroup: decoded.toGroup,
247
- signerAddress: params.signerAddress,
248
269
  unsignedTx: params.unsignedTx,
249
- txId: decoded.unsignedTx.txId
250
- })
251
- }
252
-
253
- protected async handleSign(response: {
254
- fromGroup: number
255
- toGroup: number
256
- signerAddress: string
257
- unsignedTx: string
258
- txId: string
259
- }): Promise<SignResult> {
260
- // sign the tx
261
- const signature = await this.signRaw(response.signerAddress, response.txId)
262
- // return the signature back to the provider
263
- return {
264
- fromGroup: response.fromGroup,
265
- toGroup: response.toGroup,
266
- unsignedTx: response.unsignedTx,
267
- txId: response.txId,
268
- signature: signature
270
+ txId: decoded.unsignedTx.txId,
271
+ signature,
272
+ gasAmount: decoded.unsignedTx.gasAmount,
273
+ gasPrice: fromApiNumber256(decoded.unsignedTx.gasPrice)
269
274
  }
270
275
  }
271
276
 
272
- async signHexString(params: SignHexStringParams): Promise<SignHexStringResult> {
273
- const signature = await this.signRaw(params.signerAddress, params.hexString)
274
- return { signature: signature }
275
- }
276
-
277
277
  async signMessage(params: SignMessageParams): Promise<SignMessageResult> {
278
278
  const extendedMessage = extendMessage(params.message)
279
279
  const messageHash = blake.blake2b(extendedMessage, undefined, 32)
@@ -284,7 +284,7 @@ export abstract class SignerProvider {
284
284
  abstract signRaw(signerAddress: string, hexString: string): Promise<string>
285
285
  }
286
286
 
287
- export abstract class SignerProviderWithMultipleAccounts extends SignerProvider {
287
+ export abstract class SignerProviderWithMultipleAccounts extends SignerProviderSimple {
288
288
  abstract getAccounts(): Promise<Account[]>
289
289
 
290
290
  async getAccount(signerAddress: string): Promise<Account> {
@@ -300,31 +300,6 @@ export abstract class SignerProviderWithMultipleAccounts extends SignerProvider
300
300
  abstract setSelectedAccount(address: string): Promise<void>
301
301
  }
302
302
 
303
- export class SignerProviderWrapper extends SignerProvider {
304
- signer: SignerProvider
305
- nodeProvider: NodeProvider
306
-
307
- constructor(signer: SignerProvider, nodeProvider: NodeProvider) {
308
- super()
309
- this.signer = signer
310
- this.nodeProvider = nodeProvider
311
- }
312
-
313
- getSelectedAccount(): Promise<Account> {
314
- return this.signer.getSelectedAccount()
315
- }
316
-
317
- signRaw(signerAddress: string, hexString: string): Promise<string> {
318
- return this.signer.signRaw(signerAddress, hexString)
319
- }
320
- }
321
-
322
- export interface SubmissionResult {
323
- txId: string
324
- fromGroup: number
325
- toGroup: number
326
- }
327
-
328
303
  export function verifyHexString(hexString: string, publicKey: string, signature: string): boolean {
329
304
  try {
330
305
  const key = ec.keyFromPublic(publicKey, 'hex')