@alephium/web3 0.5.0-rc.9 → 0.5.0

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 (38) hide show
  1. package/dist/alephium-web3.min.js +1 -1
  2. package/dist/alephium-web3.min.js.map +1 -1
  3. package/dist/src/api/api-alephium.d.ts +56 -6
  4. package/dist/src/api/api-alephium.js +5 -3
  5. package/dist/src/api/api-explorer.d.ts +222 -52
  6. package/dist/src/api/api-explorer.js +17 -15
  7. package/dist/src/api/explorer-provider.d.ts +18 -0
  8. package/dist/src/api/explorer-provider.js +65 -0
  9. package/dist/src/api/index.d.ts +2 -42
  10. package/dist/src/api/index.js +6 -117
  11. package/dist/src/api/node-provider.d.ts +21 -0
  12. package/dist/src/api/node-provider.js +68 -0
  13. package/dist/src/api/types.d.ts +9 -1
  14. package/dist/src/api/types.js +17 -1
  15. package/dist/src/contract/contract.d.ts +6 -3
  16. package/dist/src/contract/contract.js +31 -28
  17. package/dist/src/signer/signer.d.ts +4 -8
  18. package/dist/src/signer/signer.js +26 -8
  19. package/dist/src/signer/types.d.ts +4 -16
  20. package/dist/src/utils/index.d.ts +1 -0
  21. package/dist/src/utils/index.js +1 -0
  22. package/dist/src/utils/number.d.ts +18 -0
  23. package/dist/src/utils/number.fixture.d.ts +12 -0
  24. package/dist/src/utils/number.fixture.js +189 -0
  25. package/dist/src/utils/number.js +148 -0
  26. package/package.json +6 -5
  27. package/src/api/api-alephium.ts +177 -180
  28. package/src/api/api-explorer.ts +327 -126
  29. package/src/api/explorer-provider.ts +78 -0
  30. package/src/api/index.ts +2 -148
  31. package/src/api/node-provider.ts +84 -0
  32. package/src/api/types.ts +24 -1
  33. package/src/contract/contract.ts +31 -29
  34. package/src/signer/signer.ts +33 -26
  35. package/src/signer/types.ts +12 -9
  36. package/src/utils/index.ts +1 -0
  37. package/src/utils/number.fixture.ts +187 -0
  38. package/src/utils/number.ts +162 -0
