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