@alephium/web3 0.2.0-rc.2 → 0.2.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.
- package/.eslintignore +2 -2
- package/README.md +2 -135
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.LICENSE.txt +0 -17
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-alephium.d.ts +126 -22
- package/dist/src/api/api-alephium.js +145 -79
- package/dist/src/api/api-explorer.d.ts +163 -48
- package/dist/src/api/api-explorer.js +157 -34
- package/dist/src/api/index.d.ts +3 -2
- package/dist/src/api/index.js +22 -3
- package/dist/src/api/types.d.ts +23 -0
- package/dist/src/api/types.js +240 -0
- package/dist/src/api/utils.d.ts +6 -0
- package/dist/{scripts/rename-gitignore.js → src/api/utils.js} +11 -6
- package/dist/src/contract/contract.d.ts +117 -69
- package/dist/src/contract/contract.js +401 -435
- package/dist/src/contract/events.d.ts +4 -4
- package/dist/src/contract/index.js +5 -1
- package/dist/src/contract/ralph.d.ts +5 -4
- package/dist/src/contract/ralph.js +27 -1
- package/dist/src/global.d.ts +4 -0
- package/dist/{scripts/stop-devnet.js → src/global.js} +17 -11
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +23 -1
- package/dist/src/signer/index.d.ts +0 -1
- package/dist/src/signer/index.js +5 -2
- package/dist/src/signer/signer.d.ts +21 -11
- package/dist/src/signer/signer.js +46 -9
- package/dist/src/transaction/index.d.ts +0 -1
- package/dist/src/transaction/index.js +5 -2
- package/dist/src/transaction/status.d.ts +2 -1
- package/dist/src/utils/bs58.d.ts +1 -0
- package/dist/src/utils/bs58.js +13 -1
- package/dist/src/utils/index.d.ts +0 -1
- package/dist/src/utils/index.js +5 -2
- package/dist/src/utils/subscription.d.ts +0 -1
- package/dist/src/utils/subscription.js +2 -1
- package/dist/src/utils/utils.d.ts +6 -11
- package/dist/src/utils/utils.js +21 -25
- package/jest-config.json +11 -0
- package/package.json +11 -47
- package/src/api/api-alephium.ts +178 -30
- package/src/api/api-explorer.ts +234 -51
- package/src/api/index.ts +14 -3
- package/src/api/types.ts +233 -0
- package/{scripts/rename-gitignore.js → src/api/utils.ts} +7 -6
- package/src/contract/contract.ts +602 -527
- package/src/contract/events.ts +6 -6
- package/src/contract/ralph.ts +29 -4
- package/src/{transaction/sign-verify.ts → global.ts} +14 -15
- package/src/index.ts +7 -0
- package/src/signer/index.ts +0 -1
- package/src/signer/signer.ts +70 -19
- package/src/transaction/index.ts +0 -1
- package/src/transaction/status.ts +4 -2
- package/src/utils/bs58.ts +11 -0
- package/src/utils/index.ts +0 -1
- package/src/utils/subscription.ts +3 -3
- package/src/utils/utils.ts +14 -22
- package/.eslintrc.json +0 -21
- package/LICENSE +0 -165
- package/contracts/add/add.ral +0 -16
- package/contracts/greeter/greeter.ral +0 -7
- package/contracts/greeter/greeter_interface.ral +0 -3
- package/contracts/greeter_main.ral +0 -9
- package/contracts/main.ral +0 -6
- package/contracts/sub/sub.ral +0 -9
- package/dev/user.conf +0 -29
- package/dist/scripts/create-project.d.ts +0 -2
- package/dist/scripts/create-project.js +0 -125
- package/dist/scripts/rename-gitignore.d.ts +0 -1
- package/dist/scripts/start-devnet.d.ts +0 -1
- package/dist/scripts/start-devnet.js +0 -131
- package/dist/scripts/stop-devnet.d.ts +0 -1
- package/dist/src/signer/node-wallet.d.ts +0 -13
- package/dist/src/signer/node-wallet.js +0 -60
- package/dist/src/test/index.d.ts +0 -7
- package/dist/src/test/index.js +0 -41
- package/dist/src/test/privatekey-wallet.d.ts +0 -12
- package/dist/src/test/privatekey-wallet.js +0 -68
- package/dist/src/transaction/sign-verify.d.ts +0 -2
- package/dist/src/transaction/sign-verify.js +0 -58
- package/dist/src/utils/password-crypto.d.ts +0 -2
- package/dist/src/utils/password-crypto.js +0 -69
- package/gitignore +0 -10
- package/scripts/create-project.ts +0 -137
- package/scripts/start-devnet.js +0 -141
- package/scripts/stop-devnet.js +0 -32
- package/src/contract/ralph.test.ts +0 -178
- package/src/fixtures/address.json +0 -36
- package/src/fixtures/balance.json +0 -9
- package/src/fixtures/self-clique.json +0 -19
- package/src/fixtures/transaction.json +0 -13
- package/src/fixtures/transactions.json +0 -179
- package/src/signer/fixtures/genesis.json +0 -26
- package/src/signer/fixtures/wallets.json +0 -26
- package/src/signer/node-wallet.ts +0 -74
- package/src/test/index.ts +0 -32
- package/src/test/privatekey-wallet.ts +0 -58
- package/src/transaction/sign-verify.test.ts +0 -50
- package/src/utils/address.test.ts +0 -47
- package/src/utils/djb2.test.ts +0 -35
- package/src/utils/password-crypto.test.ts +0 -27
- package/src/utils/password-crypto.ts +0 -77
- package/src/utils/utils.test.ts +0 -161
- package/templates/base/README.md +0 -34
- package/templates/base/package.json +0 -35
- package/templates/base/src/greeter.ts +0 -41
- package/templates/base/tsconfig.json +0 -19
- package/templates/react/README.md +0 -34
- package/templates/react/config-overrides.js +0 -18
- package/templates/react/package.json +0 -66
- package/templates/react/src/App.tsx +0 -42
- package/templates/react/src/artifacts/greeter.ral.json +0 -26
- package/templates/react/src/artifacts/greeter_main.ral.json +0 -22
- package/templates/shared/.eslintrc.json +0 -12
- package/templates/shared/scripts/header.js +0 -0
- package/test/contract.test.ts +0 -178
- package/test/events.test.ts +0 -138
- package/test/transaction.test.ts +0 -72
package/src/contract/events.ts
CHANGED
|
@@ -16,14 +16,14 @@ 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 {
|
|
19
|
+
import { node } from '../api'
|
|
20
20
|
import { Subscription, SubscribeOptions } from '../utils'
|
|
21
21
|
|
|
22
|
-
export class EventSubscription extends Subscription<ContractEvent> {
|
|
22
|
+
export class EventSubscription extends Subscription<node.ContractEvent> {
|
|
23
23
|
readonly contractAddress: string
|
|
24
24
|
private fromCount: number
|
|
25
25
|
|
|
26
|
-
constructor(options: SubscribeOptions<ContractEvent>, contractAddress: string, fromCount?: number) {
|
|
26
|
+
constructor(options: SubscribeOptions<node.ContractEvent>, contractAddress: string, fromCount?: number) {
|
|
27
27
|
super(options)
|
|
28
28
|
this.contractAddress = contractAddress
|
|
29
29
|
this.fromCount = typeof fromCount === 'undefined' ? 0 : fromCount
|
|
@@ -31,7 +31,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
|
|
|
31
31
|
this.startPolling()
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
override startPolling() {
|
|
34
|
+
override startPolling(): void {
|
|
35
35
|
this.eventEmitter.on('tick', async () => {
|
|
36
36
|
await this.polling()
|
|
37
37
|
})
|
|
@@ -42,7 +42,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
|
|
|
42
42
|
return this.fromCount
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
override async polling() {
|
|
45
|
+
override async polling(): Promise<void> {
|
|
46
46
|
try {
|
|
47
47
|
const events = await this.provider.events.getEventsContractContractaddress(this.contractAddress, {
|
|
48
48
|
start: this.fromCount
|
|
@@ -67,7 +67,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
export function subscribeToEvents(
|
|
70
|
-
options: SubscribeOptions<ContractEvent>,
|
|
70
|
+
options: SubscribeOptions<node.ContractEvent>,
|
|
71
71
|
contractAddress: string,
|
|
72
72
|
fromCount?: number
|
|
73
73
|
): EventSubscription {
|
package/src/contract/ralph.ts
CHANGED
|
@@ -17,9 +17,9 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { Buffer } from 'buffer/'
|
|
20
|
+
import { Val } from '../api'
|
|
20
21
|
import { bs58, isHexString } from '../utils'
|
|
21
|
-
import {
|
|
22
|
-
import { Fields, Val } from './contract'
|
|
22
|
+
import { Fields, FieldsSig } from './contract'
|
|
23
23
|
|
|
24
24
|
const bigIntZero = BigInt(0)
|
|
25
25
|
|
|
@@ -236,7 +236,7 @@ export function encodeScriptField(tpe: string, value: Val): Uint8Array {
|
|
|
236
236
|
|
|
237
237
|
const scriptFieldRegex = /\{([0-9]*)\}/g
|
|
238
238
|
|
|
239
|
-
export function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fieldsSig:
|
|
239
|
+
export function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fieldsSig: FieldsSig): string {
|
|
240
240
|
return bytecodeTemplate.replace(scriptFieldRegex, (_, fieldIndex: string) => {
|
|
241
241
|
const fieldName = fieldsSig.names[`${fieldIndex}`]
|
|
242
242
|
const fieldType = fieldsSig.types[`${fieldIndex}`]
|
|
@@ -249,7 +249,7 @@ export function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fi
|
|
|
249
249
|
})
|
|
250
250
|
}
|
|
251
251
|
|
|
252
|
-
export function buildContractByteCode(bytecode: string, fields: Fields, fieldsSig:
|
|
252
|
+
export function buildContractByteCode(bytecode: string, fields: Fields, fieldsSig: FieldsSig): string {
|
|
253
253
|
const fieldsEncoded = fieldsSig.names.flatMap((fieldName, fieldIndex) => {
|
|
254
254
|
const fieldType = fieldsSig.types[`${fieldIndex}`]
|
|
255
255
|
if (fieldName in fields) {
|
|
@@ -343,6 +343,31 @@ function invalidVal(tpe: string, value: Val): Error {
|
|
|
343
343
|
return Error(`Invalid API value ${value} for type ${tpe}`)
|
|
344
344
|
}
|
|
345
345
|
|
|
346
|
+
export function buildDebugBytecode(bytecode: string, bytecodePatch: string): string {
|
|
347
|
+
if (bytecodePatch === '') {
|
|
348
|
+
return bytecode
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const pattern = /[=+-][0-9a-f]*/g
|
|
352
|
+
let result = ''
|
|
353
|
+
let index = 0
|
|
354
|
+
for (const parts of bytecodePatch.matchAll(pattern)) {
|
|
355
|
+
const part = parts[0]
|
|
356
|
+
const diffType = part[0]
|
|
357
|
+
if (diffType === '=') {
|
|
358
|
+
const length = parseInt(part.substring(1))
|
|
359
|
+
result = result + bytecode.slice(index, index + length)
|
|
360
|
+
index = index + length
|
|
361
|
+
} else if (diffType === '+') {
|
|
362
|
+
result = result + part.substring(1)
|
|
363
|
+
} else {
|
|
364
|
+
const length = parseInt(part.substring(1))
|
|
365
|
+
index = index + length
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return result
|
|
369
|
+
}
|
|
370
|
+
|
|
346
371
|
// export function buildContractByteCode(
|
|
347
372
|
// compiled: node.TemplateContractByteCode,
|
|
348
373
|
// templateVariables: TemplateVariables
|
|
@@ -16,24 +16,23 @@ 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 {
|
|
19
|
+
import { NodeProvider } from './api'
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
let _currentNodeProvider: NodeProvider | undefined = undefined
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
export function
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
23
|
+
export function setCurrentNodeProvider(provider: NodeProvider): void
|
|
24
|
+
export function setCurrentNodeProvider(baseUrl: string, apiKey?: string): void
|
|
25
|
+
export function setCurrentNodeProvider(provider: NodeProvider | string, apiKey?: string): void {
|
|
26
|
+
if (typeof provider == 'string') {
|
|
27
|
+
_currentNodeProvider = new NodeProvider(provider, apiKey)
|
|
28
|
+
} else {
|
|
29
|
+
_currentNodeProvider = provider
|
|
30
|
+
}
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
export function
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return key.verify(txHash, utils.signatureDecode(ec, signature))
|
|
36
|
-
} catch (error) {
|
|
37
|
-
return false
|
|
33
|
+
export function getCurrentNodeProvider(): NodeProvider {
|
|
34
|
+
if (typeof _currentNodeProvider === 'undefined') {
|
|
35
|
+
throw Error('No node provider is set.')
|
|
38
36
|
}
|
|
37
|
+
return _currentNodeProvider
|
|
39
38
|
}
|
package/src/index.ts
CHANGED
|
@@ -16,6 +16,10 @@ 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
|
+
BigInt.prototype['toJSON'] = function () {
|
|
20
|
+
return this.toString()
|
|
21
|
+
}
|
|
22
|
+
|
|
19
23
|
export * from './api'
|
|
20
24
|
export * from './contract'
|
|
21
25
|
export * from './signer'
|
|
@@ -23,3 +27,6 @@ export * from './utils'
|
|
|
23
27
|
export * from './transaction'
|
|
24
28
|
|
|
25
29
|
export * from './constants'
|
|
30
|
+
export * as web3 from './global'
|
|
31
|
+
|
|
32
|
+
export * as utils from './utils'
|
package/src/signer/index.ts
CHANGED
package/src/signer/signer.ts
CHANGED
|
@@ -17,12 +17,23 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { ec as EC } from 'elliptic'
|
|
20
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
fromApiNumber256,
|
|
22
|
+
fromApiTokens,
|
|
23
|
+
NodeProvider,
|
|
24
|
+
Number256,
|
|
25
|
+
toApiNumber256,
|
|
26
|
+
toApiNumber256Optional,
|
|
27
|
+
toApiTokens,
|
|
28
|
+
Token
|
|
29
|
+
} from '../api'
|
|
21
30
|
import { node } from '../api'
|
|
22
31
|
import * as utils from '../utils'
|
|
23
32
|
import { Eq, assertType } from '../utils'
|
|
24
33
|
import blake from 'blakejs'
|
|
25
|
-
import {
|
|
34
|
+
import { getCurrentNodeProvider } from '../global'
|
|
35
|
+
|
|
36
|
+
export type OutputRef = node.OutputRef
|
|
26
37
|
|
|
27
38
|
const ec = new EC('secp256k1')
|
|
28
39
|
|
|
@@ -49,13 +60,13 @@ export type GetAccountsResult = Account[]
|
|
|
49
60
|
|
|
50
61
|
export interface SignTransferTxParams {
|
|
51
62
|
signerAddress: string
|
|
52
|
-
destinations:
|
|
53
|
-
utxos?:
|
|
63
|
+
destinations: Destination[]
|
|
64
|
+
utxos?: OutputRef[]
|
|
54
65
|
gasAmount?: number
|
|
55
|
-
gasPrice?:
|
|
66
|
+
gasPrice?: Number256
|
|
56
67
|
submitTx?: boolean
|
|
57
68
|
}
|
|
58
|
-
assertType<Eq<SignTransferTxParams, TxBuildParams<node.BuildTransaction>>>()
|
|
69
|
+
assertType<Eq<keyof SignTransferTxParams, keyof TxBuildParams<node.BuildTransaction>>>()
|
|
59
70
|
export interface SignTransferTxResult {
|
|
60
71
|
fromGroup: number
|
|
61
72
|
toGroup: number
|
|
@@ -68,14 +79,14 @@ assertType<Eq<SignTransferTxResult, SignResult>>()
|
|
|
68
79
|
export interface SignDeployContractTxParams {
|
|
69
80
|
signerAddress: string
|
|
70
81
|
bytecode: string
|
|
71
|
-
initialAttoAlphAmount?:
|
|
82
|
+
initialAttoAlphAmount?: Number256
|
|
72
83
|
initialTokenAmounts?: Token[]
|
|
73
|
-
issueTokenAmount?:
|
|
84
|
+
issueTokenAmount?: Number256
|
|
74
85
|
gasAmount?: number
|
|
75
|
-
gasPrice?:
|
|
86
|
+
gasPrice?: Number256
|
|
76
87
|
submitTx?: boolean
|
|
77
88
|
}
|
|
78
|
-
assertType<Eq<SignDeployContractTxParams, TxBuildParams<node.BuildDeployContractTx>>>()
|
|
89
|
+
assertType<Eq<keyof SignDeployContractTxParams, keyof TxBuildParams<node.BuildDeployContractTx>>>()
|
|
79
90
|
export interface SignDeployContractTxResult {
|
|
80
91
|
fromGroup: number
|
|
81
92
|
toGroup: number
|
|
@@ -91,12 +102,12 @@ export interface SignExecuteScriptTxParams {
|
|
|
91
102
|
signerAddress: string
|
|
92
103
|
bytecode: string
|
|
93
104
|
attoAlphAmount?: string
|
|
94
|
-
tokens?:
|
|
105
|
+
tokens?: Token[]
|
|
95
106
|
gasAmount?: number
|
|
96
107
|
gasPrice?: string
|
|
97
108
|
submitTx?: boolean
|
|
98
109
|
}
|
|
99
|
-
assertType<Eq<SignExecuteScriptTxParams, TxBuildParams<node.BuildExecuteScriptTx>>>()
|
|
110
|
+
assertType<Eq<keyof SignExecuteScriptTxParams, keyof TxBuildParams<node.BuildExecuteScriptTx>>>()
|
|
100
111
|
export interface SignExecuteScriptTxResult {
|
|
101
112
|
fromGroup: number
|
|
102
113
|
toGroup: number
|
|
@@ -167,8 +178,8 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
|
|
|
167
178
|
}
|
|
168
179
|
}
|
|
169
180
|
|
|
170
|
-
constructor(
|
|
171
|
-
this.provider =
|
|
181
|
+
constructor(alwaysSubmitTx: boolean) {
|
|
182
|
+
this.provider = getCurrentNodeProvider()
|
|
172
183
|
this.alwaysSubmitTx = alwaysSubmitTx
|
|
173
184
|
}
|
|
174
185
|
|
|
@@ -176,9 +187,12 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
|
|
|
176
187
|
return (await this.getAccounts())[0].address
|
|
177
188
|
}
|
|
178
189
|
|
|
179
|
-
async submitTransaction(unsignedTx: string,
|
|
190
|
+
async submitTransaction(unsignedTx: string, signerAddress?: string): Promise<SubmissionResult> {
|
|
191
|
+
const decoded = await this.provider.transactions.postTransactionsDecodeUnsignedTx({ unsignedTx: unsignedTx })
|
|
192
|
+
const txId = decoded.unsignedTx.txId
|
|
193
|
+
|
|
180
194
|
const address = typeof signerAddress !== 'undefined' ? signerAddress : await this.defaultSignerAddress()
|
|
181
|
-
const signature = await this.signRaw(address,
|
|
195
|
+
const signature = await this.signRaw(address, txId)
|
|
182
196
|
const params: node.SubmitTransaction = { unsignedTx: unsignedTx, signature: signature }
|
|
183
197
|
return this.provider.transactions.postTransactionsSubmit(params)
|
|
184
198
|
}
|
|
@@ -206,7 +220,12 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
|
|
|
206
220
|
}
|
|
207
221
|
|
|
208
222
|
async buildTransferTx(params: SignTransferTxParams): Promise<node.BuildTransactionResult> {
|
|
209
|
-
|
|
223
|
+
const data: node.BuildTransaction = {
|
|
224
|
+
...(await this.usePublicKey(params)),
|
|
225
|
+
destinations: toApiDestinations(params.destinations),
|
|
226
|
+
gasPrice: toApiNumber256Optional(params.gasPrice)
|
|
227
|
+
}
|
|
228
|
+
return this.provider.transactions.postTransactionsBuild(data)
|
|
210
229
|
}
|
|
211
230
|
|
|
212
231
|
async signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult> {
|
|
@@ -220,7 +239,14 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
|
|
|
220
239
|
}
|
|
221
240
|
|
|
222
241
|
async buildContractCreationTx(params: SignDeployContractTxParams): Promise<node.BuildDeployContractTxResult> {
|
|
223
|
-
|
|
242
|
+
const data: node.BuildDeployContractTx = {
|
|
243
|
+
...(await this.usePublicKey(params)),
|
|
244
|
+
initialAttoAlphAmount: toApiNumber256Optional(params.initialAttoAlphAmount),
|
|
245
|
+
initialTokenAmounts: toApiTokens(params.initialTokenAmounts),
|
|
246
|
+
issueTokenAmount: toApiNumber256Optional(params.issueTokenAmount),
|
|
247
|
+
gasPrice: toApiNumber256Optional(params.gasPrice)
|
|
248
|
+
}
|
|
249
|
+
return this.provider.contracts.postContractsUnsignedTxDeployContract(data)
|
|
224
250
|
}
|
|
225
251
|
|
|
226
252
|
async signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult> {
|
|
@@ -229,7 +255,11 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
|
|
|
229
255
|
}
|
|
230
256
|
|
|
231
257
|
async buildScriptTx(params: SignExecuteScriptTxParams): Promise<node.BuildExecuteScriptTxResult> {
|
|
232
|
-
|
|
258
|
+
const data: node.BuildExecuteScriptTx = {
|
|
259
|
+
...(await this.usePublicKey(params)),
|
|
260
|
+
tokens: toApiTokens(params.tokens)
|
|
261
|
+
}
|
|
262
|
+
return this.provider.contracts.postContractsUnsignedTxExecuteScript(data)
|
|
233
263
|
}
|
|
234
264
|
|
|
235
265
|
// in general, wallet should show the decoded information to user for confirmation
|
|
@@ -311,3 +341,24 @@ export function verifySignedMessage(message: string, publicKey: string, signatur
|
|
|
311
341
|
const messageHash = blake.blake2b(extendedMessage, undefined, 32)
|
|
312
342
|
return verifyHexString(utils.binToHex(messageHash), publicKey, signature)
|
|
313
343
|
}
|
|
344
|
+
|
|
345
|
+
export interface Destination {
|
|
346
|
+
address: string
|
|
347
|
+
attoAlphAmount: Number256
|
|
348
|
+
tokens?: Token[]
|
|
349
|
+
lockTime?: number
|
|
350
|
+
message?: string
|
|
351
|
+
}
|
|
352
|
+
assertType<Eq<keyof Destination, keyof node.Destination>>
|
|
353
|
+
|
|
354
|
+
export function toApiDestination(data: Destination): node.Destination {
|
|
355
|
+
return { ...data, attoAlphAmount: toApiNumber256(data.attoAlphAmount), tokens: toApiTokens(data.tokens) }
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export function toApiDestinations(data: Destination[]): node.Destination[] {
|
|
359
|
+
return data.map(toApiDestination)
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
export function fromApiDestination(data: node.Destination): Destination {
|
|
363
|
+
return { ...data, attoAlphAmount: fromApiNumber256(data.attoAlphAmount), tokens: fromApiTokens(data.tokens) }
|
|
364
|
+
}
|
package/src/transaction/index.ts
CHANGED
|
@@ -16,9 +16,11 @@ 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 {
|
|
19
|
+
import { node } from '../api'
|
|
20
20
|
import { Subscription, SubscribeOptions } from '../utils'
|
|
21
21
|
|
|
22
|
+
export type TxStatus = node.TxStatus
|
|
23
|
+
|
|
22
24
|
export class TxStatusSubscription extends Subscription<TxStatus> {
|
|
23
25
|
readonly txId: string
|
|
24
26
|
readonly fromGroup?: number
|
|
@@ -33,7 +35,7 @@ export class TxStatusSubscription extends Subscription<TxStatus> {
|
|
|
33
35
|
this.startPolling()
|
|
34
36
|
}
|
|
35
37
|
|
|
36
|
-
override async polling() {
|
|
38
|
+
override async polling(): Promise<void> {
|
|
37
39
|
try {
|
|
38
40
|
const txStatus = await this.provider.transactions.getTransactionsStatus({
|
|
39
41
|
txId: this.txId,
|
package/src/utils/bs58.ts
CHANGED
|
@@ -23,4 +23,15 @@ const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
|
|
23
23
|
|
|
24
24
|
export const bs58 = basex(ALPHABET)
|
|
25
25
|
|
|
26
|
+
export function isBase58(s: string): boolean {
|
|
27
|
+
if (s === '' || s.trim() === '') {
|
|
28
|
+
return false
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
return bs58.encode(bs58.decode(s)) === s
|
|
32
|
+
} catch (err) {
|
|
33
|
+
return false
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
26
37
|
export default bs58
|
package/src/utils/index.ts
CHANGED
|
@@ -17,13 +17,13 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import EventEmitter from 'eventemitter3'
|
|
20
|
+
import { getCurrentNodeProvider } from '../global'
|
|
20
21
|
import { NodeProvider } from '../api'
|
|
21
22
|
|
|
22
23
|
type MessageCallback<Message> = (message: Message) => Promise<void>
|
|
23
24
|
type ErrorCallback<Message> = (error: any, subscription: Subscription<Message>) => Promise<void>
|
|
24
25
|
|
|
25
26
|
export interface SubscribeOptions<Message> {
|
|
26
|
-
provider: NodeProvider
|
|
27
27
|
pollingInterval: number
|
|
28
28
|
messageCallback: MessageCallback<Message>
|
|
29
29
|
errorCallback: ErrorCallback<Message>
|
|
@@ -40,7 +40,7 @@ export abstract class Subscription<Message> {
|
|
|
40
40
|
protected cancelled: boolean
|
|
41
41
|
|
|
42
42
|
constructor(options: SubscribeOptions<Message>) {
|
|
43
|
-
this.provider =
|
|
43
|
+
this.provider = getCurrentNodeProvider()
|
|
44
44
|
this.pollingInterval = options.pollingInterval
|
|
45
45
|
this.messageCallback = options.messageCallback
|
|
46
46
|
this.errorCallback = options.errorCallback
|
|
@@ -49,7 +49,7 @@ export abstract class Subscription<Message> {
|
|
|
49
49
|
this.eventEmitter = new EventEmitter()
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
startPolling() {
|
|
52
|
+
startPolling(): void {
|
|
53
53
|
this.eventEmitter.on('tick', async () => {
|
|
54
54
|
await this.polling()
|
|
55
55
|
|
package/src/utils/utils.ts
CHANGED
|
@@ -24,21 +24,9 @@ import { Buffer } from 'buffer/'
|
|
|
24
24
|
|
|
25
25
|
import { TOTAL_NUMBER_OF_GROUPS } from '../constants'
|
|
26
26
|
import djb2 from './djb2'
|
|
27
|
-
import * as node from '../api/api-alephium'
|
|
28
|
-
import * as explorer from '../api/api-explorer'
|
|
29
27
|
|
|
30
28
|
const ec = new EC('secp256k1')
|
|
31
29
|
|
|
32
|
-
export function convertHttpResponse<T>(
|
|
33
|
-
response: node.HttpResponse<T, { detail: string }> | explorer.HttpResponse<T, { detail: string }>
|
|
34
|
-
): T {
|
|
35
|
-
if (response.error) {
|
|
36
|
-
throw new Error(response.error.detail)
|
|
37
|
-
} else {
|
|
38
|
-
return response.data
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
30
|
export function signatureEncode(signature: EC.Signature): string {
|
|
43
31
|
let sNormalized = signature.s
|
|
44
32
|
if (ec.n && signature.s.cmp(ec.nh) === 1) {
|
|
@@ -51,7 +39,7 @@ export function signatureEncode(signature: EC.Signature): string {
|
|
|
51
39
|
}
|
|
52
40
|
|
|
53
41
|
// the signature should be in hex string format for 64 bytes
|
|
54
|
-
export
|
|
42
|
+
export function signatureDecode(ec: EC, signature: string): SignatureInput {
|
|
55
43
|
if (signature.length !== 128) {
|
|
56
44
|
throw new Error('Invalid signature length')
|
|
57
45
|
}
|
|
@@ -66,7 +54,7 @@ export const signatureDecode = (ec: EC, signature: string): SignatureInput => {
|
|
|
66
54
|
}
|
|
67
55
|
}
|
|
68
56
|
|
|
69
|
-
|
|
57
|
+
export function xorByte(intValue: number): number {
|
|
70
58
|
const byte0 = (intValue >> 24) & 0xff
|
|
71
59
|
const byte1 = (intValue >> 16) & 0xff
|
|
72
60
|
const byte2 = (intValue >> 8) & 0xff
|
|
@@ -85,7 +73,7 @@ enum AddressType {
|
|
|
85
73
|
P2C = 0x03
|
|
86
74
|
}
|
|
87
75
|
|
|
88
|
-
export
|
|
76
|
+
export function groupOfAddress(address: string): number {
|
|
89
77
|
const decoded = bs58.decode(address)
|
|
90
78
|
|
|
91
79
|
if (decoded.length == 0) throw new Error('Address string is empty')
|
|
@@ -103,7 +91,7 @@ export const groupOfAddress = (address: string): number => {
|
|
|
103
91
|
}
|
|
104
92
|
}
|
|
105
93
|
|
|
106
|
-
|
|
94
|
+
function groupOfAddressBytes(bytes: Uint8Array): number {
|
|
107
95
|
const hint = djb2(bytes) | 1
|
|
108
96
|
const hash = xorByte(hint)
|
|
109
97
|
const group = hash % TOTAL_NUMBER_OF_GROUPS
|
|
@@ -111,7 +99,7 @@ const groupOfAddressBytes = (bytes: Uint8Array): number => {
|
|
|
111
99
|
}
|
|
112
100
|
|
|
113
101
|
// Pay to public key hash address
|
|
114
|
-
|
|
102
|
+
function groupOfP2pkhAddress(address: Uint8Array): number {
|
|
115
103
|
if (address.length != 32) {
|
|
116
104
|
throw new Error(`Invalid p2pkh address length: ${address.length}`)
|
|
117
105
|
}
|
|
@@ -120,7 +108,7 @@ const groupOfP2pkhAddress = (address: Uint8Array): number => {
|
|
|
120
108
|
}
|
|
121
109
|
|
|
122
110
|
// Pay to multiple public key hash address
|
|
123
|
-
|
|
111
|
+
function groupOfP2mpkhAddress(address: Uint8Array): number {
|
|
124
112
|
if ((address.length - 2) % 32 != 0) {
|
|
125
113
|
throw new Error(`Invalid p2mpkh address length: ${address.length}`)
|
|
126
114
|
}
|
|
@@ -129,7 +117,7 @@ const groupOfP2mpkhAddress = (address: Uint8Array): number => {
|
|
|
129
117
|
}
|
|
130
118
|
|
|
131
119
|
// Pay to script hash address
|
|
132
|
-
|
|
120
|
+
function groupOfP2shAddress(address: Uint8Array): number {
|
|
133
121
|
return groupOfAddressBytes(address)
|
|
134
122
|
}
|
|
135
123
|
|
|
@@ -163,6 +151,10 @@ export function binToHex(bin: Uint8Array): string {
|
|
|
163
151
|
return Buffer.from(bin).toString('hex')
|
|
164
152
|
}
|
|
165
153
|
|
|
154
|
+
export function groupOfPrivateKey(privateKey: string): number {
|
|
155
|
+
return groupOfAddress(addressFromPublicKey(publicKeyFromPrivateKey(privateKey)))
|
|
156
|
+
}
|
|
157
|
+
|
|
166
158
|
export function publicKeyFromPrivateKey(privateKey: string): string {
|
|
167
159
|
const key = ec.keyFromPrivate(privateKey)
|
|
168
160
|
return key.getPublic(true, 'hex')
|
|
@@ -203,11 +195,11 @@ export function stringToHex(str: string): string {
|
|
|
203
195
|
return hex
|
|
204
196
|
}
|
|
205
197
|
|
|
206
|
-
export function hexToString(str:
|
|
207
|
-
return Buffer.from(str
|
|
198
|
+
export function hexToString(str: string): string {
|
|
199
|
+
return Buffer.from(str, 'hex').toString()
|
|
208
200
|
}
|
|
209
201
|
|
|
210
|
-
export function timeout(ms: number) {
|
|
202
|
+
export function timeout(ms: number): Promise<void> {
|
|
211
203
|
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
212
204
|
}
|
|
213
205
|
|
package/.eslintrc.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"plugins": ["security", "header"],
|
|
3
|
-
"extends": [
|
|
4
|
-
"prettier",
|
|
5
|
-
"plugin:prettier/recommended",
|
|
6
|
-
"plugin:security/recommended",
|
|
7
|
-
"plugin:@typescript-eslint/recommended"
|
|
8
|
-
],
|
|
9
|
-
"parser": "@typescript-eslint/parser",
|
|
10
|
-
"rules": {
|
|
11
|
-
"header/header": [2, "scripts/header.js"]
|
|
12
|
-
},
|
|
13
|
-
"overrides": [
|
|
14
|
-
{
|
|
15
|
-
"files": ["*.ts"],
|
|
16
|
-
"rules": {
|
|
17
|
-
"security/detect-non-literal-fs-filename": "off"
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
]
|
|
21
|
-
}
|