@@ -0,0 +1,78 @@
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 { ApiRequestArguments, ApiRequestHandler, forwardRequests, request } from './types'
20
+ import { Api as ExplorerApi } from './api-explorer'
21
+
22
+ function initializeExplorerApi(baseUrl: string, apiKey?: string): ExplorerApi<string> {
23
+ const explorerApi = new ExplorerApi<string>({
24
+ baseUrl: baseUrl,
25
+ baseApiParams: { secure: true },
26
+ securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
27
+ })
28
+ explorerApi.setSecurityData(apiKey ?? null)
29
+ return explorerApi
30
+ }
31
+
32
+ export class ExplorerProvider {
33
+ readonly blocks: ExplorerApi<string>['blocks']
34
+ readonly transactions: ExplorerApi<string>['transactions']
35
+ readonly addresses: ExplorerApi<string>['addresses']
36
+ readonly infos: ExplorerApi<string>['infos']
37
+ readonly mempool: ExplorerApi<string>['mempool']
38
+ readonly tokens: ExplorerApi<string>['tokens']
39
+ readonly charts: ExplorerApi<string>['charts']
40
+ readonly utils: ExplorerApi<string>['utils']
41
+
42
+ constructor(baseUrl: string, apiKey?: string)
43
+ constructor(provider: ExplorerProvider)
44
+ constructor(handler: ApiRequestHandler)
45
+ constructor(param0: string | ExplorerProvider | ApiRequestHandler, apiKey?: string) {
46
+ let explorerApi: ExplorerProvider
47
+ if (typeof param0 === 'string') {
48
+ explorerApi = initializeExplorerApi(param0, apiKey)
49
+ } else if (typeof param0 === 'function') {
50
+ explorerApi = new ExplorerProvider('https://1.2.3.4:0')
51
+ forwardRequests(explorerApi, param0 as ApiRequestHandler)
52
+ } else {
53
+ explorerApi = param0 as ExplorerProvider
54
+ }
55
+
56
+ this.blocks = { ...explorerApi.blocks }
57
+ this.transactions = { ...explorerApi.transactions }
58
+ this.addresses = { ...explorerApi.addresses }
59
+ this.infos = { ...explorerApi.infos }
60
+ this.mempool = { ...explorerApi.mempool }
61
+ this.tokens = { ...explorerApi.tokens }
62
+ this.charts = { ...explorerApi.charts }
63
+ this.utils = { ...explorerApi.utils }
64
+ }
65
+
66
+ request = (args: ApiRequestArguments): Promise<any> => {
67
+ return request(this, args)
68
+ }
69
+
70
+ // This can prevent the proxied explorer provider from being modified
71
+ static Proxy(explorerProvider: ExplorerProvider): ExplorerProvider {
72
+ return new ExplorerProvider(explorerProvider)
73
+ }
74
+
75
+ static Remote(handler: ApiRequestHandler): ExplorerProvider {
76
+ return new ExplorerProvider(handler)
77
+ }
78
+ }
package/src/api/index.ts CHANGED
@@ -16,154 +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 { Api as NodeApi } from './api-alephium'
20
- import { Api as ExplorerApi } from './api-explorer'
21
-
22
- export interface ApiRequestArguments {
23
- path: string
24
- method: string
25
- params: any[]
26
- }
27
- export type ApiRequestHandler = (args: ApiRequestArguments) => Promise<any>
28
-
29
- function forwardRequests(api: Record<string, any>, handler: ApiRequestHandler): void {
30
- // Update class properties to forward requests
31
- for (const [path, pathObject] of Object.entries(api)) {
32
- for (const method of Object.keys(pathObject)) {
33
- pathObject[`${method}`] = async (...params: any): Promise<any> => {
34
- return handler({ path, method, params })
35
- }
36
- }
37
- }
38
- }
39
-
40
- async function request(provider: Record<string, any>, args: ApiRequestArguments): Promise<any> {
41
- const call = provider[`${args.path}`][`${args.method}`] as (...any) => Promise<any>
42
- return call(...args.params)
43
- }
44
-
45
- function initializeNodeApi(baseUrl: string, apiKey?: string): NodeApi<string> {
46
- const nodeApi = new NodeApi<string>({
47
- baseUrl: baseUrl,
48
- baseApiParams: { secure: true },
49
- securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
50
- })
51
- nodeApi.setSecurityData(apiKey ?? null)
52
- return nodeApi
53
- }
54
-
55
- export class NodeProvider {
56
- readonly wallets: NodeApi<string>['wallets']
57
- readonly infos: NodeApi<string>['infos']
58
- readonly blockflow: NodeApi<string>['blockflow']
59
- readonly addresses: NodeApi<string>['addresses']
60
- readonly transactions: NodeApi<string>['transactions']
61
- readonly mempool: NodeApi<string>['mempool']
62
- readonly contracts: NodeApi<string>['contracts']
63
- readonly multisig: NodeApi<string>['multisig']
64
- readonly utils: NodeApi<string>['utils']
65
- readonly miners: NodeApi<string>['miners']
66
- readonly events: NodeApi<string>['events']
67
-
68
- constructor(baseUrl: string, apiKey?: string)
69
- constructor(provider: NodeProvider)
70
- constructor(handler: ApiRequestHandler)
71
- constructor(param0: string | NodeProvider | ApiRequestHandler, apiKey?: string) {
72
- let nodeApi: NodeProvider
73
- if (typeof param0 === 'string') {
74
- nodeApi = initializeNodeApi(param0, apiKey)
75
- } else if (typeof param0 === 'function') {
76
- nodeApi = new NodeProvider('https://1.2.3.4:0')
77
- forwardRequests(nodeApi, param0 as ApiRequestHandler)
78
- } else {
79
- nodeApi = param0 as NodeProvider
80
- }
81
-
82
- this.wallets = { ...nodeApi.wallets }
83
- this.infos = { ...nodeApi.infos }
84
- this.blockflow = { ...nodeApi.blockflow }
85
- this.addresses = { ...nodeApi.addresses }
86
- this.transactions = { ...nodeApi.transactions }
87
- this.mempool = { ...nodeApi.mempool }
88
- this.contracts = { ...nodeApi.contracts }
89
- this.multisig = { ...nodeApi.multisig }
90
- this.utils = { ...nodeApi.utils }
91
- this.miners = { ...nodeApi.miners }
92
- this.events = { ...nodeApi.events }
93
- }
94
-
95
- request = (args: ApiRequestArguments): Promise<any> => {
96
- return request(this, args)
97
- }
98
-
99
- // This can prevent the proxied node provider from being modified
100
- static Proxy(nodeProvider: NodeProvider): NodeProvider {
101
- return new NodeProvider(nodeProvider)
102
- }
103
-
104
- static Remote(handler: ApiRequestHandler): NodeProvider {
105
- return new NodeProvider(handler)
106
- }
107
- }
108
-
109
- function initializeExplorerApi(baseUrl: string, apiKey?: string): ExplorerApi<string> {
110
- const explorerApi = new ExplorerApi<string>({
111
- baseUrl: baseUrl,
112
- baseApiParams: { secure: true },
113
- securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
114
- })
115
- explorerApi.setSecurityData(apiKey ?? null)
116
- return explorerApi
117
- }
118
-
119
- export class ExplorerProvider {
120
- readonly blocks = ExplorerApi['blocks']
121
- readonly transactions = ExplorerApi['transactions']
122
- readonly addresses = ExplorerApi['addresses']
123
- readonly infos = ExplorerApi['infos']
124
- readonly unconfirmedTransactions = ExplorerApi['unconfirmedTransactions']
125
- readonly tokens = ExplorerApi['tokens']
126
- readonly charts = ExplorerApi['charts']
127
- readonly utils = ExplorerApi['utils']
128
-
129
- constructor(baseUrl: string, apiKey?: string)
130
- constructor(provider: ExplorerProvider)
131
- constructor(handler: ApiRequestHandler)
132
- constructor(param0: string | ExplorerProvider | ApiRequestHandler, apiKey?: string) {
133
- let explorerApi: ExplorerProvider
134
- if (typeof param0 === 'string') {
135
- explorerApi = initializeExplorerApi(param0, apiKey)
136
- } else if (typeof param0 === 'function') {
137
- explorerApi = new ExplorerProvider('https://1.2.3.4:0')
138
- forwardRequests(explorerApi, param0 as ApiRequestHandler)
139
- } else {
140
- explorerApi = param0 as ExplorerProvider
141
- }
142
-
143
- this.blocks = { ...explorerApi.blocks }
144
- this.transactions = { ...explorerApi.transactions }
145
- this.addresses = { ...explorerApi.addresses }
146
- this.infos = { ...explorerApi.infos }
147
- this.unconfirmedTransactions = { ...explorerApi.unconfirmedTransactions }
148
- this.tokens = { ...explorerApi.tokens }
149
- this.charts = { ...explorerApi.charts }
150
- this.utils = { ...explorerApi.utils }
151
- }
152
-
153
- request = (args: ApiRequestArguments): Promise<any> => {
154
- return request(this, args)
155
- }
156
-
157
- // This can prevent the proxied explorer provider from being modified
158
- static Proxy(explorerProvider: ExplorerProvider): ExplorerProvider {
159
- return new ExplorerProvider(explorerProvider)
160
- }
161
-
162
- static Remote(handler: ApiRequestHandler): ExplorerProvider {
163
- return new ExplorerProvider(handler)
164
- }
165
- }
166
-
19
+ export * from './node-provider'
20
+ export * from './explorer-provider'
167
21
  export * as node from './api-alephium'
