@alephium/web3 0.2.0-rc.0 → 0.2.0-rc.11
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/contracts/greeter/greeter.ral +3 -3
- package/contracts/greeter/greeter_interface.ral +1 -0
- package/contracts/greeter_main.ral +3 -5
- 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/scripts/create-project.js +3 -2
- package/dist/src/api/api-alephium.d.ts +46 -13
- package/dist/src/api/api-alephium.js +19 -3
- package/dist/src/api/api-explorer.d.ts +32 -16
- package/dist/src/api/api-explorer.js +26 -0
- 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/contract/contract.d.ts +104 -64
- package/dist/src/contract/contract.js +370 -426
- package/dist/src/contract/events.d.ts +4 -4
- package/dist/src/contract/index.js +5 -1
- package/dist/src/contract/ralph.d.ts +4 -4
- package/dist/src/global.d.ts +4 -0
- package/dist/src/global.js +38 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +20 -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/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 +5 -4
- package/dist/src/utils/utils.js +18 -17
- package/jest-config.json +11 -0
- package/package.json +7 -35
- package/scripts/create-project.ts +3 -2
- package/src/api/api-alephium.ts +56 -8
- package/src/api/api-explorer.ts +30 -0
- package/src/api/index.ts +14 -3
- package/src/api/types.ts +233 -0
- package/src/contract/contract.ts +537 -526
- package/src/contract/events.ts +6 -6
- package/src/contract/ralph.ts +4 -4
- package/src/{transaction/sign-verify.ts → global.ts} +14 -15
- package/src/index.ts +3 -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/index.ts +0 -1
- package/src/utils/subscription.ts +3 -3
- package/src/utils/utils.ts +11 -11
- package/templates/base/package.json +2 -2
- package/templates/base/src/greeter.ts +10 -9
- package/templates/react/package.json +2 -2
- package/templates/react/src/App.tsx +2 -2
- package/.eslintrc.json +0 -21
- package/LICENSE +0 -165
- package/contracts/add/add.ral +0 -16
- package/contracts/main.ral +0 -6
- package/contracts/sub/sub.ral +0 -9
- 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/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/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) {
|
|
@@ -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
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/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
|
@@ -51,7 +51,7 @@ export function signatureEncode(signature: EC.Signature): string {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
// the signature should be in hex string format for 64 bytes
|
|
54
|
-
export
|
|
54
|
+
export function signatureDecode(ec: EC, signature: string): SignatureInput {
|
|
55
55
|
if (signature.length !== 128) {
|
|
56
56
|
throw new Error('Invalid signature length')
|
|
57
57
|
}
|
|
@@ -66,7 +66,7 @@ export const signatureDecode = (ec: EC, signature: string): SignatureInput => {
|
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
export function xorByte(intValue: number): number {
|
|
70
70
|
const byte0 = (intValue >> 24) & 0xff
|
|
71
71
|
const byte1 = (intValue >> 16) & 0xff
|
|
72
72
|
const byte2 = (intValue >> 8) & 0xff
|
|
@@ -85,7 +85,7 @@ enum AddressType {
|
|
|
85
85
|
P2C = 0x03
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
export
|
|
88
|
+
export function groupOfAddress(address: string): number {
|
|
89
89
|
const decoded = bs58.decode(address)
|
|
90
90
|
|
|
91
91
|
if (decoded.length == 0) throw new Error('Address string is empty')
|
|
@@ -103,7 +103,7 @@ export const groupOfAddress = (address: string): number => {
|
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
function groupOfAddressBytes(bytes: Uint8Array): number {
|
|
107
107
|
const hint = djb2(bytes) | 1
|
|
108
108
|
const hash = xorByte(hint)
|
|
109
109
|
const group = hash % TOTAL_NUMBER_OF_GROUPS
|
|
@@ -111,7 +111,7 @@ const groupOfAddressBytes = (bytes: Uint8Array): number => {
|
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
// Pay to public key hash address
|
|
114
|
-
|
|
114
|
+
function groupOfP2pkhAddress(address: Uint8Array): number {
|
|
115
115
|
if (address.length != 32) {
|
|
116
116
|
throw new Error(`Invalid p2pkh address length: ${address.length}`)
|
|
117
117
|
}
|
|
@@ -120,7 +120,7 @@ const groupOfP2pkhAddress = (address: Uint8Array): number => {
|
|
|
120
120
|
}
|
|
121
121
|
|
|
122
122
|
// Pay to multiple public key hash address
|
|
123
|
-
|
|
123
|
+
function groupOfP2mpkhAddress(address: Uint8Array): number {
|
|
124
124
|
if ((address.length - 2) % 32 != 0) {
|
|
125
125
|
throw new Error(`Invalid p2mpkh address length: ${address.length}`)
|
|
126
126
|
}
|
|
@@ -129,7 +129,7 @@ const groupOfP2mpkhAddress = (address: Uint8Array): number => {
|
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
// Pay to script hash address
|
|
132
|
-
|
|
132
|
+
function groupOfP2shAddress(address: Uint8Array): number {
|
|
133
133
|
return groupOfAddressBytes(address)
|
|
134
134
|
}
|
|
135
135
|
|
|
@@ -190,7 +190,7 @@ export function contractIdFromTx(txId: string, outputIndex: number): string {
|
|
|
190
190
|
}
|
|
191
191
|
|
|
192
192
|
export function subContractId(parentContractId: string, pathInHex: string): string {
|
|
193
|
-
const data = Buffer.concat([hexToBinUnsafe(
|
|
193
|
+
const data = Buffer.concat([hexToBinUnsafe(parentContractId), hexToBinUnsafe(pathInHex)])
|
|
194
194
|
|
|
195
195
|
return binToHex(blake.blake2b(blake.blake2b(data, undefined, 32), undefined, 32))
|
|
196
196
|
}
|
|
@@ -203,11 +203,11 @@ export function stringToHex(str: string): string {
|
|
|
203
203
|
return hex
|
|
204
204
|
}
|
|
205
205
|
|
|
206
|
-
export function hexToString(str:
|
|
207
|
-
return Buffer.from(str
|
|
206
|
+
export function hexToString(str: string): string {
|
|
207
|
+
return Buffer.from(str, 'hex').toString()
|
|
208
208
|
}
|
|
209
209
|
|
|
210
|
-
export function timeout(ms: number) {
|
|
210
|
+
export function timeout(ms: number): Promise<void> {
|
|
211
211
|
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
212
212
|
}
|
|
213
213
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"version": "0.1.0",
|
|
4
4
|
"license": "GPL",
|
|
5
5
|
"config": {
|
|
6
|
-
"alephium_version": "1.5.0-
|
|
6
|
+
"alephium_version": "1.5.0-rc7"
|
|
7
7
|
},
|
|
8
8
|
"scripts": {
|
|
9
9
|
"build": "rm -rf dist && npx tsc --build .",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"stop-devnet": "node scripts/stop-devnet.js"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@alephium/web3": "0.2.0-rc.
|
|
15
|
+
"@alephium/web3": "0.2.0-rc.11"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"@types/elliptic": "^6.4.13",
|
|
@@ -2,31 +2,32 @@
|
|
|
2
2
|
* greeter.ts
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { TestContractParams, web3, Project } from '@alephium/web3'
|
|
6
|
+
import { testNodeWallet } from '@alephium/web3/test'
|
|
7
7
|
|
|
8
8
|
async function greet() {
|
|
9
|
-
|
|
9
|
+
web3.setCurrentNodeProvider('http://127.0.0.1:22973')
|
|
10
|
+
await Project.build()
|
|
10
11
|
|
|
11
|
-
const greeter =
|
|
12
|
+
const greeter = Project.contract('Greeter')
|
|
12
13
|
|
|
13
14
|
const testParams: TestContractParams = {
|
|
14
15
|
initialFields: { btcPrice: 1 }
|
|
15
16
|
}
|
|
16
|
-
const testResult = await greeter.testPublicMethod(
|
|
17
|
+
const testResult = await greeter.testPublicMethod('greet', testParams)
|
|
17
18
|
console.log(testResult)
|
|
18
19
|
|
|
19
|
-
const signer = await
|
|
20
|
+
const signer = await testNodeWallet()
|
|
20
21
|
|
|
21
22
|
const deployTx = await greeter.transactionForDeployment(signer, { initialFields: testParams.initialFields })
|
|
22
23
|
const greeterContractId = deployTx.contractId
|
|
23
24
|
console.log(deployTx.fromGroup)
|
|
24
25
|
console.log(deployTx.toGroup)
|
|
25
26
|
|
|
26
|
-
const submitResult = await signer.submitTransaction(deployTx.unsignedTx
|
|
27
|
+
const submitResult = await signer.submitTransaction(deployTx.unsignedTx)
|
|
27
28
|
console.log(submitResult)
|
|
28
29
|
|
|
29
|
-
const main =
|
|
30
|
+
const main = Project.script('GreeterMain')
|
|
30
31
|
|
|
31
32
|
const mainScriptTx = await main.transactionForDeployment(signer, {
|
|
32
33
|
initialFields: { greeterContractId: greeterContractId }
|
|
@@ -34,7 +35,7 @@ async function greet() {
|
|
|
34
35
|
console.log(mainScriptTx.fromGroup)
|
|
35
36
|
console.log(mainScriptTx.toGroup)
|
|
36
37
|
|
|
37
|
-
const mainSubmitResult = await signer.submitTransaction(mainScriptTx.unsignedTx
|
|
38
|
+
const mainSubmitResult = await signer.submitTransaction(mainScriptTx.unsignedTx)
|
|
38
39
|
console.log(mainSubmitResult)
|
|
39
40
|
}
|
|
40
41
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "my-dapp",
|
|
3
3
|
"version": "0.1.0",
|
|
4
4
|
"config": {
|
|
5
|
-
"alephium_version": "1.5.0-
|
|
5
|
+
"alephium_version": "1.5.0-rc7"
|
|
6
6
|
},
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"@testing-library/jest-dom": "^5.16.4",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"@types/node": "^16.11.26",
|
|
13
13
|
"@types/react": "^18.0.3",
|
|
14
14
|
"@types/react-dom": "^18.0.0",
|
|
15
|
-
"@alephium/web3": "0.2.0-rc.
|
|
15
|
+
"@alephium/web3": "0.2.0-rc.11",
|
|
16
16
|
"react": "^18.0.0",
|
|
17
17
|
"react-dom": "^18.0.0",
|
|
18
18
|
"react-scripts": "5.0.1",
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import React, { useEffect, useState } from 'react'
|
|
2
2
|
import './App.css'
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { ExplorerProvider, Contract, Script } from '@alephium/web3'
|
|
5
5
|
import contractJson from './artifacts/greeter.ral.json'
|
|
6
6
|
import scriptJson from './artifacts/greeter_main.ral.json'
|
|
7
7
|
|
|
8
8
|
function Dashboard() {
|
|
9
|
-
const api = new
|
|
9
|
+
const api = new ExplorerProvider('https://mainnet-backend.alephium.org')
|
|
10
10
|
const contract = Contract.fromJson(contractJson).toString()
|
|
11
11
|
const script = Script.fromJson(scriptJson).toString()
|
|
12
12
|
const [blocks, setBlocks] = useState('')
|
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
|
-
}
|