@alephium/web3 0.2.0-rc.33 → 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.
@@ -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; contractAddress: 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, 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,7 +243,8 @@ 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> {
@@ -241,39 +260,18 @@ export abstract class SignerProvider {
241
260
  async signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult> {
242
261
  const data = { unsignedTx: params.unsignedTx }
243
262
  const decoded = await this.getNodeProvider().transactions.postTransactionsDecodeUnsignedTx(data)
244
- return this.handleSign({
263
+ const signature = await this.signRaw(params.signerAddress, decoded.unsignedTx.txId)
264
+ return {
245
265
  fromGroup: decoded.fromGroup,
246
266
  toGroup: decoded.toGroup,
247
- signerAddress: params.signerAddress,
248
267
  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
268
+ txId: decoded.unsignedTx.txId,
269
+ signature,
270
+ gasAmount: decoded.unsignedTx.gasAmount,
271
+ gasPrice: fromApiNumber256(decoded.unsignedTx.gasPrice)
269
272
  }
270
273
  }
271
274
 
272
- async signHexString(params: SignHexStringParams): Promise<SignHexStringResult> {
273
- const signature = await this.signRaw(params.signerAddress, params.hexString)
274
- return { signature: signature }
275
- }
276
-
277
275
  async signMessage(params: SignMessageParams): Promise<SignMessageResult> {
278
276
  const extendedMessage = extendMessage(params.message)
279
277
  const messageHash = blake.blake2b(extendedMessage, undefined, 32)
@@ -284,7 +282,7 @@ export abstract class SignerProvider {
284
282
  abstract signRaw(signerAddress: string, hexString: string): Promise<string>
285
283
  }
286
284
 
287
- export abstract class SignerProviderWithMultipleAccounts extends SignerProvider {
285
+ export abstract class SignerProviderWithMultipleAccounts extends SignerProviderSimple {
288
286
  abstract getAccounts(): Promise<Account[]>
289
287
 
290
288
  async getAccount(signerAddress: string): Promise<Account> {
@@ -300,31 +298,6 @@ export abstract class SignerProviderWithMultipleAccounts extends SignerProvider
300
298
  abstract setSelectedAccount(address: string): Promise<void>
301
299
  }
302
300
 
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
301
  export function verifyHexString(hexString: string, publicKey: string, signature: string): boolean {
329
302
  try {
330
303
  const key = ec.keyFromPublic(publicKey, 'hex')