@alephium/web3 0.5.0-rc.2 → 0.5.0-rc.21

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.
Files changed (51) hide show
  1. package/dist/alephium-web3.min.js +1 -1
  2. package/dist/alephium-web3.min.js.LICENSE.txt +2 -0
  3. package/dist/alephium-web3.min.js.map +1 -1
  4. package/dist/src/api/api-alephium.d.ts +113 -26
  5. package/dist/src/api/api-alephium.js +62 -18
  6. package/dist/src/api/api-explorer.d.ts +222 -52
  7. package/dist/src/api/api-explorer.js +17 -15
  8. package/dist/src/api/explorer-provider.d.ts +18 -0
  9. package/dist/src/api/explorer-provider.js +65 -0
  10. package/dist/src/api/index.d.ts +2 -41
  11. package/dist/src/api/index.js +6 -116
  12. package/dist/src/api/node-provider.d.ts +21 -0
  13. package/dist/src/api/node-provider.js +68 -0
  14. package/dist/src/api/types.d.ts +10 -2
  15. package/dist/src/api/types.js +23 -3
  16. package/dist/src/contract/contract.d.ts +15 -7
  17. package/dist/src/contract/contract.js +62 -42
  18. package/dist/src/signer/signer.d.ts +25 -30
  19. package/dist/src/signer/signer.js +57 -30
  20. package/dist/src/signer/tx-builder.d.ts +2 -7
  21. package/dist/src/signer/tx-builder.js +10 -7
  22. package/dist/src/signer/types.d.ts +12 -16
  23. package/dist/src/transaction/sign-verify.d.ts +3 -2
  24. package/dist/src/transaction/sign-verify.js +4 -14
  25. package/dist/src/utils/index.d.ts +2 -0
  26. package/dist/src/utils/index.js +2 -0
  27. package/dist/src/utils/number.d.ts +18 -0
  28. package/dist/src/utils/number.fixture.d.ts +12 -0
  29. package/dist/src/utils/number.fixture.js +189 -0
  30. package/dist/src/utils/number.js +148 -0
  31. package/dist/src/utils/sign.d.ts +3 -0
  32. package/dist/src/utils/sign.js +89 -0
  33. package/dist/src/utils/utils.d.ts +4 -3
  34. package/dist/src/utils/utils.js +25 -10
  35. package/package.json +7 -5
  36. package/src/api/api-alephium.ts +260 -207
  37. package/src/api/api-explorer.ts +327 -126
  38. package/src/api/explorer-provider.ts +78 -0
  39. package/src/api/index.ts +2 -146
  40. package/src/api/node-provider.ts +84 -0
  41. package/src/api/types.ts +36 -3
  42. package/src/contract/contract.ts +80 -49
  43. package/src/signer/signer.ts +87 -66
  44. package/src/signer/tx-builder.ts +13 -7
  45. package/src/signer/types.ts +22 -11
  46. package/src/transaction/sign-verify.ts +10 -15
  47. package/src/utils/index.ts +2 -0
  48. package/src/utils/number.fixture.ts +187 -0
  49. package/src/utils/number.ts +162 -0
  50. package/src/utils/sign.ts +66 -0
  51. package/src/utils/utils.ts +26 -10
@@ -16,7 +16,8 @@ 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 { ec as EC } from 'elliptic'
19
+ import { Buffer } from 'buffer/'
20
+ import { createHash } from 'crypto'
20
21
  import { ExplorerProvider, fromApiNumber256, fromApiTokens, NodeProvider, toApiNumber256, toApiTokens } from '../api'
21
22
  import { node } from '../api'
22
23
  import * as utils from '../utils'
@@ -39,57 +40,58 @@ import {
39
40
  SignUnsignedTxResult,
40
41
  SubmissionResult,
41
42
  SubmitTransactionParams,
42
- ExtSignTransferTxParams,
43
- ExtSignDeployContractTxParams,
44
- ExtSignExecuteScriptTxParams,
45
- ExtSignUnsignedTxParams,
46
- ExtSignMessageParams
43
+ KeyType,
44
+ MessageHasher
47
45
  } from './types'
48
46
  import { TransactionBuilder } from './tx-builder'
47
+ import { addressFromPublicKey, groupOfAddress } from '../utils'
49
48
 
