@alephium/web3 0.29.3 → 0.30.0-beta.1
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/api/api-alephium.d.ts +1 -1
- package/dist/src/api/api-alephium.js +1 -1
- package/dist/src/codec/array-codec.d.ts +17 -0
- package/dist/src/codec/array-codec.js +58 -0
- package/dist/src/codec/asset-output-codec.d.ts +27 -0
- package/dist/src/codec/asset-output-codec.js +135 -0
- package/dist/src/codec/bigint-codec.d.ts +5 -0
- package/dist/src/codec/bigint-codec.js +85 -0
- package/dist/src/codec/bytestring-codec.d.ts +16 -0
- package/dist/src/codec/bytestring-codec.js +49 -0
- package/dist/src/codec/codec.d.ts +8 -0
- package/dist/src/codec/codec.js +9 -0
- package/dist/src/codec/compact-int-codec.d.ts +47 -0
- package/dist/src/codec/compact-int-codec.js +287 -0
- package/dist/src/codec/contract-codec.d.ts +22 -0
- package/dist/src/codec/contract-codec.js +62 -0
- package/dist/src/codec/contract-output-codec.d.ts +21 -0
- package/dist/src/codec/contract-output-codec.js +81 -0
- package/dist/src/codec/contract-output-ref-codec.d.ts +15 -0
- package/dist/src/codec/contract-output-ref-codec.js +37 -0
- package/dist/src/codec/either-codec.d.ts +17 -0
- package/dist/src/codec/either-codec.js +66 -0
- package/dist/src/codec/hash.d.ts +4 -0
- package/dist/src/codec/hash.js +40 -0
- package/dist/src/codec/index.d.ts +23 -0
- package/dist/src/codec/index.js +69 -0
- package/dist/src/codec/input-codec.d.ts +22 -0
- package/dist/src/codec/input-codec.js +70 -0
- package/dist/src/codec/instr-codec.d.ts +223 -0
- package/dist/src/codec/instr-codec.js +459 -0
- package/dist/src/codec/lockup-script-codec.d.ts +28 -0
- package/dist/src/codec/lockup-script-codec.js +79 -0
- package/dist/src/codec/long-codec.d.ts +9 -0
- package/dist/src/codec/long-codec.js +55 -0
- package/dist/src/codec/method-codec.d.ts +30 -0
- package/dist/src/codec/method-codec.js +67 -0
- package/dist/src/codec/option-codec.d.ts +15 -0
- package/dist/src/codec/option-codec.js +54 -0
- package/dist/src/codec/output-codec.d.ts +7 -0
- package/dist/src/codec/output-codec.js +26 -0
- package/dist/src/codec/script-codec.d.ts +16 -0
- package/dist/src/codec/script-codec.js +40 -0
- package/dist/src/codec/signature-codec.d.ts +14 -0
- package/dist/src/codec/signature-codec.js +36 -0
- package/dist/src/codec/signed-int-codec.d.ts +9 -0
- package/dist/src/codec/signed-int-codec.js +38 -0
- package/dist/src/codec/token-codec.d.ts +16 -0
- package/dist/src/codec/token-codec.js +45 -0
- package/dist/src/codec/transaction-codec.d.ts +27 -0
- package/dist/src/codec/transaction-codec.js +127 -0
- package/dist/src/codec/unlock-script-codec.d.ts +40 -0
- package/dist/src/codec/unlock-script-codec.js +169 -0
- package/dist/src/codec/unsigned-tx-codec.d.ts +30 -0
- package/dist/src/codec/unsigned-tx-codec.js +102 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +2 -1
- package/package.json +4 -3
- package/src/api/api-alephium.ts +1 -1
- package/src/codec/array-codec.ts +62 -0
- package/src/codec/asset-output-codec.ts +150 -0
- package/src/codec/bigint-codec.ts +91 -0
- package/src/codec/bytestring-codec.ts +55 -0
- package/src/codec/codec.ts +30 -0
- package/src/codec/compact-int-codec.ts +299 -0
- package/src/codec/contract-codec.ts +76 -0
- package/src/codec/contract-output-codec.ts +97 -0
- package/src/codec/contract-output-ref-codec.ts +41 -0
- package/src/codec/either-codec.ts +73 -0
- package/src/codec/hash.ts +34 -0
- package/src/codec/index.ts +41 -0
- package/src/codec/input-codec.ts +80 -0
- package/src/codec/instr-codec.ts +462 -0
- package/src/codec/lockup-script-codec.ts +98 -0
- package/src/codec/long-codec.ts +58 -0
- package/src/codec/method-codec.ts +85 -0
- package/src/codec/option-codec.ts +59 -0
- package/src/codec/output-codec.ts +26 -0
- package/src/codec/script-codec.ts +44 -0
- package/src/codec/signature-codec.ts +39 -0
- package/src/codec/signed-int-codec.ts +36 -0
- package/src/codec/token-codec.ts +49 -0
- package/src/codec/transaction-codec.ts +146 -0
- package/src/codec/unlock-script-codec.ts +193 -0
- package/src/codec/unsigned-tx-codec.ts +123 -0
- package/src/index.ts +1 -1
|
@@ -0,0 +1,193 @@
|
|
|
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
|
+
import { Parser } from 'binary-parser'
|
|
19
|
+
import { ArrayCodec, DecodedArray } from './array-codec'
|
|
20
|
+
import { compactUnsignedIntCodec, compactSignedIntCodec, DecodedCompactInt } from './compact-int-codec'
|
|
21
|
+
import { Codec } from './codec'
|
|
22
|
+
import { Script, scriptCodec } from './script-codec'
|
|
23
|
+
import { ByteString, byteStringCodec } from './bytestring-codec'
|
|
24
|
+
import { LockupScript, lockupScriptCodec } from './lockup-script-codec'
|
|
25
|
+
|
|
26
|
+
export interface P2PKH {
|
|
27
|
+
publicKey: Buffer
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
class P2PKHCodec implements Codec<P2PKH> {
|
|
31
|
+
parser = Parser.start().buffer('publicKey', { length: 33 })
|
|
32
|
+
|
|
33
|
+
encode(input: P2PKH): Buffer {
|
|
34
|
+
return input.publicKey
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
decode(input: Buffer): P2PKH {
|
|
38
|
+
return this.parser.parse(input)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const p2pkhCodec = new P2PKHCodec()
|
|
42
|
+
|
|
43
|
+
export interface P2MPKH {
|
|
44
|
+
publicKeys: DecodedArray<{
|
|
45
|
+
publicKey: P2PKH
|
|
46
|
+
index: DecodedCompactInt
|
|
47
|
+
}>
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class P2MPKHCodec implements Codec<P2MPKH> {
|
|
51
|
+
parser = Parser.start().nest('publicKeys', {
|
|
52
|
+
type: ArrayCodec.arrayParser(
|
|
53
|
+
Parser.start()
|
|
54
|
+
.nest('publicKey', { type: p2pkhCodec.parser })
|
|
55
|
+
.nest('index', { type: compactUnsignedIntCodec.parser })
|
|
56
|
+
)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
encode(input: P2MPKH): Buffer {
|
|
60
|
+
return Buffer.concat([
|
|
61
|
+
Buffer.from(compactUnsignedIntCodec.encode(input.publicKeys.length)),
|
|
62
|
+
...input.publicKeys.value.map((v) => {
|
|
63
|
+
return Buffer.concat([v.publicKey.publicKey, Buffer.from(compactUnsignedIntCodec.encode(v.index))])
|
|
64
|
+
})
|
|
65
|
+
])
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
decode(input: Buffer): any {
|
|
69
|
+
return this.parser.parse(input)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const p2mpkhCodec = new P2MPKHCodec()
|
|
73
|
+
|
|
74
|
+
export interface Val {
|
|
75
|
+
type: number
|
|
76
|
+
val: number | DecodedCompactInt | ByteString | LockupScript
|
|
77
|
+
}
|
|
78
|
+
class ValCodec implements Codec<Val> {
|
|
79
|
+
parser = Parser.start()
|
|
80
|
+
.int8('type')
|
|
81
|
+
.choice('val', {
|
|
82
|
+
tag: 'type',
|
|
83
|
+
choices: {
|
|
84
|
+
0x00: new Parser().uint8('value'), // Boolean
|
|
85
|
+
0x01: compactSignedIntCodec.parser, // I256
|
|
86
|
+
0x02: compactUnsignedIntCodec.parser, // U256
|
|
87
|
+
0x03: byteStringCodec.parser, // ByteVec
|
|
88
|
+
0x04: lockupScriptCodec.parser // Address
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
encode(input: Val): Buffer {
|
|
93
|
+
const valType = input.type
|
|
94
|
+
|
|
95
|
+
if (valType === 0x00) {
|
|
96
|
+
// Boolean
|
|
97
|
+
return Buffer.from([valType, input.val as number])
|
|
98
|
+
} else if (valType === 0x01) {
|
|
99
|
+
// I256
|
|
100
|
+
return Buffer.from([valType, ...compactUnsignedIntCodec.encode(input.val as DecodedCompactInt)])
|
|
101
|
+
} else if (valType === 0x02) {
|
|
102
|
+
// U256
|
|
103
|
+
return Buffer.from([valType, ...compactUnsignedIntCodec.encode(input.val as DecodedCompactInt)])
|
|
104
|
+
} else if (valType === 0x03) {
|
|
105
|
+
// ByteVec
|
|
106
|
+
return Buffer.from([valType, ...byteStringCodec.encode(input.val as ByteString)])
|
|
107
|
+
} else if (valType === 0x04) {
|
|
108
|
+
// Address
|
|
109
|
+
return Buffer.from([valType, ...lockupScriptCodec.encode(input.val as LockupScript)])
|
|
110
|
+
} else {
|
|
111
|
+
throw new Error(`ValCodec: unsupported val type: ${valType}`)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
decode(input: Buffer): Val {
|
|
116
|
+
return this.parser.parse(input)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const valCodec = new ValCodec()
|
|
121
|
+
const valsCodec = new ArrayCodec(valCodec)
|
|
122
|
+
|
|
123
|
+
export interface P2SH {
|
|
124
|
+
script: Script
|
|
125
|
+
params: DecodedArray<Val>
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export class P2SHCodec implements Codec<P2SH> {
|
|
129
|
+
parser = Parser.start()
|
|
130
|
+
.nest('script', {
|
|
131
|
+
type: scriptCodec.parser
|
|
132
|
+
})
|
|
133
|
+
.nest('params', {
|
|
134
|
+
type: valsCodec.parser
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
encode(input: P2SH): Buffer {
|
|
138
|
+
return Buffer.concat([scriptCodec.encode(input.script), valsCodec.encode(input.params.value)])
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
decode(input: Buffer): P2SH {
|
|
142
|
+
return this.parser.parse(input)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const p2shCodec = new P2SHCodec()
|
|
147
|
+
|
|
148
|
+
export interface UnlockScript {
|
|
149
|
+
scriptType: number
|
|
150
|
+
script: P2PKH | P2MPKH | P2SH
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export class UnlockScriptCodec implements Codec<UnlockScript> {
|
|
154
|
+
parser = Parser.start()
|
|
155
|
+
.uint8('scriptType')
|
|
156
|
+
.choice('script', {
|
|
157
|
+
tag: 'scriptType',
|
|
158
|
+
choices: {
|
|
159
|
+
0: p2pkhCodec.parser,
|
|
160
|
+
1: p2mpkhCodec.parser,
|
|
161
|
+
2: p2shCodec.parser,
|
|
162
|
+
3: Parser.start() // TODO: SameAsPrevious, FIXME
|
|
163
|
+
}
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
encode(input: UnlockScript): Buffer {
|
|
167
|
+
const scriptType = input.scriptType
|
|
168
|
+
const inputUnLockScript = input.script
|
|
169
|
+
const inputUnLockScriptType = Buffer.from([scriptType])
|
|
170
|
+
|
|
171
|
+
if (scriptType === 0) {
|
|
172
|
+
// P2PKH
|
|
173
|
+
return Buffer.concat([inputUnLockScriptType, p2pkhCodec.encode(inputUnLockScript as P2PKH)])
|
|
174
|
+
} else if (scriptType === 1) {
|
|
175
|
+
// P2MPKH
|
|
176
|
+
return Buffer.concat([inputUnLockScriptType, p2mpkhCodec.encode(inputUnLockScript as P2MPKH)])
|
|
177
|
+
} else if (scriptType === 2) {
|
|
178
|
+
// P2SH
|
|
179
|
+
return Buffer.concat([inputUnLockScriptType, p2shCodec.encode(input.script as P2SH)])
|
|
180
|
+
} else if (scriptType === 3) {
|
|
181
|
+
// SameAsPrevious
|
|
182
|
+
return inputUnLockScriptType
|
|
183
|
+
} else {
|
|
184
|
+
throw new Error(`TODO: encode unlock script: ${scriptType}`)
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
decode(input: Buffer): UnlockScript {
|
|
189
|
+
return this.parser.parse(input)
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export const unlockScriptCodec = new UnlockScriptCodec()
|
|
@@ -0,0 +1,123 @@
|
|
|
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
|
+
import { Parser } from 'binary-parser'
|
|
19
|
+
import { UnsignedTx as ApiUnsignedTx } from '../api/api-alephium'
|
|
20
|
+
import { binToHex, hexToBinUnsafe } from '../utils'
|
|
21
|
+
import { Script, scriptCodec, statefulScriptCodecOpt } from './script-codec'
|
|
22
|
+
import { Option } from './option-codec'
|
|
23
|
+
import { DecodedCompactInt, compactSignedIntCodec, compactUnsignedIntCodec } from './compact-int-codec'
|
|
24
|
+
import { Input, InputCodec, inputsCodec } from './input-codec'
|
|
25
|
+
import { AssetOutput, AssetOutputCodec, assetOutputsCodec } from './asset-output-codec'
|
|
26
|
+
import { DecodedArray } from './array-codec'
|
|
27
|
+
import { blakeHash } from './hash'
|
|
28
|
+
import { Codec } from './codec'
|
|
29
|
+
|
|
30
|
+
export interface UnsignedTx {
|
|
31
|
+
version: number
|
|
32
|
+
networkId: number
|
|
33
|
+
statefulScript: Option<Script>
|
|
34
|
+
gasAmount: DecodedCompactInt
|
|
35
|
+
gasPrice: DecodedCompactInt
|
|
36
|
+
inputs: DecodedArray<Input>
|
|
37
|
+
fixedOutputs: DecodedArray<AssetOutput>
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class UnsignedTxCodec implements Codec<UnsignedTx> {
|
|
41
|
+
parser = new Parser()
|
|
42
|
+
.uint8('version')
|
|
43
|
+
.uint8('networkId')
|
|
44
|
+
.nest('statefulScript', {
|
|
45
|
+
type: statefulScriptCodecOpt.parser
|
|
46
|
+
})
|
|
47
|
+
.nest('gasAmount', {
|
|
48
|
+
type: compactSignedIntCodec.parser
|
|
49
|
+
})
|
|
50
|
+
.nest('gasPrice', {
|
|
51
|
+
type: compactUnsignedIntCodec.parser
|
|
52
|
+
})
|
|
53
|
+
.nest('inputs', {
|
|
54
|
+
type: inputsCodec.parser
|
|
55
|
+
})
|
|
56
|
+
.nest('fixedOutputs', {
|
|
57
|
+
type: assetOutputsCodec.parser
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
encode(decodedUnsignedTx: UnsignedTx): Buffer {
|
|
61
|
+
return Buffer.concat([
|
|
62
|
+
Buffer.from([decodedUnsignedTx.version, decodedUnsignedTx.networkId]),
|
|
63
|
+
statefulScriptCodecOpt.encode(decodedUnsignedTx.statefulScript),
|
|
64
|
+
compactSignedIntCodec.encode(decodedUnsignedTx.gasAmount),
|
|
65
|
+
compactUnsignedIntCodec.encode(decodedUnsignedTx.gasPrice),
|
|
66
|
+
inputsCodec.encode(decodedUnsignedTx.inputs.value),
|
|
67
|
+
assetOutputsCodec.encode(decodedUnsignedTx.fixedOutputs.value)
|
|
68
|
+
])
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
decode(input: Buffer): UnsignedTx {
|
|
72
|
+
return this.parser.parse(input)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
encodeApiUnsignedTx(input: ApiUnsignedTx): Buffer {
|
|
76
|
+
const decoded = UnsignedTxCodec.fromApiUnsignedTx(input)
|
|
77
|
+
return this.encode(decoded)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
decodeApiUnsignedTx(input: Buffer): ApiUnsignedTx {
|
|
81
|
+
const decoded = this.parser.parse(input)
|
|
82
|
+
return UnsignedTxCodec.toApiUnsignedTx(decoded)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
static txId(unsignedTx: UnsignedTx): string {
|
|
86
|
+
return binToHex(blakeHash(unsignedTxCodec.encode(unsignedTx)))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
static toApiUnsignedTx(unsigned: UnsignedTx): ApiUnsignedTx {
|
|
90
|
+
const txId = UnsignedTxCodec.txId(unsigned)
|
|
91
|
+
const txIdBytes = hexToBinUnsafe(txId)
|
|
92
|
+
const version = unsigned.version
|
|
93
|
+
const networkId = unsigned.networkId
|
|
94
|
+
const gasAmount = compactSignedIntCodec.toI32(unsigned.gasAmount)
|
|
95
|
+
const gasPrice = compactUnsignedIntCodec.toU256(unsigned.gasPrice).toString()
|
|
96
|
+
const inputs = InputCodec.toAssetInputs(unsigned.inputs.value)
|
|
97
|
+
const fixedOutputs = AssetOutputCodec.toFixedAssetOutputs(txIdBytes, unsigned.fixedOutputs.value)
|
|
98
|
+
let scriptOpt: string | undefined = undefined
|
|
99
|
+
if (unsigned.statefulScript.option === 1) {
|
|
100
|
+
scriptOpt = scriptCodec.encode(unsigned.statefulScript.value!).toString('hex')
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return { txId, version, networkId, gasAmount, scriptOpt, gasPrice, inputs, fixedOutputs }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
static fromApiUnsignedTx(unsignedTx: ApiUnsignedTx): UnsignedTx {
|
|
107
|
+
const version = unsignedTx.version
|
|
108
|
+
const networkId = unsignedTx.networkId
|
|
109
|
+
const gasAmount = compactSignedIntCodec.decode(compactSignedIntCodec.encodeI32(unsignedTx.gasAmount))
|
|
110
|
+
const gasPrice = compactUnsignedIntCodec.decode(compactUnsignedIntCodec.encodeU256(BigInt(unsignedTx.gasPrice)))
|
|
111
|
+
const inputsValue = InputCodec.fromAssetInputs(unsignedTx.inputs)
|
|
112
|
+
const inputs = inputsCodec.fromArray(inputsValue)
|
|
113
|
+
const fixedOutputsValue = AssetOutputCodec.fromFixedAssetOutputs(unsignedTx.fixedOutputs)
|
|
114
|
+
const fixedOutputs = assetOutputsCodec.fromArray(fixedOutputsValue)
|
|
115
|
+
const statefulScript = statefulScriptCodecOpt.fromBuffer(
|
|
116
|
+
unsignedTx.scriptOpt ? Buffer.from(unsignedTx.scriptOpt, 'hex') : undefined
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
return { version, networkId, gasAmount, gasPrice, inputs, fixedOutputs, statefulScript }
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export const unsignedTxCodec = new UnsignedTxCodec()
|