168
22
  export * as explorer from './api-explorer'
169
23
  export * from './types'
@@ -0,0 +1,84 @@
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 { ApiRequestArguments, ApiRequestHandler, forwardRequests, request } from './types'
20
+ import { Api as NodeApi } from './api-alephium'
21
+
22
+ function initializeNodeApi(baseUrl: string, apiKey?: string): NodeApi<string> {
23
+ const nodeApi = new NodeApi<string>({
24
+ baseUrl: baseUrl,
25
+ baseApiParams: { secure: true },
26
+ securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
27
+ })
28
+ nodeApi.setSecurityData(apiKey ?? null)
29
+ return nodeApi
30
+ }
31
+
32
+ export class NodeProvider {
33
+ readonly wallets: NodeApi<string>['wallets']
34
+ readonly infos: NodeApi<string>['infos']
35
+ readonly blockflow: NodeApi<string>['blockflow']
36
+ readonly addresses: NodeApi<string>['addresses']
37
+ readonly transactions: NodeApi<string>['transactions']
38
+ readonly mempool: NodeApi<string>['mempool']
39
+ readonly contracts: NodeApi<string>['contracts']
40
+ readonly multisig: NodeApi<string>['multisig']
41
+ readonly utils: NodeApi<string>['utils']
42
+ readonly miners: NodeApi<string>['miners']
43
+ readonly events: NodeApi<string>['events']
44
+
45
+ constructor(baseUrl: string, apiKey?: string)
46
+ constructor(provider: NodeProvider)
47
+ constructor(handler: ApiRequestHandler)
48
+ constructor(param0: string | NodeProvider | ApiRequestHandler, apiKey?: string) {
49
+ let nodeApi: NodeProvider
50
+ if (typeof param0 === 'string') {
51
+ nodeApi = initializeNodeApi(param0, apiKey)
52
+ } else if (typeof param0 === 'function') {
53
+ nodeApi = new NodeProvider('https://1.2.3.4:0')
54
+ forwardRequests(nodeApi, param0 as ApiRequestHandler)
55
+ } else {
56
+ nodeApi = param0 as NodeProvider
57
+ }
58
+
59
+ this.wallets = { ...nodeApi.wallets }
60
+ this.infos = { ...nodeApi.infos }
61
+ this.blockflow = { ...nodeApi.blockflow }
62
+ this.addresses = { ...nodeApi.addresses }
63
+ this.transactions = { ...nodeApi.transactions }
64
+ this.mempool = { ...nodeApi.mempool }
65
+ this.contracts = { ...nodeApi.contracts }
66
+ this.multisig = { ...nodeApi.multisig }
67
+ this.utils = { ...nodeApi.utils }
68
+ this.miners = { ...nodeApi.miners }
69
+ this.events = { ...nodeApi.events }
70
+ }
71
+
72
+ request = (args: ApiRequestArguments): Promise<any> => {
73
+ return request(this, args)
74
+ }
75
+
76
+ // This can prevent the proxied node provider from being modified
77
+ static Proxy(nodeProvider: NodeProvider): NodeProvider {
78
+ return new NodeProvider(nodeProvider)
79
+ }
80
+
81
+ static Remote(handler: ApiRequestHandler): NodeProvider {
82
+ return new NodeProvider(handler)
83
+ }
84
+ }
package/src/api/types.ts CHANGED
@@ -19,7 +19,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
19
19
  import { assertType, bs58, Eq } from '../utils'
