@alephium/web3 0.30.0-beta.1 → 0.30.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.
Files changed (90) hide show
  1. package/dist/alephium-web3.min.js +1 -1
  2. package/dist/alephium-web3.min.js.map +1 -1
  3. package/dist/src/api/api-alephium.d.ts +5 -1
  4. package/dist/src/api/api-alephium.js +1 -1
  5. package/dist/src/contract/contract.d.ts +1 -0
  6. package/dist/src/contract/contract.js +1 -0
  7. package/dist/src/index.d.ts +0 -1
  8. package/dist/src/index.js +1 -2
  9. package/package.json +3 -4
  10. package/src/api/api-alephium.ts +5 -1
  11. package/src/contract/contract.ts +2 -0
  12. package/src/index.ts +1 -1
  13. package/dist/src/codec/array-codec.d.ts +0 -17
  14. package/dist/src/codec/array-codec.js +0 -58
  15. package/dist/src/codec/asset-output-codec.d.ts +0 -27
  16. package/dist/src/codec/asset-output-codec.js +0 -135
  17. package/dist/src/codec/bigint-codec.d.ts +0 -5
  18. package/dist/src/codec/bigint-codec.js +0 -85
  19. package/dist/src/codec/bytestring-codec.d.ts +0 -16
  20. package/dist/src/codec/bytestring-codec.js +0 -49
  21. package/dist/src/codec/codec.d.ts +0 -8
  22. package/dist/src/codec/codec.js +0 -9
  23. package/dist/src/codec/compact-int-codec.d.ts +0 -47
  24. package/dist/src/codec/compact-int-codec.js +0 -287
  25. package/dist/src/codec/contract-codec.d.ts +0 -22
  26. package/dist/src/codec/contract-codec.js +0 -62
  27. package/dist/src/codec/contract-output-codec.d.ts +0 -21
  28. package/dist/src/codec/contract-output-codec.js +0 -81
  29. package/dist/src/codec/contract-output-ref-codec.d.ts +0 -15
  30. package/dist/src/codec/contract-output-ref-codec.js +0 -37
  31. package/dist/src/codec/either-codec.d.ts +0 -17
  32. package/dist/src/codec/either-codec.js +0 -66
  33. package/dist/src/codec/hash.d.ts +0 -4
  34. package/dist/src/codec/hash.js +0 -40
  35. package/dist/src/codec/index.d.ts +0 -23
  36. package/dist/src/codec/index.js +0 -69
  37. package/dist/src/codec/input-codec.d.ts +0 -22
  38. package/dist/src/codec/input-codec.js +0 -70
  39. package/dist/src/codec/instr-codec.d.ts +0 -223
  40. package/dist/src/codec/instr-codec.js +0 -459
  41. package/dist/src/codec/lockup-script-codec.d.ts +0 -28
  42. package/dist/src/codec/lockup-script-codec.js +0 -79
  43. package/dist/src/codec/long-codec.d.ts +0 -9
  44. package/dist/src/codec/long-codec.js +0 -55
  45. package/dist/src/codec/method-codec.d.ts +0 -30
  46. package/dist/src/codec/method-codec.js +0 -67
  47. package/dist/src/codec/option-codec.d.ts +0 -15
  48. package/dist/src/codec/option-codec.js +0 -54
  49. package/dist/src/codec/output-codec.d.ts +0 -7
  50. package/dist/src/codec/output-codec.js +0 -26
  51. package/dist/src/codec/script-codec.d.ts +0 -16
  52. package/dist/src/codec/script-codec.js +0 -40
  53. package/dist/src/codec/signature-codec.d.ts +0 -14
  54. package/dist/src/codec/signature-codec.js +0 -36
  55. package/dist/src/codec/signed-int-codec.d.ts +0 -9
  56. package/dist/src/codec/signed-int-codec.js +0 -38
  57. package/dist/src/codec/token-codec.d.ts +0 -16
  58. package/dist/src/codec/token-codec.js +0 -45
  59. package/dist/src/codec/transaction-codec.d.ts +0 -27
  60. package/dist/src/codec/transaction-codec.js +0 -127
  61. package/dist/src/codec/unlock-script-codec.d.ts +0 -40
  62. package/dist/src/codec/unlock-script-codec.js +0 -169
  63. package/dist/src/codec/unsigned-tx-codec.d.ts +0 -30
  64. package/dist/src/codec/unsigned-tx-codec.js +0 -102
  65. package/src/codec/array-codec.ts +0 -62
  66. package/src/codec/asset-output-codec.ts +0 -150
  67. package/src/codec/bigint-codec.ts +0 -91
  68. package/src/codec/bytestring-codec.ts +0 -55
  69. package/src/codec/codec.ts +0 -30
  70. package/src/codec/compact-int-codec.ts +0 -299
  71. package/src/codec/contract-codec.ts +0 -76
  72. package/src/codec/contract-output-codec.ts +0 -97
  73. package/src/codec/contract-output-ref-codec.ts +0 -41
  74. package/src/codec/either-codec.ts +0 -73
  75. package/src/codec/hash.ts +0 -34
  76. package/src/codec/index.ts +0 -41
  77. package/src/codec/input-codec.ts +0 -80
  78. package/src/codec/instr-codec.ts +0 -462
  79. package/src/codec/lockup-script-codec.ts +0 -98
  80. package/src/codec/long-codec.ts +0 -58
  81. package/src/codec/method-codec.ts +0 -85
  82. package/src/codec/option-codec.ts +0 -59
  83. package/src/codec/output-codec.ts +0 -26
  84. package/src/codec/script-codec.ts +0 -44
  85. package/src/codec/signature-codec.ts +0 -39
  86. package/src/codec/signed-int-codec.ts +0 -36
  87. package/src/codec/token-codec.ts +0 -49
  88. package/src/codec/transaction-codec.ts +0 -146
  89. package/src/codec/unlock-script-codec.ts +0 -193
  90. package/src/codec/unsigned-tx-codec.ts +0 -123
@@ -1,193 +0,0 @@
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()
@@ -1,123 +0,0 @@
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()