@alephium/web3 0.2.0-rc.3 → 0.2.0-rc.30
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 +117 -17
- 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 +107 -68
- package/dist/src/contract/contract.js +395 -451
- package/dist/src/contract/events.d.ts +4 -4
- package/dist/src/contract/events.js +2 -1
- 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 +25 -13
- package/dist/src/signer/signer.js +51 -16
- 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/transaction/status.js +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 -3
- package/dist/src/utils/subscription.js +0 -1
- package/dist/src/utils/utils.d.ts +4 -9
- package/dist/src/utils/utils.js +20 -24
- package/jest-config.json +11 -0
- package/package.json +11 -47
- package/src/api/api-alephium.ts +169 -25
- 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 +579 -545
- package/src/contract/events.ts +6 -5
- 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 +79 -27
- package/src/transaction/index.ts +0 -1
- package/src/transaction/status.ts +5 -2
- package/src/utils/bs58.ts +11 -0
- package/src/utils/index.ts +0 -1
- package/src/utils/subscription.ts +1 -3
- package/src/utils/utils.ts +11 -19
- 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/contracts/test/metadata.ral +0 -17
- package/contracts/test/warnings.ral +0 -5
- 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 -197
- package/test/events.test.ts +0 -138
- package/test/transaction.test.ts +0 -72
package/src/contract/events.ts
CHANGED
|
@@ -16,14 +16,15 @@ 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 { web3 } from '..'
|
|
20
|
+
import { node } from '../api'
|
|
20
21
|
import { Subscription, SubscribeOptions } from '../utils'
|
|
21
22
|
|
|
22
|
-
export class EventSubscription extends Subscription<ContractEvent> {
|
|
23
|
+
export class EventSubscription extends Subscription<node.ContractEvent> {
|
|
23
24
|
readonly contractAddress: string
|
|
24
25
|
private fromCount: number
|
|
25
26
|
|
|
26
|
-
constructor(options: SubscribeOptions<ContractEvent>, contractAddress: string, fromCount?: number) {
|
|
27
|
+
constructor(options: SubscribeOptions<node.ContractEvent>, contractAddress: string, fromCount?: number) {
|
|
27
28
|
super(options)
|
|
28
29
|
this.contractAddress = contractAddress
|
|
29
30
|
this.fromCount = typeof fromCount === 'undefined' ? 0 : fromCount
|
|
@@ -44,7 +45,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
|
|
|
44
45
|
|
|
45
46
|
override async polling(): Promise<void> {
|
|
46
47
|
try {
|
|
47
|
-
const events = await
|
|
48
|
+
const events = await web3.getCurrentNodeProvider().events.getEventsContractContractaddress(this.contractAddress, {
|
|
48
49
|
start: this.fromCount
|
|
49
50
|
})
|
|
50
51
|
if (this.cancelled) {
|
|
@@ -67,7 +68,7 @@ export class EventSubscription extends Subscription<ContractEvent> {
|
|
|
67
68
|
}
|
|
68
69
|
|
|
69
70
|
export function subscribeToEvents(
|
|
70
|
-
options: SubscribeOptions<ContractEvent>,
|
|
71
|
+
options: SubscribeOptions<node.ContractEvent>,
|
|
71
72
|
contractAddress: string,
|
|
72
73
|
fromCount?: number
|
|
73
74
|
): 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,22 @@ 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
|
+
Number256,
|
|
24
|
+
toApiNumber256,
|
|
25
|
+
toApiNumber256Optional,
|
|
26
|
+
toApiTokens,
|
|
27
|
+
Token
|
|
28
|
+
} from '../api'
|
|
21
29
|
import { node } from '../api'
|
|
22
30
|
import * as utils from '../utils'
|
|
23
31
|
import { Eq, assertType } from '../utils'
|
|
24
32
|
import blake from 'blakejs'
|
|
25
|
-
import {
|
|
33
|
+
import { web3 } from '..'
|
|
34
|
+
|
|
35
|
+
export type OutputRef = node.OutputRef
|
|
26
36
|
|
|
27
37
|
const ec = new EC('secp256k1')
|
|
28
38
|
|
|
@@ -49,13 +59,13 @@ export type GetAccountsResult = Account[]
|
|
|
49
59
|
|
|
50
60
|
export interface SignTransferTxParams {
|
|
51
61
|
signerAddress: string
|
|
52
|
-
destinations:
|
|
53
|
-
utxos?:
|
|
62
|
+
destinations: Destination[]
|
|
63
|
+
utxos?: OutputRef[]
|
|
54
64
|
gasAmount?: number
|
|
55
|
-
gasPrice?:
|
|
65
|
+
gasPrice?: Number256
|
|
56
66
|
submitTx?: boolean
|
|
57
67
|
}
|
|
58
|
-
assertType<Eq<SignTransferTxParams, TxBuildParams<node.BuildTransaction>>>()
|
|
68
|
+
assertType<Eq<keyof SignTransferTxParams, keyof TxBuildParams<node.BuildTransaction>>>()
|
|
59
69
|
export interface SignTransferTxResult {
|
|
60
70
|
fromGroup: number
|
|
61
71
|
toGroup: number
|
|
@@ -68,14 +78,14 @@ assertType<Eq<SignTransferTxResult, SignResult>>()
|
|
|
68
78
|
export interface SignDeployContractTxParams {
|
|
69
79
|
signerAddress: string
|
|
70
80
|
bytecode: string
|
|
71
|
-
initialAttoAlphAmount?:
|
|
81
|
+
initialAttoAlphAmount?: Number256
|
|
72
82
|
initialTokenAmounts?: Token[]
|
|
73
|
-
issueTokenAmount?:
|
|
83
|
+
issueTokenAmount?: Number256
|
|
74
84
|
gasAmount?: number
|
|
75
|
-
gasPrice?:
|
|
85
|
+
gasPrice?: Number256
|
|
76
86
|
submitTx?: boolean
|
|
77
87
|
}
|
|
78
|
-
assertType<Eq<SignDeployContractTxParams, TxBuildParams<node.BuildDeployContractTx>>>()
|
|
88
|
+
assertType<Eq<keyof SignDeployContractTxParams, keyof TxBuildParams<node.BuildDeployContractTx>>>()
|
|
79
89
|
export interface SignDeployContractTxResult {
|
|
80
90
|
fromGroup: number
|
|
81
91
|
toGroup: number
|
|
@@ -91,12 +101,12 @@ export interface SignExecuteScriptTxParams {
|
|
|
91
101
|
signerAddress: string
|
|
92
102
|
bytecode: string
|
|
93
103
|
attoAlphAmount?: string
|
|
94
|
-
tokens?:
|
|
104
|
+
tokens?: Token[]
|
|
95
105
|
gasAmount?: number
|
|
96
106
|
gasPrice?: string
|
|
97
107
|
submitTx?: boolean
|
|
98
108
|
}
|
|
99
|
-
assertType<Eq<SignExecuteScriptTxParams, TxBuildParams<node.BuildExecuteScriptTx>>>()
|
|
109
|
+
assertType<Eq<keyof SignExecuteScriptTxParams, keyof TxBuildParams<node.BuildExecuteScriptTx>>>()
|
|
100
110
|
export interface SignExecuteScriptTxResult {
|
|
101
111
|
fromGroup: number
|
|
102
112
|
toGroup: number
|
|
@@ -152,7 +162,6 @@ export interface SignerProvider {
|
|
|
152
162
|
}
|
|
153
163
|
|
|
154
164
|
export abstract class SignerWithNodeProvider implements SignerProvider {
|
|
155
|
-
readonly provider: NodeProvider
|
|
156
165
|
alwaysSubmitTx: boolean
|
|
157
166
|
|
|
158
167
|
abstract getAccounts(): Promise<Account[]>
|
|
@@ -167,20 +176,26 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
|
|
|
167
176
|
}
|
|
168
177
|
}
|
|
169
178
|
|
|
170
|
-
|
|
171
|
-
|
|
179
|
+
abstract setActiveAccount(addressIndex: number): Promise<void>
|
|
180
|
+
abstract setActiveAccount(address: string): Promise<void>
|
|
181
|
+
abstract setActiveAccount(input: unknown): Promise<void>
|
|
182
|
+
|
|
183
|
+
abstract getActiveAccount(): Promise<Account>
|
|
184
|
+
|
|
185
|
+
constructor(alwaysSubmitTx: boolean) {
|
|
172
186
|
this.alwaysSubmitTx = alwaysSubmitTx
|
|
173
187
|
}
|
|
174
188
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
189
|
+
async submitTransaction(unsignedTx: string, signerAddress?: string): Promise<SubmissionResult> {
|
|
190
|
+
const decoded = await web3
|
|
191
|
+
.getCurrentNodeProvider()
|
|
192
|
+
.transactions.postTransactionsDecodeUnsignedTx({ unsignedTx: unsignedTx })
|
|
193
|
+
const txId = decoded.unsignedTx.txId
|
|
178
194
|
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
const signature = await this.signRaw(address, txHash)
|
|
195
|
+
const address = typeof signerAddress !== 'undefined' ? signerAddress : (await this.getActiveAccount()).address
|
|
196
|
+
const signature = await this.signRaw(address, txId)
|
|
182
197
|
const params: node.SubmitTransaction = { unsignedTx: unsignedTx, signature: signature }
|
|
183
|
-
return
|
|
198
|
+
return web3.getCurrentNodeProvider().transactions.postTransactionsSubmit(params)
|
|
184
199
|
}
|
|
185
200
|
|
|
186
201
|
private shouldSubmitTx(params: SubmitTx): boolean {
|
|
@@ -206,7 +221,12 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
|
|
|
206
221
|
}
|
|
207
222
|
|
|
208
223
|
async buildTransferTx(params: SignTransferTxParams): Promise<node.BuildTransactionResult> {
|
|
209
|
-
|
|
224
|
+
const data: node.BuildTransaction = {
|
|
225
|
+
...(await this.usePublicKey(params)),
|
|
226
|
+
destinations: toApiDestinations(params.destinations),
|
|
227
|
+
gasPrice: toApiNumber256Optional(params.gasPrice)
|
|
228
|
+
}
|
|
229
|
+
return web3.getCurrentNodeProvider().transactions.postTransactionsBuild(data)
|
|
210
230
|
}
|
|
211
231
|
|
|
212
232
|
async signDeployContractTx(params: SignDeployContractTxParams): Promise<SignDeployContractTxResult> {
|
|
@@ -220,7 +240,14 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
|
|
|
220
240
|
}
|
|
221
241
|
|
|
222
242
|
async buildContractCreationTx(params: SignDeployContractTxParams): Promise<node.BuildDeployContractTxResult> {
|
|
223
|
-
|
|
243
|
+
const data: node.BuildDeployContractTx = {
|
|
244
|
+
...(await this.usePublicKey(params)),
|
|
245
|
+
initialAttoAlphAmount: toApiNumber256Optional(params.initialAttoAlphAmount),
|
|
246
|
+
initialTokenAmounts: toApiTokens(params.initialTokenAmounts),
|
|
247
|
+
issueTokenAmount: toApiNumber256Optional(params.issueTokenAmount),
|
|
248
|
+
gasPrice: toApiNumber256Optional(params.gasPrice)
|
|
249
|
+
}
|
|
250
|
+
return web3.getCurrentNodeProvider().contracts.postContractsUnsignedTxDeployContract(data)
|
|
224
251
|
}
|
|
225
252
|
|
|
226
253
|
async signExecuteScriptTx(params: SignExecuteScriptTxParams): Promise<SignExecuteScriptTxResult> {
|
|
@@ -229,14 +256,18 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
|
|
|
229
256
|
}
|
|
230
257
|
|
|
231
258
|
async buildScriptTx(params: SignExecuteScriptTxParams): Promise<node.BuildExecuteScriptTxResult> {
|
|
232
|
-
|
|
259
|
+
const data: node.BuildExecuteScriptTx = {
|
|
260
|
+
...(await this.usePublicKey(params)),
|
|
261
|
+
tokens: toApiTokens(params.tokens)
|
|
262
|
+
}
|
|
263
|
+
return web3.getCurrentNodeProvider().contracts.postContractsUnsignedTxExecuteScript(data)
|
|
233
264
|
}
|
|
234
265
|
|
|
235
266
|
// in general, wallet should show the decoded information to user for confirmation
|
|
236
267
|
// please overwrite this function for real wallet
|
|
237
268
|
async signUnsignedTx(params: SignUnsignedTxParams): Promise<SignUnsignedTxResult> {
|
|
238
269
|
const data = { unsignedTx: params.unsignedTx }
|
|
239
|
-
const decoded = await
|
|
270
|
+
const decoded = await web3.getCurrentNodeProvider().transactions.postTransactionsDecodeUnsignedTx(data)
|
|
240
271
|
return this.handleSign(
|
|
241
272
|
{
|
|
242
273
|
fromGroup: decoded.fromGroup,
|
|
@@ -257,7 +288,7 @@ export abstract class SignerWithNodeProvider implements SignerProvider {
|
|
|
257
288
|
const signature = await this.signRaw(response.signerAddress, response.txId)
|
|
258
289
|
// submit the tx if required
|
|
259
290
|
if (submitTx) {
|
|
260
|
-
await
|
|
291
|
+
await web3.getCurrentNodeProvider().transactions.postTransactionsSubmit({
|
|
261
292
|
unsignedTx: response.unsignedTx,
|
|
262
293
|
signature: signature
|
|
263
294
|
})
|
|
@@ -311,3 +342,24 @@ export function verifySignedMessage(message: string, publicKey: string, signatur
|
|
|
311
342
|
const messageHash = blake.blake2b(extendedMessage, undefined, 32)
|
|
312
343
|
return verifyHexString(utils.binToHex(messageHash), publicKey, signature)
|
|
313
344
|
}
|
|
345
|
+
|
|
346
|
+
export interface Destination {
|
|
347
|
+
address: string
|
|
348
|
+
attoAlphAmount: Number256
|
|
349
|
+
tokens?: Token[]
|
|
350
|
+
lockTime?: number
|
|
351
|
+
message?: string
|
|
352
|
+
}
|
|
353
|
+
assertType<Eq<keyof Destination, keyof node.Destination>>
|
|
354
|
+
|
|
355
|
+
export function toApiDestination(data: Destination): node.Destination {
|
|
356
|
+
return { ...data, attoAlphAmount: toApiNumber256(data.attoAlphAmount), tokens: toApiTokens(data.tokens) }
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
export function toApiDestinations(data: Destination[]): node.Destination[] {
|
|
360
|
+
return data.map(toApiDestination)
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export function fromApiDestination(data: node.Destination): Destination {
|
|
364
|
+
return { ...data, attoAlphAmount: fromApiNumber256(data.attoAlphAmount), tokens: fromApiTokens(data.tokens) }
|
|
365
|
+
}
|
package/src/transaction/index.ts
CHANGED
|
@@ -16,9 +16,12 @@ 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 { web3 } from '..'
|
|
20
|
+
import { node } from '../api'
|
|
20
21
|
import { Subscription, SubscribeOptions } from '../utils'
|
|
21
22
|
|
|
23
|
+
export type TxStatus = node.TxStatus
|
|
24
|
+
|
|
22
25
|
export class TxStatusSubscription extends Subscription<TxStatus> {
|
|
23
26
|
readonly txId: string
|
|
24
27
|
readonly fromGroup?: number
|
|
@@ -35,7 +38,7 @@ export class TxStatusSubscription extends Subscription<TxStatus> {
|
|
|
35
38
|
|
|
36
39
|
override async polling(): Promise<void> {
|
|
37
40
|
try {
|
|
38
|
-
const txStatus = await
|
|
41
|
+
const txStatus = await web3.getCurrentNodeProvider().transactions.getTransactionsStatus({
|
|
39
42
|
txId: this.txId,
|
|
40
43
|
fromGroup: this.fromGroup,
|
|
41
44
|
toGroup: this.toGroup
|
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,20 +17,19 @@ 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>
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
export abstract class Subscription<Message> {
|
|
33
|
-
provider: NodeProvider
|
|
34
33
|
pollingInterval: number
|
|
35
34
|
|
|
36
35
|
protected messageCallback: MessageCallback<Message>
|
|
@@ -40,7 +39,6 @@ export abstract class Subscription<Message> {
|
|
|
40
39
|
protected cancelled: boolean
|
|
41
40
|
|
|
42
41
|
constructor(options: SubscribeOptions<Message>) {
|
|
43
|
-
this.provider = options.provider
|
|
44
42
|
this.pollingInterval = options.pollingInterval
|
|
45
43
|
this.messageCallback = options.messageCallback
|
|
46
44
|
this.errorCallback = options.errorCallback
|
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')
|
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
|
-
}
|