50
- const ec = new EC('secp256k1')
49
+ export abstract class SignerProvider {
50
+ abstract get nodeProvider(): NodeProvider | undefined
51
+ abstract get explorerProvider(): ExplorerProvider | undefined
51
52
 
52
- export interface SignerProvider {
53
- get nodeProvider(): NodeProvider | undefined
54
- get explorerProvider(): ExplorerProvider | undefined
53
+ protected abstract unsafeGetSelectedAccount(): Promise<Account>
54
+ async getSelectedAccount(): Promise<Account> {
55
+ const account = await this.unsafeGetSelectedAccount()
56
+ SignerProvider.validateAccount(account)
57
+ return account
58
+ }
55
59
 
56
- getSelectedAddress(): Promise<Address>
60
+ static validateAccount(account: Account): void {
61
+ const derivedAddress = addressFromPublicKey(account.publicKey, account.keyType)
62
+ const derivedGroup = groupOfAddress(derivedAddress)
63
+ if (derivedAddress !== account.address || derivedGroup !== account.group) {
64
+ throw Error(`Invalid accounot data: ${JSON.stringify(account)}`)
65
+ }
66
+ }
57
67
 
58
- signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>
59
- signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>
60
- signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>
61
- signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>
68
+ abstract signAndSubmitTransferTx(params: SignTransferTxParams): Promise<SignTransferTxResult>
69
+ abstract signAndSubmitDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult>
70
+ abstract signAndSubmitExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>
71
+ abstract signAndSubmitUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>
62
72
 
63
- signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>
73
+ abstract signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult>
64
74
  // The message will be prefixed with 'Alephium Signed Message: ' before signing
65
75
  // so that the resulted signature cannot be reused for building transactions.
66
- signMessage(params: SignMessageParams): Promise<SignMessageResult>
76
+ abstract signMessage(params: SignMessageParams): Promise<SignMessageResult>
67
77
  }
68
78
 
69
79
  // Abstraction for interactive signer (e.g. WalletConnect instance, Extension wallet object)
70
- export interface InteractiveSignerProvider<EnableOptions extends EnableOptionsBase = EnableOptionsBase>
71
- extends SignerProvider {
72
- enable(opt?: EnableOptions): Promise<Address>
73
- disconnect(): Promise<void>
74
-
75
- // Methods inherited from SignerProvider, but require networkId in the params
76
- signAndSubmitTransferTx(params: ExtSignTransferTxParams): Promise<SignTransferTxResult>
77
- signAndSubmitDeployContractTx(params: ExtSignDeployContractTxParams): Promise<SignDeployContractTxResult>
78
- signAndSubmitExecuteScriptTx(params: ExtSignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult>
79
- signAndSubmitUnsignedTx(params: ExtSignUnsignedTxParams): Promise<SignUnsignedTxResult>
80
- signUnsignedTx(params: ExtSignUnsignedTxParams): Promise<SignUnsignedTxResult>
81
- signMessage(params: ExtSignMessageParams): Promise<SignMessageResult>
82
- }
83
-
84
- export abstract class SignerProviderSimple extends TransactionBuilder implements SignerProvider {
85
- abstract get explorerProvider(): ExplorerProvider | undefined
80
+ export abstract class InteractiveSignerProvider<
81
+ EnableOptions extends EnableOptionsBase = EnableOptionsBase
82
+ > extends SignerProvider {
83
+ protected abstract unsafeEnable(opt?: EnableOptions): Promise<Account>
84
+ async enable(opt?: EnableOptions): Promise<Account> {
85
+ const account = await this.unsafeEnable(opt)
86
+ SignerProvider.validateAccount(account)
87
+ return account
88
+ }
86
89
 
87
- abstract getSelectedAccount(): Promise<Account>
90
+ abstract disconnect(): Promise<void>
91
+ }
88
92
 
89
- async getSelectedAddress(): Promise<Address> {
90
- const account = await this.getSelectedAccount()
91
- return account.address
92
- }
93
+ export abstract class SignerProviderSimple extends SignerProvider {
94
+ abstract override get nodeProvider(): NodeProvider
93
95
 
94
96
  async submitTransaction(params: SubmitTransactionParams): Promise<SubmissionResult> {
95
97
  const data: node.SubmitTransaction = { unsignedTx: params.unsignedTx, signature: params.signature }
@@ -133,8 +135,11 @@ export abstract class SignerProviderSimple extends TransactionBuilder implements
133
135
  return { signature, ...response }
134
136
  }
135
137
 
136
- override async buildTransferTx(params: SignTransferTxParams): Promise<Omit<SignTransferTxResult, 'signature'>> {
137
- return super.buildTransferTx(params, await this.getPublicKey(params.signerAddress))
138
+ async buildTransferTx(params: SignTransferTxParams): Promise<Omit<SignTransferTxResult, 'signature'>> {
139
+ return TransactionBuilder.from(this.nodeProvider).buildTransferTx(
140
+ params,
141
+ await this.getPublicKey(params.signerAddress)
142
+ )
138
143
  }
139
144
 
140
145
  async signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult> {
@@ -143,10 +148,13 @@ export abstract class SignerProviderSimple extends TransactionBuilder implements
143
148
  return { signature, ...response }
144
149
  }
145
150
 
146
- override async buildDeployContractTx(
151
+ async buildDeployContractTx(
147
152
  params: SignDeployContractTxParams
148
153
  ): Promise<Omit<SignDeployContractTxResult, 'signature'>> {
149
- return super.buildDeployContractTx(params, await this.getPublicKey(params.signerAddress))
154
+ return TransactionBuilder.from(this.nodeProvider).buildDeployContractTx(
155
+ params,
156
+ await this.getPublicKey(params.signerAddress)
157
+ )
150
158
  }
151
159
 
152
160
  async signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult> {
@@ -155,24 +163,24 @@ export abstract class SignerProviderSimple extends TransactionBuilder implements
155
163
  return { signature, ...response }
156
164
  }
157
165
 
158
- override async buildExecuteScriptTx(
159
- params: SignExecuteScriptTxParams
160
- ): Promise<Omit<SignExecuteScriptTxResult, 'signature'>> {
161
- return super.buildExecuteScriptTx(params, await this.getPublicKey(params.signerAddress))
166
+ async buildExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<Omit<SignExecuteScriptTxResult, 'signature'>> {
167
+ return TransactionBuilder.from(this.nodeProvider).buildExecuteScriptTx(
168
+ params,
169
+ await this.getPublicKey(params.signerAddress)
170
+ )
162
171
  }
163
172
 
164
173
  // in general, wallet should show the decoded information to user for confirmation
165
174
  // please overwrite this function for real wallet
166
175
  async signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult> {
167
- const response = await this.buildUnsignedTx(params)
176
+ const response = await TransactionBuilder.from(this.nodeProvider).buildUnsignedTx(params)
168
177
  const signature = await this.signRaw(params.signerAddress, response.txId)
169
178
  return { signature, ...response }
170
179
  }
171
180
 
172
181
  async signMessage(params: SignMessageParams): Promise<SignMessageResult> {
173
- const extendedMessage = extendMessage(params.message)
174
- const messageHash = blake.blake2b(extendedMessage, undefined, 32)
175
- const signature = await this.signRaw(params.signerAddress, utils.binToHex(messageHash))
182
+ const messageHash = hashMessage(params.message, params.messageHasher)
183
+ const signature = await this.signRaw(params.signerAddress, messageHash)
176
184
  return { signature: signature }
177
185
  }
178
186
 
@@ -180,7 +188,7 @@ export abstract class SignerProviderSimple extends TransactionBuilder implements
180
188
  }
181
189
 
182
190
  export abstract class SignerProviderWithMultipleAccounts extends SignerProviderSimple {
183
- abstract setSelectedAddress(address: string): Promise<void>
191
+ abstract setSelectedAccount(address: string): Promise<void>
184
192
 
185
193
  abstract getAccounts(): Promise<Account[]>
186
194
 
@@ -204,7 +212,7 @@ export abstract class SignerProviderWithCachedAccounts<T extends Account> extend
204
212
  private _selectedAccount: T | undefined = undefined
205
213
  protected readonly _accounts = new Map<Address, T>()
206
214
 
207
- getSelectedAccount(): Promise<T> {
215
+ protected unsafeGetSelectedAccount(): Promise<T> {
208
216
  if (this._selectedAccount === undefined) {
209
217
  throw Error('No account is selected yet')
210
218
  } else {
@@ -212,7 +220,7 @@ export abstract class SignerProviderWithCachedAccounts<T extends Account> extend
212
220
  }
213
221
  }
214
222
 
215
- setSelectedAddress(address: string): Promise<void> {
223
+ setSelectedAccount(address: string): Promise<void> {
216
224
  const accountOpt = this._accounts.get(address)
217
225
  if (accountOpt === undefined) {
218
226
  throw Error('The address is not in the accounts')
@@ -236,23 +244,36 @@ export abstract class SignerProviderWithCachedAccounts<T extends Account> extend
236
244
  }
237
245
  }
238
246
 
239
- export function verifyHexString(hexString: string, publicKey: string, signature: string): boolean {
240
- try {
241
- const key = ec.keyFromPublic(publicKey, 'hex')
242
- return key.verify(hexString, utils.signatureDecode(ec, signature))
243
- } catch (error) {
244
- return false
245
- }
247
+ export function extendMessage(message: string): string {
248
+ return 'Alephium Signed Message: ' + message
246
249
  }
247
250
 
248
- function extendMessage(message: string): string {
249
- return 'Alephium Signed Message: ' + message
251
+ export function hashMessage(message: string, hasher: MessageHasher): string {
252
+ switch (hasher) {
253
+ case 'alephium':
254
+ return utils.binToHex(blake.blake2b(extendMessage(message), undefined, 32))
255
+ case 'sha256':
256
+ const sha256 = createHash('sha256')
257
+ sha256.update(Buffer.from(message))
258
+ return utils.binToHex(sha256.digest())
259
+ case 'blake2b':
260
+ return utils.binToHex(blake.blake2b(message, undefined, 32))
261
+ case 'identity':
262
+ return message
263
+ default:
264
+ throw Error(`Invalid message hasher: ${hasher}`)
265
+ }
250
266
  }
251
267
 
252
- export function verifySignedMessage(message: string, publicKey: string, signature: string): boolean {
253
- const extendedMessage = extendMessage(message)
254
- const messageHash = blake.blake2b(extendedMessage, undefined, 32)
255
- return verifyHexString(utils.binToHex(messageHash), publicKey, signature)
268
+ export function verifySignedMessage(
269
+ message: string,
270
+ messageHasher: MessageHasher,
271
+ publicKey: string,
272
+ signature: string,
273
+ keyType?: KeyType
274
+ ): boolean {
275
+ const messageHash = hashMessage(message, messageHasher)
276
+ return utils.verifySignature(messageHash, publicKey, signature, keyType)
256
277
  }
257
278
 
258
279
  export function toApiDestination(data: Destination): node.Destination {
@@ -21,6 +21,7 @@ import { fromApiNumber256, node, NodeProvider, toApiNumber256Optional, toApiToke
21
21
  import { addressFromPublicKey } from '../utils'
22
22
  import { toApiDestinations } from './signer'
23
23
  import {
24
+ KeyType,
24
25
  SignDeployContractTxParams,
25
26
  SignDeployContractTxResult,
26
27
  SignerAddress,
@@ -35,8 +36,10 @@ import {
35
36
  export abstract class TransactionBuilder {
36
37
  abstract get nodeProvider(): NodeProvider
37
38
 
38
- static create(baseUrl: string, apiKey?: string) {
39
- const nodeProvider = new NodeProvider(baseUrl, apiKey)
39
+ static from(nodeProvider: NodeProvider): TransactionBuilder
40
+ static from(baseUrl: string, apiKey?: string): TransactionBuilder
41
+ static from(param0: string | NodeProvider, param1?: string): TransactionBuilder {
42
+ const nodeProvider = typeof param0 === 'string' ? new NodeProvider(param0, param1) : (param0 as NodeProvider)
40
43
  return new (class extends TransactionBuilder {
41
44
  get nodeProvider(): NodeProvider {
42
45
  return nodeProvider
@@ -44,8 +47,8 @@ export abstract class TransactionBuilder {
44
47
  })()
45
48
  }
46
49
 
47
- private static validatePublicKey(params: SignerAddress, publicKey: string) {
48
- const address = addressFromPublicKey(publicKey)
50
+ private static validatePublicKey(params: SignerAddress, publicKey: string, keyType?: KeyType) {
51
+ const address = addressFromPublicKey(publicKey, keyType)
49
52
  if (address !== params.signerAddress) {
50
53
  throw new Error('Unmatched public key')
51
54
  }
@@ -55,11 +58,12 @@ export abstract class TransactionBuilder {
55
58
  params: SignTransferTxParams,
56
59
  publicKey: string
57
60
  ): Promise<Omit<SignTransferTxResult, 'signature'>> {
58
- TransactionBuilder.validatePublicKey(params, publicKey)
61
+ TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType)
59
62
 
60
63
  const { destinations, gasPrice, ...rest } = params
61
64
  const data: node.BuildTransaction = {
62
65
  fromPublicKey: publicKey,
66
+ fromPublicKeyType: params.signerKeyType,
63
67
  destinations: toApiDestinations(destinations),
64
68
  gasPrice: toApiNumber256Optional(gasPrice),
65
69
  ...rest
@@ -72,11 +76,12 @@ export abstract class TransactionBuilder {
72
76
  params: SignDeployContractTxParams,
73
77
  publicKey: string
74
78
  ): Promise<Omit<SignDeployContractTxResult, 'signature'>> {
75
- TransactionBuilder.validatePublicKey(params, publicKey)
79
+ TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType)
76
80
 
77
81
  const { initialAttoAlphAmount, initialTokenAmounts, issueTokenAmount, gasPrice, ...rest } = params
78
82
  const data: node.BuildDeployContractTx = {
79
83
  fromPublicKey: publicKey,
84
+ fromPublicKeyType: params.signerKeyType,
80
85
  initialAttoAlphAmount: toApiNumber256Optional(initialAttoAlphAmount),
81
86
  initialTokenAmounts: toApiTokens(initialTokenAmounts),
82
87
  issueTokenAmount: toApiNumber256Optional(issueTokenAmount),
@@ -92,11 +97,12 @@ export abstract class TransactionBuilder {
92
97
  params: SignExecuteScriptTxParams,
93
98
  publicKey: string
94
99
  ): Promise<Omit<SignExecuteScriptTxResult, 'signature'>> {
95
- TransactionBuilder.validatePublicKey(params, publicKey)
100
+ TransactionBuilder.validatePublicKey(params, publicKey, params.signerKeyType)
96
101
 
97
102
  const { attoAlphAmount, tokens, gasPrice, ...rest } = params
98
103
  const data: node.BuildExecuteScriptTx = {
99
104
  fromPublicKey: publicKey,
105
+ fromPublicKeyType: params.signerKeyType,
100
106
  attoAlphAmount: toApiNumber256Optional(attoAlphAmount),
101
107
  tokens: toApiTokens(tokens),
102
108
  gasPrice: toApiNumber256Optional(gasPrice),
@@ -33,18 +33,22 @@ export interface Destination {
33
33
  }
34
34
  assertType<Eq<keyof Destination, keyof node.Destination>>
35
35
 
36
+ export type KeyType = 'default' | 'bip340-schnorr'
37
+
36
38
  export interface Account {
39
+ keyType: KeyType
37
40
  address: string
38
41
  group: number
39
42
  publicKey: string
40
43
  }
41
44
 
42
- export type SignerAddress = { signerAddress: string }
43
- type TxBuildParams<T> = Omit<T, 'fromPublicKey' | 'targetBlockHash'> & SignerAddress
45
+ export type SignerAddress = { signerAddress: string; signerKeyType?: KeyType }
46
+ type TxBuildParams<T> = Omit<T, 'fromPublicKey' | 'fromPublicKeyType' | 'targetBlockHash'> & SignerAddress
44
47
  type SignResult<T> = Omit<T, 'gasPrice'> & { signature: string; gasPrice: Number256 }
45
48
 
46
49
  export interface SignTransferTxParams {
47
50
  signerAddress: string
51
+ signerKeyType?: KeyType
48
52
  destinations: Destination[]
49
53
  utxos?: OutputRef[]
50
54
  gasAmount?: number
@@ -64,6 +68,7 @@ assertType<Eq<SignTransferTxResult, SignResult<node.BuildTransactionResult>>>()
64
68
 
65
69
  export interface SignDeployContractTxParams {
66
70
  signerAddress: string
71
+ signerKeyType?: KeyType
67
72
  bytecode: string
68
73
  initialAttoAlphAmount?: Number256
69
74
  initialTokenAmounts?: Token[]
@@ -91,6 +96,7 @@ assertType<
91
96
 
92
97
  export interface SignExecuteScriptTxParams {
93
98
  signerAddress: string
99
+ signerKeyType?: KeyType
94
100
  bytecode: string
95
101
  attoAlphAmount?: Number256
96
102
  tokens?: Token[]
@@ -115,6 +121,7 @@ assertType<
115
121
 
116
122
  export interface SignUnsignedTxParams {
117
123
  signerAddress: string
124
+ signerKeyType?: KeyType
118
125
  unsignedTx: string
119
126
  }
120
127
  assertType<Eq<SignUnsignedTxParams, { unsignedTx: string } & SignerAddress>>()
@@ -129,11 +136,19 @@ export interface SignUnsignedTxResult {
129
136
  }
130
137
  assertType<Eq<SignUnsignedTxResult, SignTransferTxResult>>
131
138
 
139
+ export type MessageHasher =
140
+ | 'alephium' // Message is prefixed with 'Alephium signed message: ' before hashed with blake2b
141
+ | 'sha256'
142
+ | 'blake2b'
143
+ | 'identity' // No hash is used, the message to be 32 bytes
144
+
132
145
  export interface SignMessageParams {
133
146
  signerAddress: string
147
+ signerKeyType?: KeyType
134
148
  message: string
149
+ messageHasher: MessageHasher
135
150
  }
136
- assertType<Eq<SignMessageParams, { message: string } & SignerAddress>>()
151
+ assertType<Eq<SignMessageParams, { message: string; messageHasher: MessageHasher } & SignerAddress>>()
137
152
  export interface SignMessageResult {
138
153
  signature: string
139
154
  }
@@ -151,13 +166,9 @@ export interface SubmissionResult {
151
166
  export interface EnableOptionsBase {
152
167
  // chainGroup - specify whether to use addresses from a specific group
153
168
  chainGroup?: number
169
+ // keyType - specify which type of signing algorithm to use
170
+ keyType?: KeyType
171
+
154
172
  networkId: string
155
- onDisconnected: () => Promise<void>
173
+ onDisconnected: () => Promise<void> | void
156
174
  }
157
-
158
- // Transaction Params for InteractiveSignerProvider
159
- export type ExtSignTransferTxParams = SignTransferTxParams & { networkId: string }
160
- export type ExtSignDeployContractTxParams = SignDeployContractTxParams & { networkId: string }
161
- export type ExtSignExecuteScriptTxParams = SignExecuteScriptTxParams & { networkId: string }
162
- export type ExtSignUnsignedTxParams = SignUnsignedTxParams & { networkId: string }
163
- export type ExtSignMessageParams = SignMessageParams & { networkId: string }
@@ -17,22 +17,17 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
19
  import * as utils from '../utils'
20
- import { ec as EC } from 'elliptic'
20
+ import { KeyType } from '../signer'
21
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)
22
+ export function transactionSign(txId: string, privateKey: string, keyType?: KeyType): string {
23
+ return utils.sign(txId, privateKey, keyType)
29
24
  }
30
25
 
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
- }
26
+ export function transactionVerifySignature(
27
+ txId: string,
28
+ publicKey: string,
29
+ signature: string,
30
+ keyType?: KeyType
31
+ ): boolean {
32
+ return utils.verifySignature(txId, publicKey, signature, keyType)
38
33
  }
@@ -21,3 +21,5 @@ export * from './bs58'
21
21
  export * from './djb2'
22
22
  export * from './utils'
23
23
  export * from './subscription'
24
+ export * from './sign'
25
+ export * from './number'
@@ -0,0 +1,187 @@
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
+ export const tests = [
19
+ {
20
+ raw: 123456n,
21
+ decimal: 5,
22
+ exact: '1.23456',
23
+ alphFormat: '1.23',
24
+ tokenFormat: '1.2346'
25
+ },
26
+ {
27
+ raw: 12345612n,
28
+ decimal: 2,
29
+ exact: '123,456.12',
30
+ alphFormat: '123,456.12',
31
+ tokenFormat: '123,456.12'
32
+ },
33
+ {
34
+ raw: 123456123456n,
35
+ decimal: 6,
36
+ exact: '123,456.123456',
37
+ alphFormat: '123,456.12',
38
+ tokenFormat: '123,456.1235'
39
+ },
40
+ {
41
+ raw: 12n,
42
+ decimal: 2,
43
+ exact: '0.12',
44
+ alphFormat: '0.12',
45
+ tokenFormat: '0.12'
46
+ },
47
+ {
48
+ raw: 123456n,
49
+ decimal: 6,
50
+ exact: '0.123456',
51
+ alphFormat: '0.12',
52
+ tokenFormat: '0.1235'
53
+ },
54
+ {
55
+ raw: 123456n,
56
+ decimal: 7,
57
+ exact: '0.0123456',
58
+ alphFormat: '0.012',
59
+ tokenFormat: '0.0123'
60
+ },
61
+ {
62
+ raw: 123456n,
63
+ decimal: 8,
64
+ exact: '0.00123456',
65
+ alphFormat: '0.0012',
66
+ tokenFormat: '0.0012'
67
+ },
68
+ {
69
+ raw: 123456n,
70
+ decimal: 9,
71
+ exact: '0.000123456',
72
+ alphFormat: '0.00012',
73
+ tokenFormat: '0.00012'
74
+ },
75
+ {
76
+ raw: 123456n,
77
+ decimal: 11,
78
+ exact: '0.00000123456',
79
+ alphFormat: '0.0000012',
80
+ tokenFormat: '0.0000012'
81
+ },
82
+ {
83
+ raw: -123456n,
84
+ decimal: 11,
85
+ exact: '-0.00000123456',
86
+ alphFormat: '-0.0000012',
87
+ tokenFormat: '-0.0000012'
88
+ },
89
+ {
90
+ raw: 8923088n,
91
+ decimal: 10,
92
+ exact: '0.0008923088',
93
+ alphFormat: '0.00089',
94
+ tokenFormat: '0.00089'
95
+ },
96
+ {
97
+ raw: 885n,
98
+ decimal: 6,
99
+ exact: '0.000885',
100
+ alphFormat: '0.00089',
101
+ tokenFormat: '0.00089'
102
+ },
103
+ {
104
+ raw: 100000000000n,
105
+ decimal: 18,
106
+ exact: '0.0000001',
107
+ alphFormat: '0.0000001',
108
+ tokenFormat: '0.0000001'
109
+ },
110
+ {
111
+ raw: 1504000000000000000n,
112
+ decimal: 18,
113
+ exact: '1.504',
114
+ alphFormat: '1.50',
115
+ tokenFormat: '1.504'
116
+ },
117
+ {
118
+ raw: 1505000000000000000n,
119
+ decimal: 18,
120
+ exact: '1.505',
121
+ alphFormat: '1.51',
122
+ tokenFormat: '1.505'
123
+ },
124
+ {
125
+ raw: 1500050000000000000n,
126
+ decimal: 18,
127
+ exact: '1.50005',
128
+ alphFormat: '1.50',
129
+ tokenFormat: '1.5001'
130
+ },
131
+ {
132
+ raw: 100n,
133
+ decimal: 0,
134
+ exact: '100',
135
+ alphFormat: '100.00',
136
+ tokenFormat: '100.0'
137
+ },
138
+ {
139
+ raw: 123456789n,
140
+ decimal: 0,
141
+ exact: '123,456,789',
142
+ alphFormat: '123,456,789.00',
143
+ tokenFormat: '123,456,789.0'
144
+ },
145
+ {
146
+ raw: -123456789n,
147
+ decimal: 0,
148
+ exact: '-123,456,789',
149
+ alphFormat: '-123,456,789.00',
150
+ tokenFormat: '-123,456,789.0'
151
+ },
152
+ {
153
+ raw: 0n,
154
+ decimal: 0,
155
+ exact: '0',
156
+ alphFormat: '0.00',
157
+ tokenFormat: '0.0'
158
+ }
159
+ ]
160
+
161
+ export const tests1 = [
162
+ {
163
+ raw: '0',
164
+ decimals: 18,
165
+ amount: 0n
166
+ },
167
+ {
168
+ raw: '1.23',
169
+ decimals: 2,
170
+ amount: 123n
171
+ },
172
+ {
173
+ raw: '1',
174
+ decimals: 5,
175
+ amount: 100000n
176
+ },
177
+ {
178
+ raw: '1',
179
+ decimals: 18,
180
+ amount: 1000000000000000000n
181
+ },
182
+ {
183
+ raw: '1.23456789',
184
+ decimals: 18,
185
+ amount: 1234567890000000000n
186
+ }
187
+ ]