@alephium/web3 1.8.1 → 1.8.2
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/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/address/address.js +29 -11
- package/dist/src/api/types.js +9 -16
- package/dist/src/contract/contract.d.ts +6 -0
- package/dist/src/contract/contract.js +46 -7
- package/dist/src/contract/ralph.js +2 -4
- package/dist/src/error.d.ts +4 -0
- package/dist/src/error.js +36 -0
- package/dist/src/exchange/exchange.js +4 -9
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/token/nft.js +2 -1
- package/dist/src/utils/bs58.d.ts +1 -0
- package/dist/src/utils/bs58.js +11 -1
- package/package.json +1 -1
- package/src/address/address.ts +6 -12
- package/src/api/types.ts +11 -16
- package/src/codec/transaction-codec.ts +1 -1
- package/src/contract/contract.ts +50 -7
- package/src/contract/ralph.ts +2 -4
- package/src/error.ts +37 -0
- package/src/exchange/exchange.ts +5 -9
- package/src/index.ts +1 -0
- package/src/signer/tx-builder.ts +1 -2
- package/src/token/nft.ts +2 -1
- package/src/utils/bs58.ts +9 -0
package/src/contract/contract.ts
CHANGED
|
@@ -54,7 +54,9 @@ import {
|
|
|
54
54
|
WebCrypto,
|
|
55
55
|
hexToBinUnsafe,
|
|
56
56
|
isDevnet,
|
|
57
|
-
HexString
|
|
57
|
+
HexString,
|
|
58
|
+
isHexString,
|
|
59
|
+
hexToString
|
|
58
60
|
} from '../utils'
|
|
59
61
|
import { contractIdFromAddress, groupOfAddress, addressFromContractId, subContractId } from '../address'
|
|
60
62
|
import { getCurrentNodeProvider } from '../global'
|
|
@@ -87,6 +89,7 @@ import {
|
|
|
87
89
|
i32Codec,
|
|
88
90
|
BytesConst
|
|
89
91
|
} from '../codec'
|
|
92
|
+
import { TraceableError } from '../error'
|
|
90
93
|
|
|
91
94
|
const crypto = new WebCrypto()
|
|
92
95
|
|
|
@@ -405,7 +408,7 @@ export class Contract extends Artifact {
|
|
|
405
408
|
printDebugMessages(funcName: string, messages: DebugMessage[]) {
|
|
406
409
|
if (isContractDebugMessageEnabled() && messages.length != 0) {
|
|
407
410
|
console.log(`Testing ${this.name}.${funcName}:`)
|
|
408
|
-
messages.forEach((m) =>
|
|
411
|
+
messages.forEach((m) => printDebugMessage(m))
|
|
409
412
|
}
|
|
410
413
|
}
|
|
411
414
|
|
|
@@ -503,6 +506,7 @@ export class Contract extends Artifact {
|
|
|
503
506
|
fieldNames: ['address'],
|
|
504
507
|
fieldTypes: ['Address']
|
|
505
508
|
}
|
|
509
|
+
static DebugEventIndex = -3
|
|
506
510
|
|
|
507
511
|
static fromApiEvent(
|
|
508
512
|
event: node.ContractEventByTxId,
|
|
@@ -602,7 +606,7 @@ export class Contract extends Artifact {
|
|
|
602
606
|
: this.bytecode
|
|
603
607
|
return ralph.buildContractByteCode(bytecode, initialFields, this.fieldsSig, this.structs)
|
|
604
608
|
} catch (error) {
|
|
605
|
-
throw new
|
|
609
|
+
throw new TraceableError(`Failed to build bytecode for contract ${this.name}`, error)
|
|
606
610
|
}
|
|
607
611
|
}
|
|
608
612
|
|
|
@@ -770,7 +774,7 @@ export class Script extends Artifact {
|
|
|
770
774
|
try {
|
|
771
775
|
return ralph.buildScriptByteCode(this.bytecodeTemplate, initialFields, this.fieldsSig, this.structs)
|
|
772
776
|
} catch (error) {
|
|
773
|
-
throw new
|
|
777
|
+
throw new TraceableError(`Failed to build bytecode for script ${this.name}`, error)
|
|
774
778
|
}
|
|
775
779
|
}
|
|
776
780
|
}
|
|
@@ -1494,6 +1498,38 @@ export async function testMethod<
|
|
|
1494
1498
|
} as TestContractResult<R, M>
|
|
1495
1499
|
}
|
|
1496
1500
|
|
|
1501
|
+
function printDebugMessage(m: node.DebugMessage) {
|
|
1502
|
+
console.log(`> Contract @ ${m.contractAddress} - ${m.message}`)
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
export async function getDebugMessagesFromTx(txId: HexString, provider?: NodeProvider) {
|
|
1506
|
+
if (isHexString(txId) && txId.length === 64) {
|
|
1507
|
+
const nodeProvider = provider ?? getCurrentNodeProvider()
|
|
1508
|
+
const events = await nodeProvider.events.getEventsTxIdTxid(txId)
|
|
1509
|
+
return events.events
|
|
1510
|
+
.filter((e) => e.eventIndex === Contract.DebugEventIndex)
|
|
1511
|
+
.map((e) => {
|
|
1512
|
+
if (e.fields.length === 1 && e.fields[0].type === 'ByteVec') {
|
|
1513
|
+
return {
|
|
1514
|
+
contractAddress: e.contractAddress,
|
|
1515
|
+
message: hexToString(e.fields[0].value as string)
|
|
1516
|
+
}
|
|
1517
|
+
} else {
|
|
1518
|
+
throw new Error(`Invalid debug log: ${JSON.stringify(e.fields)}`)
|
|
1519
|
+
}
|
|
1520
|
+
})
|
|
1521
|
+
} else {
|
|
1522
|
+
throw new Error(`Invalid tx id: ${txId}`)
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
export async function printDebugMessagesFromTx(txId: HexString, provider?: NodeProvider) {
|
|
1527
|
+
const messages = await getDebugMessagesFromTx(txId, provider)
|
|
1528
|
+
if (messages.length > 0) {
|
|
1529
|
+
messages.forEach((m) => printDebugMessage(m))
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
|
|
1497
1533
|
export class RalphMap<K extends Val, V extends Val> {
|
|
1498
1534
|
private readonly groupIndex: number
|
|
1499
1535
|
constructor(
|
|
@@ -1546,7 +1582,10 @@ export async function getMapItem<R extends Val>(
|
|
|
1546
1582
|
// the map item contract does not exist
|
|
1547
1583
|
return undefined
|
|
1548
1584
|
}
|
|
1549
|
-
throw
|
|
1585
|
+
throw new TraceableError(
|
|
1586
|
+
`Failed to get value from map ${mapName}, key: ${key}, parent contract id: ${parentContractId}`,
|
|
1587
|
+
error
|
|
1588
|
+
)
|
|
1550
1589
|
}
|
|
1551
1590
|
}
|
|
1552
1591
|
|
|
@@ -1841,7 +1880,11 @@ export async function signExecuteMethod<I extends ContractInstance, F extends Fi
|
|
|
1841
1880
|
gasPrice: params.gasPrice
|
|
1842
1881
|
}
|
|
1843
1882
|
|
|
1844
|
-
|
|
1883
|
+
const result = await signer.signAndSubmitExecuteScriptTx(signerParams)
|
|
1884
|
+
if (isContractDebugMessageEnabled() && (await contract.contract.isDevnet(signer))) {
|
|
1885
|
+
await printDebugMessagesFromTx(result.txId, signer.nodeProvider)
|
|
1886
|
+
}
|
|
1887
|
+
return result
|
|
1845
1888
|
}
|
|
1846
1889
|
|
|
1847
1890
|
function getBytecodeTemplate(
|
|
@@ -2097,7 +2140,7 @@ export async function getContractEventsCurrentCount(contractAddress: Address): P
|
|
|
2097
2140
|
if (error instanceof Error && error.message.includes(`${contractAddress} not found`)) {
|
|
2098
2141
|
return 0
|
|
2099
2142
|
}
|
|
2100
|
-
throw error
|
|
2143
|
+
throw new TraceableError(`Failed to get the event count for the contract ${contractAddress}`, error)
|
|
2101
2144
|
})
|
|
2102
2145
|
}
|
|
2103
2146
|
|
package/src/contract/ralph.ts
CHANGED
|
@@ -46,6 +46,7 @@ import {
|
|
|
46
46
|
U256Const5
|
|
47
47
|
} from '../codec'
|
|
48
48
|
import { boolCodec } from '../codec/codec'
|
|
49
|
+
import { TraceableError } from '../error'
|
|
49
50
|
|
|
50
51
|
export function encodeByteVec(hex: string): Uint8Array {
|
|
51
52
|
if (!isHexString(hex)) {
|
|
@@ -423,10 +424,7 @@ function _encodeField<T>(fieldName: string, encodeFunc: () => T): T {
|
|
|
423
424
|
try {
|
|
424
425
|
return encodeFunc()
|
|
425
426
|
} catch (error) {
|
|
426
|
-
|
|
427
|
-
throw new Error(`Invalid ${fieldName}, error: ${error.message}`)
|
|
428
|
-
}
|
|
429
|
-
throw error
|
|
427
|
+
throw new TraceableError(`Failed to encode the field ${fieldName}`, error)
|
|
430
428
|
}
|
|
431
429
|
}
|
|
432
430
|
|
package/src/error.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
export class TraceableError extends Error {
|
|
20
|
+
trace: any | undefined
|
|
21
|
+
|
|
22
|
+
constructor(message?: string, innerError?: any) {
|
|
23
|
+
const innerErrorMessage =
|
|
24
|
+
innerError === undefined ? undefined : innerError instanceof Error ? innerError.message : `${innerError}`
|
|
25
|
+
super(innerErrorMessage ? `${message}, error: ${innerErrorMessage}` : message)
|
|
26
|
+
this.trace = innerError
|
|
27
|
+
|
|
28
|
+
const actualProto = new.target.prototype
|
|
29
|
+
|
|
30
|
+
if (Object.setPrototypeOf) {
|
|
31
|
+
Object.setPrototypeOf(this, actualProto)
|
|
32
|
+
} else {
|
|
33
|
+
const object = this as any
|
|
34
|
+
object.__proto__ = actualProto
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/exchange/exchange.ts
CHANGED
|
@@ -17,19 +17,15 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { AddressType, addressFromPublicKey, addressFromScript } from '../address'
|
|
20
|
-
import {
|
|
20
|
+
import { base58ToBytes, binToHex, hexToBinUnsafe, isHexString } from '../utils'
|
|
21
21
|
import { Transaction } from '../api/api-alephium'
|
|
22
22
|
import { Address } from '../signer'
|
|
23
23
|
import { P2SH, unlockScriptCodec } from '../codec/unlock-script-codec'
|
|
24
24
|
import { scriptCodec } from '../codec/script-codec'
|
|
25
|
+
import { TraceableError } from '../error'
|
|
25
26
|
|
|
26
27
|
export function validateExchangeAddress(address: string) {
|
|
27
|
-
|
|
28
|
-
try {
|
|
29
|
-
decoded = bs58.decode(address)
|
|
30
|
-
} catch (_) {
|
|
31
|
-
throw new Error('Invalid base58 string')
|
|
32
|
-
}
|
|
28
|
+
const decoded = base58ToBytes(address)
|
|
33
29
|
if (decoded.length === 0) throw new Error('Address is empty')
|
|
34
30
|
const addressType = decoded[0]
|
|
35
31
|
if (addressType !== AddressType.P2PKH && addressType !== AddressType.P2SH) {
|
|
@@ -106,8 +102,8 @@ export function getAddressFromUnlockScript(unlockScript: string): Address {
|
|
|
106
102
|
let p2sh: P2SH
|
|
107
103
|
try {
|
|
108
104
|
p2sh = unlockScriptCodec.decode(decoded).value as P2SH
|
|
109
|
-
} catch (
|
|
110
|
-
throw new
|
|
105
|
+
} catch (e) {
|
|
106
|
+
throw new TraceableError(`Invalid p2sh unlock script: ${unlockScript}`, e)
|
|
111
107
|
}
|
|
112
108
|
return addressFromScript(scriptCodec.encode(p2sh.script))
|
|
113
109
|
}
|
package/src/index.ts
CHANGED
package/src/signer/tx-builder.ts
CHANGED
|
@@ -31,7 +31,6 @@ import {
|
|
|
31
31
|
SignExecuteScriptChainedTxResult,
|
|
32
32
|
SignExecuteScriptTxParams,
|
|
33
33
|
SignExecuteScriptTxResult,
|
|
34
|
-
SignTransferChainedTxResult,
|
|
35
34
|
SignTransferTxParams,
|
|
36
35
|
SignTransferTxResult,
|
|
37
36
|
SignUnsignedTxParams,
|
|
@@ -40,7 +39,7 @@ import {
|
|
|
40
39
|
import { unsignedTxCodec } from '../codec'
|
|
41
40
|
import { groupIndexOfTransaction } from '../transaction'
|
|
42
41
|
import { blakeHash } from '../codec/hash'
|
|
43
|
-
import { BuildDeployContractTxResult, BuildChainedTx
|
|
42
|
+
import { BuildDeployContractTxResult, BuildChainedTx } from '../api/api-alephium'
|
|
44
43
|
|
|
45
44
|
export abstract class TransactionBuilder {
|
|
46
45
|
abstract get nodeProvider(): NodeProvider
|
package/src/token/nft.ts
CHANGED
|
@@ -21,6 +21,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
21
21
|
|
|
22
22
|
import 'cross-fetch/polyfill'
|
|
23
23
|
import { NFTCollectionUriMetaData, NFTTokenUriMetaData } from '../api'
|
|
24
|
+
import { TraceableError } from '../error'
|
|
24
25
|
|
|
25
26
|
export const validNFTTokenUriMetaDataFields = ['name', 'description', 'image', 'attributes']
|
|
26
27
|
export const validNFTTokenUriMetaDataAttributesFields = ['trait_type', 'value']
|
|
@@ -130,7 +131,7 @@ async function fetchNFTMetadata(nftBaseUri: string, index: number) {
|
|
|
130
131
|
try {
|
|
131
132
|
return await (await fetch(`${nftBaseUri}${index}`)).json()
|
|
132
133
|
} catch (e) {
|
|
133
|
-
throw new
|
|
134
|
+
throw new TraceableError(`Error fetching NFT metadata from ${nftBaseUri}${index}`, e)
|
|
134
135
|
}
|
|
135
136
|
}
|
|
136
137
|
|
package/src/utils/bs58.ts
CHANGED
|
@@ -18,6 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
18
18
|
|
|
19
19
|
/** This source is under MIT License and come originally from https://github.com/cryptocoinjs/bs58 **/
|
|
20
20
|
import basex from 'base-x'
|
|
21
|
+
import { TraceableError } from '../error'
|
|
21
22
|
|
|
22
23
|
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
|
23
24
|
|
|
@@ -34,4 +35,12 @@ export function isBase58(s: string): boolean {
|
|
|
34
35
|
}
|
|
35
36
|
}
|
|
36
37
|
|
|
38
|
+
export function base58ToBytes(s: string): Uint8Array {
|
|
39
|
+
try {
|
|
40
|
+
return bs58.decode(s)
|
|
41
|
+
} catch (e) {
|
|
42
|
+
throw new TraceableError(`Invalid base58 string ${s}`, e)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
37
46
|
export default bs58
|