20
20
  import * as node from './api-alephium'
21
21
 
22
- export type Number256 = bigint
22
+ export type Number256 = bigint | string
23
23
  export type Val = Number256 | boolean | string | Val[]
24
24
  export type NamedVals = Record<string, Val>
25
25
 
@@ -245,3 +245,26 @@ export function typeLength(tpe: string): number {
245
245
  const [, dims] = decodeArrayType(tpe)
246
246
  return dims.reduce((a, b) => a * b)
247
247
  }
248
+
249
+ export interface ApiRequestArguments {
250
+ path: string
251
+ method: string
252
+ params: any[]
253
+ }
254
+ export type ApiRequestHandler = (args: ApiRequestArguments) => Promise<any>
255
+
256
+ export function forwardRequests(api: Record<string, any>, handler: ApiRequestHandler): void {
257
+ // Update class properties to forward requests
258
+ for (const [path, pathObject] of Object.entries(api)) {
259
+ for (const method of Object.keys(pathObject)) {
260
+ pathObject[`${method}`] = async (...params: any): Promise<any> => {
261
+ return handler({ path, method, params })
262
+ }
263
+ }
264
+ }
265
+ }
266
+
267
+ export async function request(provider: Record<string, any>, args: ApiRequestArguments): Promise<any> {
268
+ const call = provider[`${args.path}`][`${args.method}`] as (...any) => Promise<any>
269
+ return call(...args.params)
270
+ }
@@ -53,7 +53,8 @@ import {
53
53
  assertType,
54
54
  Eq,
55
55
  Optional,
56
- groupOfAddress
56
+ groupOfAddress,
57
+ addressFromContractId
57
58
  } from '../utils'
