@alephium/web3 0.3.0-rc.3 → 0.3.0-rc.5

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.
@@ -22,133 +22,40 @@ import {
22
22
  fromApiNumber256,
23
23
  fromApiTokens,
24
24
  NodeProvider,
25
- Number256,
26
25
  toApiNumber256,
27
26
  toApiNumber256Optional,
28
- toApiTokens,
29
- Token
27
+ toApiTokens
30
28
  } from '../api'
31
29
  import { node } from '../api'
32
30
  import * as utils from '../utils'
33
- import { Eq, assertType } from '../utils'
34
31
  import blake from 'blakejs'
35
-
36
- export type OutputRef = node.OutputRef
32
+ import {
33
+ Account,
34
+ Address,
35
+ EnableOptionsBase,
36
+ Destination,
37
+ SignDeployContractTxParams,
38
+ SignDeployContractTxResult,
39
+ SignerAddress,
40
+ SignExecuteScriptTxParams,
41
+ SignExecuteScriptTxResult,
42
+ SignMessageParams,
43
+ SignMessageResult,
44
+ SignTransferTxParams,
45
+ SignTransferTxResult,
46
+ SignUnsignedTxParams,
47
+ SignUnsignedTxResult,
48
+ SubmissionResult,
49
+ SubmitTransactionParams
50
+ } from './types'
37
51
 
38
52
  const ec = new EC('secp256k1')
39
53
 
