@alephium/web3 0.41.0 → 0.43.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/{utils → address}/address.js +15 -19
- package/dist/src/address/index.d.ts +1 -0
- package/dist/src/address/index.js +34 -0
- package/dist/src/api/node-provider.js +10 -9
- package/dist/src/api/types.js +1 -1
- package/dist/src/codec/array-codec.d.ts +2 -3
- package/dist/src/codec/array-codec.js +1 -2
- package/dist/src/codec/asset-output-codec.d.ts +3 -4
- package/dist/src/codec/asset-output-codec.js +10 -12
- package/dist/src/codec/bigint-codec.d.ts +2 -3
- package/dist/src/codec/bigint-codec.js +2 -3
- package/dist/src/codec/bytestring-codec.d.ts +5 -6
- package/dist/src/codec/bytestring-codec.js +5 -5
- package/dist/src/codec/codec.d.ts +2 -3
- package/dist/src/codec/compact-int-codec.d.ts +12 -13
- package/dist/src/codec/compact-int-codec.js +22 -22
- package/dist/src/codec/contract-codec.d.ts +5 -6
- package/dist/src/codec/contract-codec.js +24 -24
- package/dist/src/codec/contract-output-codec.d.ts +2 -3
- package/dist/src/codec/contract-output-codec.js +8 -10
- package/dist/src/codec/contract-output-ref-codec.d.ts +3 -4
- package/dist/src/codec/contract-output-ref-codec.js +2 -2
- package/dist/src/codec/either-codec.d.ts +2 -3
- package/dist/src/codec/either-codec.js +1 -2
- package/dist/src/codec/hash.d.ts +2 -3
- package/dist/src/codec/hash.js +17 -0
- package/dist/src/codec/input-codec.d.ts +3 -4
- package/dist/src/codec/input-codec.js +6 -6
- package/dist/src/codec/instr-codec.d.ts +2 -3
- package/dist/src/codec/instr-codec.js +1 -2
- package/dist/src/codec/lockup-script-codec.d.ts +5 -6
- package/dist/src/codec/lockup-script-codec.js +1 -2
- package/dist/src/codec/long-codec.d.ts +2 -3
- package/dist/src/codec/long-codec.js +1 -2
- package/dist/src/codec/method-codec.d.ts +2 -3
- package/dist/src/codec/method-codec.js +1 -2
- package/dist/src/codec/option-codec.d.ts +3 -4
- package/dist/src/codec/option-codec.js +2 -3
- package/dist/src/codec/script-codec.d.ts +4 -5
- package/dist/src/codec/script-codec.js +1 -3
- package/dist/src/codec/signature-codec.d.ts +3 -4
- package/dist/src/codec/signature-codec.js +17 -0
- package/dist/src/codec/signed-int-codec.d.ts +2 -3
- package/dist/src/codec/signed-int-codec.js +1 -2
- package/dist/src/codec/token-codec.d.ts +3 -4
- package/dist/src/codec/token-codec.js +3 -3
- package/dist/src/codec/transaction-codec.d.ts +4 -5
- package/dist/src/codec/transaction-codec.js +12 -13
- package/dist/src/codec/unlock-script-codec.d.ts +5 -6
- package/dist/src/codec/unlock-script-codec.js +14 -15
- package/dist/src/codec/unsigned-tx-codec.d.ts +4 -5
- package/dist/src/codec/unsigned-tx-codec.js +4 -5
- package/dist/src/contract/contract.d.ts +8 -95
- package/dist/src/contract/contract.js +31 -564
- package/dist/src/contract/ralph.d.ts +1 -1
- package/dist/src/contract/ralph.js +9 -10
- package/dist/src/{utils → exchange}/exchange.js +5 -5
- package/dist/src/exchange/index.d.ts +1 -0
- package/dist/src/exchange/index.js +25 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +2 -0
- package/dist/src/signer/signer.js +4 -5
- package/dist/src/signer/tx-builder.js +3 -3
- package/dist/src/transaction/index.d.ts +1 -0
- package/dist/src/transaction/index.js +1 -0
- package/dist/src/transaction/utils.d.ts +2 -0
- package/dist/src/transaction/utils.js +34 -0
- package/dist/src/utils/index.d.ts +0 -2
- package/dist/src/utils/index.js +0 -7
- package/dist/src/utils/sign.js +1 -1
- package/dist/src/utils/utils.d.ts +1 -0
- package/dist/src/utils/utils.js +30 -11
- package/dist/src/utils/webcrypto.js +3 -4
- package/package.json +1 -2
- package/src/{utils → address}/address.ts +15 -19
- package/src/address/index.ts +19 -0
- package/src/api/node-provider.ts +2 -9
- package/src/api/types.ts +2 -2
- package/src/codec/array-codec.ts +3 -4
- package/src/codec/asset-output-codec.ts +16 -18
- package/src/codec/bigint-codec.ts +4 -5
- package/src/codec/bytestring-codec.ts +8 -8
- package/src/codec/codec.ts +2 -3
- package/src/codec/compact-int-codec.ts +36 -37
- package/src/codec/contract-codec.ts +28 -27
- package/src/codec/contract-output-codec.ts +11 -13
- package/src/codec/contract-output-ref-codec.ts +5 -5
- package/src/codec/either-codec.ts +3 -4
- package/src/codec/hash.ts +2 -3
- package/src/codec/input-codec.ts +10 -10
- package/src/codec/instr-codec.ts +3 -4
- package/src/codec/lockup-script-codec.ts +8 -9
- package/src/codec/long-codec.ts +3 -4
- package/src/codec/method-codec.ts +3 -4
- package/src/codec/option-codec.ts +4 -5
- package/src/codec/script-codec.ts +5 -7
- package/src/codec/signature-codec.ts +3 -4
- package/src/codec/signed-int-codec.ts +3 -4
- package/src/codec/token-codec.ts +6 -6
- package/src/codec/transaction-codec.ts +17 -18
- package/src/codec/unlock-script-codec.ts +26 -27
- package/src/codec/unsigned-tx-codec.ts +10 -11
- package/src/contract/contract.ts +18 -790
- package/src/contract/ralph.ts +10 -11
- package/src/{utils → exchange}/exchange.ts +3 -11
- package/src/exchange/index.ts +19 -0
- package/src/index.ts +2 -0
- package/src/signer/signer.ts +2 -3
- package/src/signer/tx-builder.ts +2 -2
- package/src/transaction/index.ts +1 -0
- package/src/transaction/utils.ts +38 -0
- package/src/utils/index.ts +0 -2
- package/src/utils/sign.ts +1 -1
- package/src/utils/utils.ts +29 -10
- package/src/utils/webcrypto.ts +3 -4
- package/webpack.config.js +1 -6
- package/dist/src/utils/error.d.ts +0 -15
- package/dist/src/utils/error.js +0 -66
- package/src/utils/error.ts +0 -77
- /package/dist/src/{utils → address}/address.d.ts +0 -0
- /package/dist/src/{utils → exchange}/exchange.d.ts +0 -0
|
@@ -15,17 +15,16 @@ GNU Lesser General Public License for more details.
|
|
|
15
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { ArrayCodec, DecodedArray } from './array-codec'
|
|
21
20
|
import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
|
|
22
21
|
import { signedIntCodec } from './signed-int-codec'
|
|
23
22
|
import { longCodec } from './long-codec'
|
|
24
23
|
import { ByteString, byteStringCodec } from './bytestring-codec'
|
|
25
|
-
import { LockupScript, MultiSig,
|
|
24
|
+
import { LockupScript, MultiSig, P2SH, lockupScriptCodec } from './lockup-script-codec'
|
|
26
25
|
import { FixedAssetOutput } from '../api/api-alephium'
|
|
27
26
|
import { blakeHash, createHint } from './hash'
|
|
28
|
-
import { bs58, binToHex } from '../utils'
|
|
27
|
+
import { bs58, binToHex, hexToBinUnsafe, concatBytes } from '../utils'
|
|
29
28
|
import { Codec } from './codec'
|
|
30
29
|
import { PublicKeyHash } from './lockup-script-codec'
|
|
31
30
|
import { Token, tokensCodec } from './token-codec'
|
|
@@ -33,7 +32,7 @@ import { Token, tokensCodec } from './token-codec'
|
|
|
33
32
|
export interface AssetOutput {
|
|
34
33
|
amount: DecodedCompactInt
|
|
35
34
|
lockupScript: LockupScript
|
|
36
|
-
lockTime:
|
|
35
|
+
lockTime: Uint8Array
|
|
37
36
|
tokens: DecodedArray<Token>
|
|
38
37
|
additionalData: ByteString
|
|
39
38
|
}
|
|
@@ -56,17 +55,16 @@ export class AssetOutputCodec implements Codec<AssetOutput> {
|
|
|
56
55
|
type: byteStringCodec.parser
|
|
57
56
|
})
|
|
58
57
|
|
|
59
|
-
encode(input: AssetOutput):
|
|
60
|
-
const amount =
|
|
58
|
+
encode(input: AssetOutput): Uint8Array {
|
|
59
|
+
const amount = compactUnsignedIntCodec.encode(input.amount)
|
|
61
60
|
const lockupScript = lockupScriptCodec.encode(input.lockupScript)
|
|
62
|
-
const
|
|
63
|
-
const
|
|
64
|
-
const additionalData = Buffer.from(byteStringCodec.encode(input.additionalData))
|
|
61
|
+
const tokens = tokensCodec.encode(input.tokens.value)
|
|
62
|
+
const additionalData = byteStringCodec.encode(input.additionalData)
|
|
65
63
|
|
|
66
|
-
return
|
|
64
|
+
return concatBytes([amount, lockupScript, input.lockTime, tokens, additionalData])
|
|
67
65
|
}
|
|
68
66
|
|
|
69
|
-
decode(input:
|
|
67
|
+
decode(input: Uint8Array): AssetOutput {
|
|
70
68
|
return this.parser.parse(input)
|
|
71
69
|
}
|
|
72
70
|
|
|
@@ -79,13 +77,13 @@ export class AssetOutputCodec implements Codec<AssetOutput> {
|
|
|
79
77
|
const lockTime = Number(longCodec.decode(output.lockTime))
|
|
80
78
|
const tokens = output.tokens.value.map((token) => {
|
|
81
79
|
return {
|
|
82
|
-
id: token.tokenId
|
|
80
|
+
id: binToHex(token.tokenId),
|
|
83
81
|
amount: compactUnsignedIntCodec.toU256(token.amount).toString()
|
|
84
82
|
}
|
|
85
83
|
})
|
|
86
|
-
const message = output.additionalData.value
|
|
84
|
+
const message = binToHex(output.additionalData.value)
|
|
87
85
|
const scriptType = output.lockupScript.scriptType
|
|
88
|
-
const key = binToHex(blakeHash(
|
|
86
|
+
const key = binToHex(blakeHash(concatBytes([txIdBytes, signedIntCodec.encode(index)])))
|
|
89
87
|
const outputLockupScript = output.lockupScript.script
|
|
90
88
|
const address = bs58.encode(lockupScriptCodec.encode(output.lockupScript))
|
|
91
89
|
|
|
@@ -117,11 +115,11 @@ export class AssetOutputCodec implements Codec<AssetOutput> {
|
|
|
117
115
|
static fromFixedAssetOutput(fixedOutput: FixedAssetOutput): AssetOutput {
|
|
118
116
|
const amount: DecodedCompactInt = compactUnsignedIntCodec.fromU256(BigInt(fixedOutput.attoAlphAmount))
|
|
119
117
|
|
|
120
|
-
const lockTime
|
|
121
|
-
const lockupScript: LockupScript = lockupScriptCodec.decode(
|
|
118
|
+
const lockTime = longCodec.encode(BigInt(fixedOutput.lockTime))
|
|
119
|
+
const lockupScript: LockupScript = lockupScriptCodec.decode(bs58.decode(fixedOutput.address))
|
|
122
120
|
const tokensValue = fixedOutput.tokens.map((token) => {
|
|
123
121
|
return {
|
|
124
|
-
tokenId:
|
|
122
|
+
tokenId: hexToBinUnsafe(token.id),
|
|
125
123
|
amount: compactUnsignedIntCodec.fromU256(BigInt(token.amount))
|
|
126
124
|
}
|
|
127
125
|
})
|
|
@@ -129,7 +127,7 @@ export class AssetOutputCodec implements Codec<AssetOutput> {
|
|
|
129
127
|
length: compactUnsignedIntCodec.fromU32(tokensValue.length),
|
|
130
128
|
value: tokensValue
|
|
131
129
|
}
|
|
132
|
-
const additionalDataValue =
|
|
130
|
+
const additionalDataValue = hexToBinUnsafe(fixedOutput.message)
|
|
133
131
|
const additionalData: ByteString = {
|
|
134
132
|
length: compactUnsignedIntCodec.fromU32(additionalDataValue.length),
|
|
135
133
|
value: additionalDataValue
|
|
@@ -15,12 +15,11 @@ GNU Lesser General Public License for more details.
|
|
|
15
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
export class BigIntCodec {
|
|
20
|
-
static encode(value: bigint):
|
|
19
|
+
static encode(value: bigint): Uint8Array {
|
|
21
20
|
// Special case for zero.
|
|
22
21
|
if (value === 0n) {
|
|
23
|
-
return
|
|
22
|
+
return new Uint8Array([0])
|
|
24
23
|
}
|
|
25
24
|
|
|
26
25
|
const isNegative = value < 0n
|
|
@@ -62,10 +61,10 @@ export class BigIntCodec {
|
|
|
62
61
|
}
|
|
63
62
|
|
|
64
63
|
// The byte array needs to be reversed since we've constructed it in little-endian order.
|
|
65
|
-
return
|
|
64
|
+
return new Uint8Array(bytes.reverse())
|
|
66
65
|
}
|
|
67
66
|
|
|
68
|
-
static decode(encoded:
|
|
67
|
+
static decode(encoded: Uint8Array, signed: boolean): bigint {
|
|
69
68
|
// Special case for zero.
|
|
70
69
|
if (encoded.length === 1 && encoded[0] === 0) {
|
|
71
70
|
return 0n
|
|
@@ -15,14 +15,14 @@ GNU Lesser General Public License for more details.
|
|
|
15
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
|
|
21
20
|
import { Codec } from './codec'
|
|
21
|
+
import { concatBytes } from '../utils'
|
|
22
22
|
|
|
23
23
|
export interface ByteString {
|
|
24
24
|
length: DecodedCompactInt
|
|
25
|
-
value:
|
|
25
|
+
value: Uint8Array
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
export class ByteStringCodec implements Codec<ByteString> {
|
|
@@ -36,19 +36,19 @@ export class ByteStringCodec implements Codec<ByteString> {
|
|
|
36
36
|
}
|
|
37
37
|
})
|
|
38
38
|
|
|
39
|
-
encode(input: ByteString):
|
|
40
|
-
return
|
|
39
|
+
encode(input: ByteString): Uint8Array {
|
|
40
|
+
return concatBytes([compactUnsignedIntCodec.encode(input.length), input.value])
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
decode(input:
|
|
43
|
+
decode(input: Uint8Array): ByteString {
|
|
44
44
|
return this.parser.parse(input)
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
return
|
|
47
|
+
encodeBytes(input: Uint8Array): Uint8Array {
|
|
48
|
+
return concatBytes([compactUnsignedIntCodec.encodeU32(input.length), input])
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
decodeBytes(input: Uint8Array): Uint8Array {
|
|
52
52
|
return this.decode(input).value
|
|
53
53
|
}
|
|
54
54
|
}
|
package/src/codec/codec.ts
CHANGED
|
@@ -15,13 +15,12 @@ GNU Lesser General Public License for more details.
|
|
|
15
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
|
|
21
20
|
export interface Codec<T> {
|
|
22
21
|
parser: Parser
|
|
23
|
-
encode(input: T):
|
|
24
|
-
decode(input:
|
|
22
|
+
encode(input: T): Uint8Array
|
|
23
|
+
decode(input: Uint8Array): T
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
export function assert(value: boolean, message: string) {
|
|
@@ -15,11 +15,10 @@ GNU Lesser General Public License for more details.
|
|
|
15
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { Codec, assert } from './codec'
|
|
21
20
|
import { BigIntCodec } from './bigint-codec'
|
|
22
|
-
import {
|
|
21
|
+
import { binToHex } from '../utils'
|
|
23
22
|
|
|
24
23
|
export class CompactInt {
|
|
25
24
|
static readonly oneBytePrefix = 0x00
|
|
@@ -66,24 +65,24 @@ export class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
66
65
|
|
|
67
66
|
parser = compactIntParser
|
|
68
67
|
|
|
69
|
-
encode(input: DecodedCompactInt):
|
|
70
|
-
return
|
|
68
|
+
encode(input: DecodedCompactInt): Uint8Array {
|
|
69
|
+
return new Uint8Array([input.mode, ...input.rest])
|
|
71
70
|
}
|
|
72
71
|
|
|
73
|
-
encodeU32(value: number):
|
|
72
|
+
encodeU32(value: number): Uint8Array {
|
|
74
73
|
if (value < this.oneByteBound) {
|
|
75
|
-
return
|
|
74
|
+
return new Uint8Array([(CompactInt.oneBytePrefix + value) & 0xff])
|
|
76
75
|
} else if (value < this.twoByteBound) {
|
|
77
|
-
return
|
|
76
|
+
return new Uint8Array([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff])
|
|
78
77
|
} else if (value < this.fourByteBound) {
|
|
79
|
-
return
|
|
78
|
+
return new Uint8Array([
|
|
80
79
|
(CompactInt.fourBytePrefix + (value >> 24)) & 0xff,
|
|
81
80
|
(value >> 16) & 0xff,
|
|
82
81
|
(value >> 8) & 0xff,
|
|
83
82
|
value & 0xff
|
|
84
83
|
])
|
|
85
84
|
} else {
|
|
86
|
-
return
|
|
85
|
+
return new Uint8Array([
|
|
87
86
|
CompactInt.multiBytePrefix,
|
|
88
87
|
(value >> 24) & 0xff,
|
|
89
88
|
(value >> 16) & 0xff,
|
|
@@ -93,7 +92,7 @@ export class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
93
92
|
}
|
|
94
93
|
}
|
|
95
94
|
|
|
96
|
-
encodeU256(value: bigint):
|
|
95
|
+
encodeU256(value: bigint): Uint8Array {
|
|
97
96
|
assert(value >= 0n, 'Value should be positive')
|
|
98
97
|
|
|
99
98
|
if (value < this.fourByteBound) {
|
|
@@ -107,26 +106,26 @@ export class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
107
106
|
assert(bytes.length <= 32, 'Expect <= 32 bytes for U256')
|
|
108
107
|
|
|
109
108
|
const header = (bytes.length - 4 + CompactInt.multiBytePrefix) & 0xff
|
|
110
|
-
return
|
|
109
|
+
return new Uint8Array([header, ...bytes])
|
|
111
110
|
}
|
|
112
111
|
}
|
|
113
112
|
|
|
114
|
-
decodeU32(input:
|
|
113
|
+
decodeU32(input: Uint8Array): number {
|
|
115
114
|
const decoded = this.decode(input)
|
|
116
115
|
return this.toU32(decoded)
|
|
117
116
|
}
|
|
118
117
|
|
|
119
|
-
decodeU256(input:
|
|
118
|
+
decodeU256(input: Uint8Array): bigint {
|
|
120
119
|
const decoded = this.decode(input)
|
|
121
120
|
return this.toU256(decoded)
|
|
122
121
|
}
|
|
123
122
|
|
|
124
|
-
decode(input:
|
|
123
|
+
decode(input: Uint8Array): DecodedCompactInt {
|
|
125
124
|
return this.parser.parse(input)
|
|
126
125
|
}
|
|
127
126
|
|
|
128
127
|
toU32(value: DecodedCompactInt): number {
|
|
129
|
-
const body =
|
|
128
|
+
const body = new Uint8Array([value.mode, ...value.rest])
|
|
130
129
|
return decodePositiveInt(value.mode, body)
|
|
131
130
|
}
|
|
132
131
|
|
|
@@ -140,7 +139,7 @@ export class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
140
139
|
return BigInt(this.toU32(value))
|
|
141
140
|
} else {
|
|
142
141
|
assert(value.rest.length <= 32, 'Expect <= 32 bytes for U256')
|
|
143
|
-
return BigIntCodec.decode(
|
|
142
|
+
return BigIntCodec.decode(value.rest, false)
|
|
144
143
|
}
|
|
145
144
|
}
|
|
146
145
|
|
|
@@ -159,34 +158,34 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
159
158
|
|
|
160
159
|
parser = compactIntParser
|
|
161
160
|
|
|
162
|
-
encode(input: DecodedCompactInt):
|
|
163
|
-
return
|
|
161
|
+
encode(input: DecodedCompactInt): Uint8Array {
|
|
162
|
+
return new Uint8Array([input.mode, ...input.rest])
|
|
164
163
|
}
|
|
165
164
|
|
|
166
|
-
decode(input:
|
|
165
|
+
decode(input: Uint8Array): DecodedCompactInt {
|
|
167
166
|
return this.parser.parse(input)
|
|
168
167
|
}
|
|
169
168
|
|
|
170
|
-
decodeI32(input:
|
|
169
|
+
decodeI32(input: Uint8Array): number {
|
|
171
170
|
const decoded = this.decode(input)
|
|
172
171
|
return this.toI32(decoded)
|
|
173
172
|
}
|
|
174
173
|
|
|
175
|
-
encodeI32(value: number):
|
|
174
|
+
encodeI32(value: number): Uint8Array {
|
|
176
175
|
if (value >= 0) {
|
|
177
176
|
if (value < this.oneByteBound) {
|
|
178
|
-
return
|
|
177
|
+
return new Uint8Array([(CompactInt.oneBytePrefix + value) & 0xff])
|
|
179
178
|
} else if (value < this.twoByteBound) {
|
|
180
|
-
return
|
|
179
|
+
return new Uint8Array([(CompactInt.twoBytePrefix + (value >> 8)) & 0xff, value & 0xff])
|
|
181
180
|
} else if (value < this.fourByteBound) {
|
|
182
|
-
return
|
|
181
|
+
return new Uint8Array([
|
|
183
182
|
(CompactInt.fourBytePrefix + (value >> 24)) & 0xff,
|
|
184
183
|
(value >> 16) & 0xff,
|
|
185
184
|
(value >> 8) & 0xff,
|
|
186
185
|
value & 0xff
|
|
187
186
|
])
|
|
188
187
|
} else {
|
|
189
|
-
return
|
|
188
|
+
return new Uint8Array([
|
|
190
189
|
CompactInt.multiBytePrefix,
|
|
191
190
|
(value >> 24) & 0xff,
|
|
192
191
|
(value >> 16) & 0xff,
|
|
@@ -196,18 +195,18 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
196
195
|
}
|
|
197
196
|
} else {
|
|
198
197
|
if (value >= -this.oneByteBound) {
|
|
199
|
-
return
|
|
198
|
+
return new Uint8Array([(value ^ CompactInt.oneByteNegPrefix) & 0xff])
|
|
200
199
|
} else if (value >= -this.twoByteBound) {
|
|
201
|
-
return
|
|
200
|
+
return new Uint8Array([((value >> 8) ^ CompactInt.twoByteNegPrefix) & 0xff, value & 0xff])
|
|
202
201
|
} else if (value >= -this.fourByteBound) {
|
|
203
|
-
return
|
|
202
|
+
return new Uint8Array([
|
|
204
203
|
((value >> 24) ^ CompactInt.fourByteNegPrefix) & 0xff,
|
|
205
204
|
(value >> 16) & 0xff,
|
|
206
205
|
(value >> 8) & 0xff,
|
|
207
206
|
value & 0xff
|
|
208
207
|
])
|
|
209
208
|
} else {
|
|
210
|
-
return
|
|
209
|
+
return new Uint8Array([
|
|
211
210
|
CompactInt.multiBytePrefix,
|
|
212
211
|
(value >> 24) & 0xff,
|
|
213
212
|
(value >> 16) & 0xff,
|
|
@@ -218,23 +217,23 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
218
217
|
}
|
|
219
218
|
}
|
|
220
219
|
|
|
221
|
-
encodeI256(value: bigint):
|
|
220
|
+
encodeI256(value: bigint): Uint8Array {
|
|
222
221
|
if (value >= -0x20000000 && value < 0x20000000) {
|
|
223
222
|
return this.encodeI32(Number(value))
|
|
224
223
|
} else {
|
|
225
224
|
const bytes = BigIntCodec.encode(value)
|
|
226
225
|
const header = (bytes.length - 4 + CompactInt.multiBytePrefix) & 0xff
|
|
227
|
-
return
|
|
226
|
+
return new Uint8Array([header, ...bytes])
|
|
228
227
|
}
|
|
229
228
|
}
|
|
230
229
|
|
|
231
|
-
decodeI256(input:
|
|
230
|
+
decodeI256(input: Uint8Array): bigint {
|
|
232
231
|
const decoded = this.decode(input)
|
|
233
232
|
return this.toI256(decoded)
|
|
234
233
|
}
|
|
235
234
|
|
|
236
235
|
toI32(value: DecodedCompactInt): number {
|
|
237
|
-
const body =
|
|
236
|
+
const body = new Uint8Array([value.mode, ...value.rest])
|
|
238
237
|
const mode = value.mode & maskRest
|
|
239
238
|
if (fixedSize(mode)) {
|
|
240
239
|
const isPositive = (value.mode & signFlag) == 0
|
|
@@ -263,7 +262,7 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
263
262
|
return BigInt(this.toI32(value))
|
|
264
263
|
} else {
|
|
265
264
|
assert(value.rest.length <= 32, 'Expect <= 32 bytes for I256')
|
|
266
|
-
return BigIntCodec.decode(
|
|
265
|
+
return BigIntCodec.decode(value.rest, true)
|
|
267
266
|
}
|
|
268
267
|
}
|
|
269
268
|
|
|
@@ -274,7 +273,7 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
|
|
|
274
273
|
|
|
275
274
|
export const compactSignedIntCodec = new CompactSignedIntCodec()
|
|
276
275
|
|
|
277
|
-
function decodePositiveInt(rawMode: number, body:
|
|
276
|
+
function decodePositiveInt(rawMode: number, body: Uint8Array): number {
|
|
278
277
|
const mode = rawMode & maskRest
|
|
279
278
|
|
|
280
279
|
switch (mode) {
|
|
@@ -288,14 +287,14 @@ function decodePositiveInt(rawMode: number, body: Buffer): number {
|
|
|
288
287
|
return ((body[0] & maskMode) << 24) | ((body[1] & 0xff) << 16) | ((body[2] & 0xff) << 8) | (body[3] & 0xff)
|
|
289
288
|
default:
|
|
290
289
|
if (body.length === 5) {
|
|
291
|
-
return Number(BigInt('0x' + body.slice(1)
|
|
290
|
+
return Number(BigInt('0x' + binToHex(body.slice(1))))
|
|
292
291
|
} else {
|
|
293
292
|
throw new Error(`decodePositiveInt: Expect 4 bytes int, but get ${body.length - 1} bytes int`)
|
|
294
293
|
}
|
|
295
294
|
}
|
|
296
295
|
}
|
|
297
296
|
|
|
298
|
-
function decodeNegativeInt(rawMode: number, body:
|
|
297
|
+
function decodeNegativeInt(rawMode: number, body: Uint8Array) {
|
|
299
298
|
const mode = rawMode & maskRest
|
|
300
299
|
switch (mode) {
|
|
301
300
|
case CompactInt.oneBytePrefix:
|
|
@@ -16,19 +16,19 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
import { Buffer } from 'buffer/'
|
|
20
19
|
import { Parser } from 'binary-parser'
|
|
21
20
|
import { ArrayCodec, DecodedArray } from './array-codec'
|
|
22
21
|
import { Codec } from './codec'
|
|
23
22
|
import { compactSignedIntCodec, compactUnsignedIntCodec, DecodedCompactInt } from './compact-int-codec'
|
|
24
23
|
import { Method, MethodCodec, methodCodec } from './method-codec'
|
|
24
|
+
import { concatBytes } from '../utils'
|
|
25
25
|
|
|
26
26
|
const compactSignedIntsCodec = new ArrayCodec(compactSignedIntCodec)
|
|
27
27
|
|
|
28
28
|
export interface HalfDecodedContract {
|
|
29
29
|
fieldLength: DecodedCompactInt
|
|
30
30
|
methodIndexes: DecodedArray<DecodedCompactInt>
|
|
31
|
-
methods:
|
|
31
|
+
methods: Uint8Array
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
export interface Contract {
|
|
@@ -36,24 +36,6 @@ export interface Contract {
|
|
|
36
36
|
methods: Method[]
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
export function toHalfDecoded(contract: Contract): HalfDecodedContract {
|
|
40
|
-
const fieldLength = compactSignedIntCodec.fromI32(contract.fieldLength)
|
|
41
|
-
const methods = contract.methods.map((m) => methodCodec.encode(MethodCodec.fromMethod(m)))
|
|
42
|
-
let count = 0
|
|
43
|
-
const methodIndexes = Array.from(Array(methods.length).keys()).map((index) => {
|
|
44
|
-
count += methods[`${index}`].length
|
|
45
|
-
return count
|
|
46
|
-
})
|
|
47
|
-
return {
|
|
48
|
-
fieldLength,
|
|
49
|
-
methodIndexes: {
|
|
50
|
-
length: compactSignedIntCodec.fromI32(methodIndexes.length),
|
|
51
|
-
value: methodIndexes.map((value) => compactSignedIntCodec.fromI32(value))
|
|
52
|
-
},
|
|
53
|
-
methods: methods.reduce((acc, buffer) => Buffer.concat([acc, buffer]))
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
39
|
export class ContractCodec implements Codec<HalfDecodedContract> {
|
|
58
40
|
parser = Parser.start()
|
|
59
41
|
.nest('fieldLength', {
|
|
@@ -64,19 +46,19 @@ export class ContractCodec implements Codec<HalfDecodedContract> {
|
|
|
64
46
|
})
|
|
65
47
|
.buffer('methods', { readUntil: 'eof' })
|
|
66
48
|
|
|
67
|
-
encode(input: HalfDecodedContract):
|
|
68
|
-
return
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
49
|
+
encode(input: HalfDecodedContract): Uint8Array {
|
|
50
|
+
return concatBytes([
|
|
51
|
+
compactSignedIntCodec.encode(input.fieldLength),
|
|
52
|
+
compactSignedIntsCodec.encode(input.methodIndexes.value),
|
|
53
|
+
input.methods
|
|
72
54
|
])
|
|
73
55
|
}
|
|
74
56
|
|
|
75
|
-
decode(input:
|
|
57
|
+
decode(input: Uint8Array): HalfDecodedContract {
|
|
76
58
|
return this.parser.parse(input)
|
|
77
59
|
}
|
|
78
60
|
|
|
79
|
-
decodeContract(input:
|
|
61
|
+
decodeContract(input: Uint8Array): Contract {
|
|
80
62
|
const halfDecoded = this.decode(input)
|
|
81
63
|
const fieldLength = compactUnsignedIntCodec.toU32(halfDecoded.fieldLength)
|
|
82
64
|
const methodIndexes = halfDecoded.methodIndexes.value.map((v) => compactUnsignedIntCodec.toU32(v))
|
|
@@ -90,6 +72,25 @@ export class ContractCodec implements Codec<HalfDecodedContract> {
|
|
|
90
72
|
|
|
91
73
|
return { fieldLength, methods }
|
|
92
74
|
}
|
|
75
|
+
|
|
76
|
+
encodeContract(contract: Contract): Uint8Array {
|
|
77
|
+
const fieldLength = compactSignedIntCodec.fromI32(contract.fieldLength)
|
|
78
|
+
const methods = contract.methods.map((m) => methodCodec.encode(MethodCodec.fromMethod(m)))
|
|
79
|
+
let count = 0
|
|
80
|
+
const methodIndexes = Array.from(Array(methods.length).keys()).map((index) => {
|
|
81
|
+
count += methods[`${index}`].length
|
|
82
|
+
return count
|
|
83
|
+
})
|
|
84
|
+
const halfDecoded = {
|
|
85
|
+
fieldLength,
|
|
86
|
+
methodIndexes: {
|
|
87
|
+
length: compactSignedIntCodec.fromI32(methodIndexes.length),
|
|
88
|
+
value: methodIndexes.map((value) => compactSignedIntCodec.fromI32(value))
|
|
89
|
+
},
|
|
90
|
+
methods: concatBytes(methods)
|
|
91
|
+
}
|
|
92
|
+
return this.encode(halfDecoded)
|
|
93
|
+
}
|
|
93
94
|
}
|
|
94
95
|
|
|
95
96
|
export const contractCodec = new ContractCodec()
|
|
@@ -15,7 +15,6 @@ GNU Lesser General Public License for more details.
|
|
|
15
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { DecodedArray } from './array-codec'
|
|
21
20
|
import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
|
|
@@ -24,7 +23,7 @@ import { Codec } from './codec'
|
|
|
24
23
|
import { Token, tokensCodec } from './token-codec'
|
|
25
24
|
import { ContractOutput as ApiContractOutput } from '../api/api-alephium'
|
|
26
25
|
import { blakeHash, createHint } from './hash'
|
|
27
|
-
import { binToHex, bs58 } from '../utils'
|
|
26
|
+
import { binToHex, bs58, concatBytes, hexToBinUnsafe } from '../utils'
|
|
28
27
|
import { signedIntCodec } from './signed-int-codec'
|
|
29
28
|
import { lockupScriptCodec } from './lockup-script-codec'
|
|
30
29
|
|
|
@@ -46,26 +45,26 @@ export class ContractOutputCodec implements Codec<ContractOutput> {
|
|
|
46
45
|
type: tokensCodec.parser
|
|
47
46
|
})
|
|
48
47
|
|
|
49
|
-
encode(input: ContractOutput):
|
|
50
|
-
const amount =
|
|
48
|
+
encode(input: ContractOutput): Uint8Array {
|
|
49
|
+
const amount = compactUnsignedIntCodec.encode(input.amount)
|
|
51
50
|
const lockupScript = input.lockupScript.contractId
|
|
52
|
-
const tokens =
|
|
51
|
+
const tokens = tokensCodec.encode(input.tokens.value)
|
|
53
52
|
|
|
54
|
-
return
|
|
53
|
+
return concatBytes([amount, lockupScript, tokens])
|
|
55
54
|
}
|
|
56
55
|
|
|
57
|
-
decode(input:
|
|
56
|
+
decode(input: Uint8Array): ContractOutput {
|
|
58
57
|
return this.parser.parse(input)
|
|
59
58
|
}
|
|
60
59
|
|
|
61
60
|
static convertToApiContractOutput(txIdBytes: Uint8Array, output: ContractOutput, index: number): ApiContractOutput {
|
|
62
61
|
const hint = createHint(output.lockupScript.contractId)
|
|
63
|
-
const key = binToHex(blakeHash(
|
|
62
|
+
const key = binToHex(blakeHash(concatBytes([txIdBytes, signedIntCodec.encode(index)])))
|
|
64
63
|
const attoAlphAmount = compactUnsignedIntCodec.toU256(output.amount).toString()
|
|
65
|
-
const address = bs58.encode(
|
|
64
|
+
const address = bs58.encode(new Uint8Array([0x03, ...output.lockupScript.contractId]))
|
|
66
65
|
const tokens = output.tokens.value.map((token) => {
|
|
67
66
|
return {
|
|
68
|
-
id: token.tokenId
|
|
67
|
+
id: binToHex(token.tokenId),
|
|
69
68
|
amount: compactUnsignedIntCodec.toU256(token.amount).toString()
|
|
70
69
|
}
|
|
71
70
|
})
|
|
@@ -74,12 +73,11 @@ export class ContractOutputCodec implements Codec<ContractOutput> {
|
|
|
74
73
|
|
|
75
74
|
static convertToOutput(apiContractOutput: ApiContractOutput): ContractOutput {
|
|
76
75
|
const amount: DecodedCompactInt = compactUnsignedIntCodec.fromU256(BigInt(apiContractOutput.attoAlphAmount))
|
|
77
|
-
const lockupScript: P2C = lockupScriptCodec.decode(
|
|
78
|
-
.script as P2C
|
|
76
|
+
const lockupScript: P2C = lockupScriptCodec.decode(bs58.decode(apiContractOutput.address)).script as P2C
|
|
79
77
|
|
|
80
78
|
const tokensValue = apiContractOutput.tokens.map((token) => {
|
|
81
79
|
return {
|
|
82
|
-
tokenId:
|
|
80
|
+
tokenId: hexToBinUnsafe(token.id),
|
|
83
81
|
amount: compactUnsignedIntCodec.fromU256(BigInt(token.amount))
|
|
84
82
|
}
|
|
85
83
|
})
|
|
@@ -15,25 +15,25 @@ GNU Lesser General Public License for more details.
|
|
|
15
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { ArrayCodec } from './array-codec'
|
|
21
20
|
import { Codec } from './codec'
|
|
22
21
|
import { signedIntCodec } from './signed-int-codec'
|
|
22
|
+
import { concatBytes } from '../utils'
|
|
23
23
|
|
|
24
24
|
export interface ContractOutputRef {
|
|
25
25
|
hint: number
|
|
26
|
-
key:
|
|
26
|
+
key: Uint8Array
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
export class ContractOutputRefCodec implements Codec<ContractOutputRef> {
|
|
30
30
|
parser = Parser.start().int32('hint').buffer('key', { length: 32 })
|
|
31
31
|
|
|
32
|
-
encode(input: ContractOutputRef):
|
|
33
|
-
return
|
|
32
|
+
encode(input: ContractOutputRef): Uint8Array {
|
|
33
|
+
return concatBytes([signedIntCodec.encode(input.hint), input.key])
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
decode(input:
|
|
36
|
+
decode(input: Uint8Array): ContractOutputRef {
|
|
37
37
|
return this.parser.parse(input)
|
|
38
38
|
}
|
|
39
39
|
}
|
|
@@ -15,7 +15,6 @@ GNU Lesser General Public License for more details.
|
|
|
15
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import { Parser } from 'binary-parser'
|
|
20
19
|
import { Codec } from './codec'
|
|
21
20
|
|
|
@@ -39,17 +38,17 @@ export class EitherCodec<L, R> implements Codec<Either<L, R>> {
|
|
|
39
38
|
})
|
|
40
39
|
) {}
|
|
41
40
|
|
|
42
|
-
encode(input: Either<L, R>):
|
|
41
|
+
encode(input: Either<L, R>): Uint8Array {
|
|
43
42
|
const result = [input.either]
|
|
44
43
|
if (input.either === 0) {
|
|
45
44
|
result.push(...this.leftCodec.encode(input.value as L))
|
|
46
45
|
} else {
|
|
47
46
|
result.push(...this.rightCodec.encode(input.value as R))
|
|
48
47
|
}
|
|
49
|
-
return
|
|
48
|
+
return new Uint8Array(result)
|
|
50
49
|
}
|
|
51
50
|
|
|
52
|
-
decode(input:
|
|
51
|
+
decode(input: Uint8Array): Either<L, R> {
|
|
53
52
|
const result = this.parser.parse(input)
|
|
54
53
|
return {
|
|
55
54
|
...result,
|
package/src/codec/hash.ts
CHANGED
|
@@ -15,14 +15,13 @@ GNU Lesser General Public License for more details.
|
|
|
15
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
|
-
import { Buffer } from 'buffer/'
|
|
19
18
|
import blake from 'blakejs'
|
|
20
19
|
|
|
21
20
|
export function blakeHash(raw: Uint8Array) {
|
|
22
21
|
return blake.blake2b(raw, undefined, 32)
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
export function djbIntHash(bytes:
|
|
24
|
+
export function djbIntHash(bytes: Uint8Array): number {
|
|
26
25
|
let hash = 5381
|
|
27
26
|
bytes.forEach((byte) => {
|
|
28
27
|
hash = (hash << 5) + hash + (byte & 0xff)
|
|
@@ -30,6 +29,6 @@ export function djbIntHash(bytes: Buffer): number {
|
|
|
30
29
|
return hash
|
|
31
30
|
}
|
|
32
31
|
|
|
33
|
-
export function createHint(input:
|
|
32
|
+
export function createHint(input: Uint8Array): number {
|
|
34
33
|
return djbIntHash(input) | 1
|
|
35
34
|
}
|