58
59
  import { getCurrentNodeProvider } from '../global'
59
60
  import * as path from 'path'
@@ -1360,6 +1361,15 @@ export interface CallContractResult<R> {
1360
1361
  events: ContractEvent[]
1361
1362
  }
1362
1363
 
1364
+ function specialContractAddress(n: number): string {
1365
+ const bytes = new Uint8Array(32).fill(0)
1366
+ bytes[31] = n
1367
+ return addressFromContractId(binToHex(bytes))
1368
+ }
1369
+
1370
+ export const CreateContractEventAddress = specialContractAddress(-1)
1371
+ export const DestroyContractEventAddress = specialContractAddress(-2)
1372
+
1363
1373
  export interface SystemEventSig extends EventSig {
1364
1374
  optionalFieldNames?: string[]
1365
1375
  optionalFieldTypes?: string[]
@@ -1472,18 +1482,17 @@ export async function fetchContractState<F extends Fields, I extends ContractIns
1472
1482
  }
1473
1483
 
1474
1484
  export function subscribeContractCreatedEvent(
1475
- instance: ContractInstance,
1476
1485
  options: SubscribeOptions<ContractCreatedEvent>,
1477
1486
  fromCount?: number
1478
1487
  ): EventSubscription {
1479
1488
  return subscribeEventsFromContract(
1480
1489
  options,
1481
- instance.address,
1490
+ CreateContractEventAddress,
1482
1491
  Contract.ContractCreatedEventIndex,
1483
1492
  (event) => {
1484
1493
  return {
1485
1494
  ...decodeContractCreatedEvent(event),
1486
- contractAddress: instance.address
1495
+ contractAddress: CreateContractEventAddress
1487
1496
  }
1488
1497
  },
1489
1498
  fromCount
@@ -1491,18 +1500,17 @@ export function subscribeContractCreatedEvent(
1491
1500
  }
1492
1501
 
1493
1502
  export function subscribeContractDestroyedEvent(
1494
- instance: ContractInstance,
1495
1503
  options: SubscribeOptions<ContractDestroyedEvent>,
1496
1504
  fromCount?: number
1497
1505
  ): EventSubscription {
1498
1506
  return subscribeEventsFromContract(
1499
1507
  options,
1500
- instance.address,
1508
+ DestroyContractEventAddress,
1501
1509
  Contract.ContractDestroyedEventIndex,
1502
1510
  (event) => {
1503
1511
  return {
1504
1512
  ...decodeContractDestroyedEvent(event),
1505
- contractAddress: instance.address
1513
+ contractAddress: DestroyContractEventAddress
1506
1514
  }
1507
1515
  },
1508
1516
  fromCount
@@ -1552,34 +1560,17 @@ export function subscribeContractEvent<F extends Fields, M extends ContractEvent
1552
1560
  )
1553
1561
  }
1554
1562
 
1555
- export function subscribeAllEvents(
1563
+ export function subscribeContractEvents(
1556
1564
  contract: Contract,
1557
1565
  instance: ContractInstance,
1558
1566
  options: SubscribeOptions<ContractEvent<any>>,
1559
1567
  fromCount?: number
1560
1568
  ): EventSubscription {
1561
1569
  const messageCallback = (event: node.ContractEvent): Promise<void> => {
1562
- switch (event.eventIndex) {
1563
- case Contract.ContractCreatedEventIndex: {
1564
- return options.messageCallback({
1565
- ...decodeContractCreatedEvent(event),
1566
- contractAddress: instance.address
1567
- })
1568
- }
1569
-
1570
- case Contract.ContractDestroyedEventIndex: {
1571
- return options.messageCallback({
1572
- ...decodeContractDestroyedEvent(event),
1573
- contractAddress: instance.address
1574
- })
1575
- }
1576
-
1577
- default:
1578
- return options.messageCallback({
1579
- ...decodeEvent(contract, instance, event, event.eventIndex),
1580
- contractAddress: instance.address
1581
- })
1582
- }
1570
+ return options.messageCallback({
1571
+ ...decodeEvent(contract, instance, event, event.eventIndex),
1572
+ contractAddress: instance.address
1573
+ })
1583
1574
  }
1584
1575
  const errorCallback = (err: any, subscription: Subscription<node.ContractEvent>): Promise<void> => {
1585
1576
  return options.errorCallback(err, subscription as unknown as Subscription<ContractEvent<any>>)
@@ -1610,3 +1601,14 @@ export async function callMethod<I, F extends Fields, A extends Arguments, R>(
1610
1601
  const callResult = contract.contract.fromApiCallContractResult(result, txId, methodIndex)
1611
1602
  return callResult as CallContractResult<R>
1612
1603
  }
1604
+
1605
+ export async function getContractEventsCurrentCount(contractAddress: Address): Promise<number> {
1606
+ return getCurrentNodeProvider()
1607
+ .events.getEventsContractContractaddressCurrentCount(contractAddress)
1608
+ .catch((error) => {
1609
+ if (error instanceof Error && error.message.includes(`${contractAddress} not found`)) {
1610
+ return 0
1611
+ }
1612
+ throw error
1613
+ })
1614
+ }
@@ -16,6 +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 { Buffer } from 'buffer/'
20
+ import { createHash } from 'crypto'
19
21
  import { ExplorerProvider, fromApiNumber256, fromApiTokens, NodeProvider, toApiNumber256, toApiTokens } from '../api'
20
22
  import { node } from '../api'
21
23
  import * as utils from '../utils'
@@ -38,12 +40,8 @@ import {
38
40
  SignUnsignedTxResult,
39
41
  SubmissionResult,
40
42
  SubmitTransactionParams,
41
- ExtSignTransferTxParams,
42
- ExtSignDeployContractTxParams,
43
- ExtSignExecuteScriptTxParams,
44
- ExtSignUnsignedTxParams,
45
- ExtSignMessageParams,
46
- KeyType
43
+ KeyType,
44
+ MessageHasher
47
45
  } from './types'
48
46
  import { TransactionBuilder } from './tx-builder'
49
47
  import { addressFromPublicKey, groupOfAddress } from '../utils'
@@ -90,18 +88,6 @@ export abstract class InteractiveSignerProvider<
90
88
  }
91
89
 
92
90
  abstract disconnect(): Promise<void>
93
-
94
- // Methods inherited from SignerProvider, but require networkId in the params
95
- abstract override signAndSubmitTransferTx(params: ExtSignTransferTxParams): Promise<SignTransferTxResult>
96
- abstract override signAndSubmitDeployContractTx(
97
- params: ExtSignDeployContractTxParams
98
- ): Promise<SignDeployContractTxResult>
99
- abstract override signAndSubmitExecuteScriptTx(
100
- params: ExtSignExecuteScriptTxParams
101
- ): Promise<SignExecuteScriptTxResult>
102
- abstract override signAndSubmitUnsignedTx(params: ExtSignUnsignedTxParams): Promise<SignUnsignedTxResult>
103
- abstract override signUnsignedTx(params: ExtSignUnsignedTxParams): Promise<SignUnsignedTxResult>
104
- abstract override signMessage(params: ExtSignMessageParams): Promise<SignMessageResult>
105
91
  }
106
92
 
107
93
  export abstract class SignerProviderSimple extends SignerProvider {
@@ -193,9 +179,8 @@ export abstract class SignerProviderSimple extends SignerProvider {
193
179
  }
194
180
 
195
181
  async signMessage(params: SignMessageParams): Promise<SignMessageResult> {
196
- const extendedMessage = extendMessage(params.message)
197
- const messageHash = blake.blake2b(extendedMessage, undefined, 32)
198
- 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)
199
184
  return { signature: signature }
200
185
  }
201
186
 
@@ -259,14 +244,36 @@ export abstract class SignerProviderWithCachedAccounts<T extends Account> extend
259
244
  }
260
245
  }
261
246
 
262
- function extendMessage(message: string): string {
247
+ export function extendMessage(message: string): string {
263
248
  return 'Alephium Signed Message: ' + message
264
249
  }
265
250
 
266
- export function verifySignedMessage(message: string, publicKey: string, signature: string, keyType?: KeyType): boolean {
267
- const extendedMessage = extendMessage(message)
268
- const messageHash = blake.blake2b(extendedMessage, undefined, 32)
269
- return utils.verifySignature(utils.binToHex(messageHash), publicKey, signature, keyType)
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
+ }
266
+ }
267
+
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)
270
277
  }