40
- export interface Account {
41
- address: string
42
- group: number
43
- publicKey: string
44
- }
45
-
46
- export type SignerAddress = { signerAddress: string }
47
- type TxBuildParams<T> = Omit<T, 'fromPublicKey' | 'targetBlockHash'> & SignerAddress
48
- type SignResult<T> = Omit<T, 'gasPrice'> & { signature: string; gasPrice: Number256 }
49
-
50
- export interface SignTransferTxParams {
51
- signerAddress: string
52
- destinations: Destination[]
53
- utxos?: OutputRef[]
54
- gasAmount?: number
55
- gasPrice?: Number256
56
- }
57
- assertType<Eq<keyof SignTransferTxParams, keyof TxBuildParams<node.BuildTransaction>>>()
58
- export interface SignTransferTxResult {
59
- fromGroup: number
60
- toGroup: number
61
- unsignedTx: string
62
- txId: string
63
- signature: string
64
- gasAmount: number
65
- gasPrice: Number256
66
- }
67
- assertType<Eq<SignTransferTxResult, SignResult<node.BuildTransactionResult>>>()
68
-
69
- export interface SignDeployContractTxParams {
70
- signerAddress: string
71
- bytecode: string
72
- initialAttoAlphAmount?: Number256
73
- initialTokenAmounts?: Token[]
74
- issueTokenAmount?: Number256
75
- gasAmount?: number
76
- gasPrice?: Number256
77
- }
78
- assertType<Eq<keyof SignDeployContractTxParams, keyof TxBuildParams<node.BuildDeployContractTx>>>()
79
- export interface SignDeployContractTxResult {
80
- fromGroup: number
81
- toGroup: number
82
- unsignedTx: string
83
- txId: string
84
- signature: string
85
- contractId: string
86
- contractAddress: string
87
- gasAmount: number
88
- gasPrice: Number256
89
- }
90
- assertType<Eq<SignDeployContractTxResult, SignResult<node.BuildDeployContractTxResult> & { contractId: string }>>()
91
-
92
- export interface SignExecuteScriptTxParams {
93
- signerAddress: string
94
- bytecode: string
95
- attoAlphAmount?: Number256
96
- tokens?: Token[]
97
- gasAmount?: number
98
- gasPrice?: Number256
99
- }
100
- assertType<Eq<keyof SignExecuteScriptTxParams, keyof TxBuildParams<node.BuildExecuteScriptTx>>>()
101
- export interface SignExecuteScriptTxResult {
102
- fromGroup: number
103
- toGroup: number
104
- unsignedTx: string
105
- txId: string
106
- signature: string
107
- gasAmount: number
108
- gasPrice: Number256
109
- }
110
- assertType<Eq<SignExecuteScriptTxResult, SignResult<node.BuildExecuteScriptTxResult>>>()
111
-
112
- export interface SignUnsignedTxParams {
113
- signerAddress: string
114
- unsignedTx: string
115
- }
116
- assertType<Eq<SignUnsignedTxParams, { unsignedTx: string } & SignerAddress>>()
117
- export interface SignUnsignedTxResult {
118
- fromGroup: number
119
- toGroup: number
120
- unsignedTx: string
121
- txId: string
122
- signature: string
123
- gasAmount: number
124
- gasPrice: Number256
125
- }
126
- assertType<Eq<SignUnsignedTxResult, SignTransferTxResult>>
127
-
128
- export interface SignMessageParams {
129
- signerAddress: string
130
- message: string
131
- }
132
- assertType<Eq<SignMessageParams, { message: string } & SignerAddress>>()
133
- export interface SignMessageResult {
134
- signature: string
135
- }
136
-
137
- export interface SubmitTransactionParams {
138
- unsignedTx: string
139
- signature: string
140
- }
141
- export interface SubmissionResult {
142
- txId: string
143
- fromGroup: number
144
- toGroup: number
145
- }
146
-
147
54
  export interface SignerProvider {
148
55
  get nodeProvider(): NodeProvider | undefined
149
56
  get explorerProvider(): ExplorerProvider | undefined
150
57
 
151
- getSelectedAccount(): Promise<Account>
58
+ getSelectedAddress(): Promise<Address>
152
59
 
153
60
  signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>
154
61
  signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>
@@ -161,11 +68,24 @@ export interface SignerProvider {
161
68
  signMessage(params: SignMessageParams): Promise<SignMessageResult>
162
69
  }
163
70
 
71
+ // Abstraction for interactive signer (e.g. WalletConnect instance, Extension wallet object)
72
+ export interface InteractiveSignerProvider<EnableOptions extends EnableOptionsBase = EnableOptionsBase>
73
+ extends SignerProvider {
74
+ enable(opt?: EnableOptions): Promise<void>
75
+ disconnect(): Promise<void>
76
+ }
77
+
164
78
  export abstract class SignerProviderSimple implements SignerProvider {
165
79
  abstract get nodeProvider(): NodeProvider | undefined
166
80
  abstract get explorerProvider(): ExplorerProvider | undefined
81
+
167
82
  abstract getSelectedAccount(): Promise<Account>
168
83
 
84
+ async getSelectedAddress(): Promise<Address> {
85
+ const account = await this.getSelectedAccount()
86
+ return account.address
87
+ }
88
+
169
89
  private getNodeProvider(): NodeProvider {
170
90
  if (this.nodeProvider === undefined) {
171
91
  throw Error('The signer does not contain a node provider')
@@ -199,16 +119,14 @@ export abstract class SignerProviderSimple implements SignerProvider {
199
119
  return signResult
200
120
  }
201
121
 
122
+ protected abstract getPublicKey(address: string): Promise<string>
123
+
202
124
  private async usePublicKey<T extends SignerAddress>(
203
125
  params: T
204
126
  ): Promise<Omit<T, 'signerAddress'> & { fromPublicKey: string }> {
205
127
  const { signerAddress, ...restParams } = params
206
- const selectedAccount = await this.getSelectedAccount()
207
- if (signerAddress !== selectedAccount.address) {
208
- throw new Error('The signer address is not the selected address')
209
- } else {
210
- return { fromPublicKey: selectedAccount.publicKey, ...restParams }
211
- }
128
+ const publicKey = await this.getPublicKey(signerAddress)
129
+ return { fromPublicKey: publicKey, ...restParams }
212
130
  }
213
131
 
214
132
  async signTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult> {
@@ -288,6 +206,8 @@ export abstract class SignerProviderSimple implements SignerProvider {
288
206
  }
289
207
 
290
208
  export abstract class SignerProviderWithMultipleAccounts extends SignerProviderSimple {
209
+ abstract setSelectedAddress(address: string): Promise<void>
210
+
291
211
  abstract getAccounts(): Promise<Account[]>
292
212
 
293
213
  async getAccount(signerAddress: string): Promise<Account> {
@@ -300,7 +220,46 @@ export abstract class SignerProviderWithMultipleAccounts extends SignerProviderS
300
220
  }
301
221
  }
302
222
 
303
- abstract setSelectedAccount(address: string): Promise<void>
223
+ async getPublicKey(signerAddress: string): Promise<string> {
224
+ const account = await this.getAccount(signerAddress)
225
+ return account.publicKey
226
+ }
227
+ }
228
+
229
+ export abstract class SignerProviderWithCachedAccounts<T extends Account> extends SignerProviderWithMultipleAccounts {
230
+ private _selectedAccount: T | undefined = undefined
231
+ protected readonly _accounts = new Map<Address, T>()
232
+
233
+ getSelectedAccount(): Promise<T> {
234
+ if (this._selectedAccount === undefined) {
235
+ throw Error('No account is selected yet')
236
+ } else {
237
+ return Promise.resolve(this._selectedAccount)
238
+ }
239
+ }
240
+
241
+ setSelectedAddress(address: string): Promise<void> {
242
+ const accountOpt = this._accounts.get(address)
243
+ if (accountOpt === undefined) {
244
+ throw Error('The address is not in the accounts')
245
+ } else {
246
+ this._selectedAccount = accountOpt
247
+ return Promise.resolve()
248
+ }
249
+ }
250
+
251
+ getAccounts(): Promise<T[]> {
252
+ return Promise.resolve(Array.from(this._accounts.values()))
253
+ }
254
+
255
+ override async getAccount(address: string): Promise<T> {
256
+ const account = this._accounts.get(address)
257
+ if (account === undefined) {
258
+ throw Error('The address is not in the accounts')
259
+ }
260
+
261
+ return Promise.resolve(account)
262
+ }
304
263
  }
305
264
 
306
265
  export function verifyHexString(hexString: string, publicKey: string, signature: string): boolean {
@@ -322,15 +281,6 @@ export function verifySignedMessage(message: string, publicKey: string, signatur
322
281
  return verifyHexString(utils.binToHex(messageHash), publicKey, signature)
323
282
  }
324
283
 
325
- export interface Destination {
326
- address: string
327
- attoAlphAmount: Number256
328
- tokens?: Token[]
329
- lockTime?: number
330
- message?: string
331
- }
332
- assertType<Eq<keyof Destination, keyof node.Destination>>
333
-
334
284
  export function toApiDestination(data: Destination): node.Destination {
335
285
  return { ...data, attoAlphAmount: toApiNumber256(data.attoAlphAmount), tokens: toApiTokens(data.tokens) }
336
286
  }
@@ -0,0 +1,148 @@
1
+ /*
2
+ Copyright 2018 - 2022 The Alephium Authors
3
+ This file is part of the alephium project.
4
+
5
+ The library is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ The library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ import { Number256, Token } from '../api'
20
+ import { node } from '../api'
21
+ import { Eq, assertType } from '../utils'
22
+
23
+ export type Address = string
24
+
25
+ export type OutputRef = node.OutputRef
26
+
27
+ export interface Destination {
28
+ address: string
29
+ attoAlphAmount: Number256
30
+ tokens?: Token[]
31
+ lockTime?: number
32
+ message?: string
33
+ }
34
+ assertType<Eq<keyof Destination, keyof node.Destination>>
35
+
36
+ export interface Account {
37
+ address: string
38
+ group: number
39
+ publicKey: string
40
+ }
41
+
42
+ export type SignerAddress = { signerAddress: string }
43
+ type TxBuildParams<T> = Omit<T, 'fromPublicKey' | 'targetBlockHash'> & SignerAddress
44
+ type SignResult<T> = Omit<T, 'gasPrice'> & { signature: string; gasPrice: Number256 }
45
+
46
+ export interface SignTransferTxParams {
47
+ signerAddress: string
48
+ destinations: Destination[]
49
+ utxos?: OutputRef[]
50
+ gasAmount?: number
51
+ gasPrice?: Number256
52
+ }
53
+ assertType<Eq<keyof SignTransferTxParams, keyof TxBuildParams<node.BuildTransaction>>>()
54
+ export interface SignTransferTxResult {
55
+ fromGroup: number
56
+ toGroup: number
57
+ unsignedTx: string
58
+ txId: string
59
+ signature: string
60
+ gasAmount: number
61
+ gasPrice: Number256
62
+ }
63
+ assertType<Eq<SignTransferTxResult, SignResult<node.BuildTransactionResult>>>()
64
+
65
+ export interface SignDeployContractTxParams {
66
+ signerAddress: string
67
+ bytecode: string
68
+ initialAttoAlphAmount?: Number256
69
+ initialTokenAmounts?: Token[]
70
+ issueTokenAmount?: Number256
71
+ gasAmount?: number
72
+ gasPrice?: Number256
73
+ }
74
+ assertType<Eq<keyof SignDeployContractTxParams, keyof TxBuildParams<node.BuildDeployContractTx>>>()
75
+ export interface SignDeployContractTxResult {
76
+ fromGroup: number
77
+ toGroup: number
78
+ unsignedTx: string
79
+ txId: string
80
+ signature: string
81
+ contractId: string
82
+ contractAddress: string
83
+ gasAmount: number
84
+ gasPrice: Number256
85
+ }
86
+ assertType<Eq<SignDeployContractTxResult, SignResult<node.BuildDeployContractTxResult> & { contractId: string }>>()
87
+
88
+ export interface SignExecuteScriptTxParams {
89
+ signerAddress: string
90
+ bytecode: string
91
+ attoAlphAmount?: Number256
92
+ tokens?: Token[]
93
+ gasAmount?: number
94
+ gasPrice?: Number256
95
+ }
96
+ assertType<Eq<keyof SignExecuteScriptTxParams, keyof TxBuildParams<node.BuildExecuteScriptTx>>>()
97
+ export interface SignExecuteScriptTxResult {
98
+ fromGroup: number
99
+ toGroup: number
100
+ unsignedTx: string
101
+ txId: string
102
+ signature: string
103
+ gasAmount: number
104
+ gasPrice: Number256
105
+ }
106
+ assertType<Eq<SignExecuteScriptTxResult, SignResult<node.BuildExecuteScriptTxResult>>>()
107
+
108
+ export interface SignUnsignedTxParams {
109
+ signerAddress: string
110
+ unsignedTx: string
111
+ }
112
+ assertType<Eq<SignUnsignedTxParams, { unsignedTx: string } & SignerAddress>>()
113
+ export interface SignUnsignedTxResult {
114
+ fromGroup: number
115
+ toGroup: number
116
+ unsignedTx: string
117
+ txId: string
118
+ signature: string
119
+ gasAmount: number
120
+ gasPrice: Number256
121
+ }
122
+ assertType<Eq<SignUnsignedTxResult, SignTransferTxResult>>
123
+
124
+ export interface SignMessageParams {
125
+ signerAddress: string
126
+ message: string
127
+ }
128
+ assertType<Eq<SignMessageParams, { message: string } & SignerAddress>>()
129
+ export interface SignMessageResult {
130
+ signature: string
131
+ }
132
+
133
+ export interface SubmitTransactionParams {
134
+ unsignedTx: string
135
+ signature: string
136
+ }
137
+ export interface SubmissionResult {
138
+ txId: string
139
+ fromGroup: number
140
+ toGroup: number
141
+ }
142
+
143
+ export interface EnableOptionsBase {
144
+ // chainGroup - specify whether to use addresses from a specific group
145
+ chainGroup?: number
146
+ onDisconnected: () => Promise<void>
147
+ onNetworkChanged: (network: { networkName: string; networkId: number }) => Promise<void>
148
+ }
@@ -17,3 +17,4 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
19
  export * from './status'
20
+ export * from './sign-verify'
@@ -0,0 +1,38 @@
1
+ /*
2
+ Copyright 2018 - 2022 The Alephium Authors
3
+ This file is part of the alephium project.
4
+
5
+ The library is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ The library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ import * as utils from '../utils'
20
+ import { ec as EC } from 'elliptic'
21
+
22
+ const ec = new EC('secp256k1')
23
+
24
+ export function transactionSign(txHash: string, privateKey: string): string {
25
+ const keyPair = ec.keyFromPrivate(privateKey)
26
+ const signature = keyPair.sign(txHash)
27
+
28
+ return utils.encodeSignature(signature)
29
+ }
30
+
31
+ export function transactionVerifySignature(txHash: string, publicKey: string, signature: string): boolean {
32
+ try {
33
+ const key = ec.keyFromPublic(publicKey, 'hex')
34
+ return key.verify(txHash, utils.signatureDecode(ec, signature))
35
+ } catch (error) {
36
+ return false
37
+ }
38
+ }
package/webpack.config.js CHANGED
@@ -41,6 +41,7 @@ module.exports = {
41
41
  fs: false,
42
42
  stream: require.resolve('stream-browserify'),
43
43
  crypto: require.resolve('crypto-browserify'),
44
+ path: require.resolve('path-browserify'),
44
45
  buffer: require.resolve('buffer/')
45
46
  }
46
47
  },