271
278
 
272
279
  export function toApiDestination(data: Destination): node.Destination {
@@ -136,12 +136,19 @@ export interface SignUnsignedTxResult {
136
136
  }
137
137
  assertType<Eq<SignUnsignedTxResult, SignTransferTxResult>>
138
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
+
139
145
  export interface SignMessageParams {
140
146
  signerAddress: string
141
147
  signerKeyType?: KeyType
142
148
  message: string
149
+ messageHasher: MessageHasher
143
150
  }
144
- assertType<Eq<SignMessageParams, { message: string } & SignerAddress>>()
151
+ assertType<Eq<SignMessageParams, { message: string; messageHasher: MessageHasher } & SignerAddress>>()
145
152
  export interface SignMessageResult {
146
153
  signature: string
147
154
  }
@@ -159,13 +166,9 @@ export interface SubmissionResult {
159
166
  export interface EnableOptionsBase {
160
167
  // chainGroup - specify whether to use addresses from a specific group
161
168
  chainGroup?: number
169
+ // keyType - specify which type of signing algorithm to use
170
+ keyType?: KeyType
171
+
162
172
  networkId: string
163
- onDisconnected: () => Promise<void>
173
+ onDisconnected: () => Promise<void> | void
164
174
  }
165
-
166
- // Transaction Params for InteractiveSignerProvider
167
- export type ExtSignTransferTxParams = SignTransferTxParams & { networkId: string }
168
- export type ExtSignDeployContractTxParams = SignDeployContractTxParams & { networkId: string }
169
- export type ExtSignExecuteScriptTxParams = SignExecuteScriptTxParams & { networkId: string }
170
- export type ExtSignUnsignedTxParams = SignUnsignedTxParams & { networkId: string }
171
- export type ExtSignMessageParams = SignMessageParams & { networkId: string }
@@ -22,3 +22,4 @@ export * from './djb2'
22
22
  export * from './utils'
23
23
  export * from './subscription'
24
24
  export * from './sign'
25
+ export * from './number'