@alephium/web3 0.29.2 → 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.
Files changed (87) 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 +1 -1
  4. package/dist/src/api/api-alephium.js +1 -1
  5. package/dist/src/codec/array-codec.d.ts +17 -0
  6. package/dist/src/codec/array-codec.js +58 -0
  7. package/dist/src/codec/asset-output-codec.d.ts +27 -0
  8. package/dist/src/codec/asset-output-codec.js +135 -0
  9. package/dist/src/codec/bigint-codec.d.ts +5 -0
  10. package/dist/src/codec/bigint-codec.js +85 -0
  11. package/dist/src/codec/bytestring-codec.d.ts +16 -0
  12. package/dist/src/codec/bytestring-codec.js +49 -0
  13. package/dist/src/codec/codec.d.ts +8 -0
  14. package/dist/src/codec/codec.js +9 -0
  15. package/dist/src/codec/compact-int-codec.d.ts +47 -0
  16. package/dist/src/codec/compact-int-codec.js +287 -0
  17. package/dist/src/codec/contract-codec.d.ts +22 -0
  18. package/dist/src/codec/contract-codec.js +62 -0
  19. package/dist/src/codec/contract-output-codec.d.ts +21 -0
  20. package/dist/src/codec/contract-output-codec.js +81 -0
  21. package/dist/src/codec/contract-output-ref-codec.d.ts +15 -0
  22. package/dist/src/codec/contract-output-ref-codec.js +37 -0
  23. package/dist/src/codec/either-codec.d.ts +17 -0
  24. package/dist/src/codec/either-codec.js +66 -0
  25. package/dist/src/codec/hash.d.ts +4 -0
  26. package/dist/src/codec/hash.js +40 -0
  27. package/dist/src/codec/index.d.ts +23 -0
  28. package/dist/src/codec/index.js +69 -0
  29. package/dist/src/codec/input-codec.d.ts +22 -0
  30. package/dist/src/codec/input-codec.js +70 -0
  31. package/dist/src/codec/instr-codec.d.ts +223 -0
  32. package/dist/src/codec/instr-codec.js +459 -0
  33. package/dist/src/codec/lockup-script-codec.d.ts +28 -0
  34. package/dist/src/codec/lockup-script-codec.js +79 -0
  35. package/dist/src/codec/long-codec.d.ts +9 -0
  36. package/dist/src/codec/long-codec.js +55 -0
  37. package/dist/src/codec/method-codec.d.ts +30 -0
  38. package/dist/src/codec/method-codec.js +67 -0
  39. package/dist/src/codec/option-codec.d.ts +15 -0
  40. package/dist/src/codec/option-codec.js +54 -0
  41. package/dist/src/codec/output-codec.d.ts +7 -0
  42. package/dist/src/codec/output-codec.js +26 -0
  43. package/dist/src/codec/script-codec.d.ts +16 -0
  44. package/dist/src/codec/script-codec.js +40 -0
  45. package/dist/src/codec/signature-codec.d.ts +14 -0
  46. package/dist/src/codec/signature-codec.js +36 -0
  47. package/dist/src/codec/signed-int-codec.d.ts +9 -0
  48. package/dist/src/codec/signed-int-codec.js +38 -0
  49. package/dist/src/codec/token-codec.d.ts +16 -0
  50. package/dist/src/codec/token-codec.js +45 -0
  51. package/dist/src/codec/transaction-codec.d.ts +27 -0
  52. package/dist/src/codec/transaction-codec.js +127 -0
  53. package/dist/src/codec/unlock-script-codec.d.ts +40 -0
  54. package/dist/src/codec/unlock-script-codec.js +169 -0
  55. package/dist/src/codec/unsigned-tx-codec.d.ts +30 -0
  56. package/dist/src/codec/unsigned-tx-codec.js +102 -0
  57. package/dist/src/index.d.ts +1 -0
  58. package/dist/src/index.js +2 -1
  59. package/package.json +4 -3
  60. package/src/api/api-alephium.ts +1 -1
  61. package/src/codec/array-codec.ts +62 -0
  62. package/src/codec/asset-output-codec.ts +150 -0
  63. package/src/codec/bigint-codec.ts +91 -0
  64. package/src/codec/bytestring-codec.ts +55 -0
  65. package/src/codec/codec.ts +30 -0
  66. package/src/codec/compact-int-codec.ts +299 -0
  67. package/src/codec/contract-codec.ts +76 -0
  68. package/src/codec/contract-output-codec.ts +97 -0
  69. package/src/codec/contract-output-ref-codec.ts +41 -0
  70. package/src/codec/either-codec.ts +73 -0
  71. package/src/codec/hash.ts +34 -0
  72. package/src/codec/index.ts +41 -0
  73. package/src/codec/input-codec.ts +80 -0
  74. package/src/codec/instr-codec.ts +462 -0
  75. package/src/codec/lockup-script-codec.ts +98 -0
  76. package/src/codec/long-codec.ts +58 -0
  77. package/src/codec/method-codec.ts +85 -0
  78. package/src/codec/option-codec.ts +59 -0
  79. package/src/codec/output-codec.ts +26 -0
  80. package/src/codec/script-codec.ts +44 -0
  81. package/src/codec/signature-codec.ts +39 -0
  82. package/src/codec/signed-int-codec.ts +36 -0
  83. package/src/codec/token-codec.ts +49 -0
  84. package/src/codec/transaction-codec.ts +146 -0
  85. package/src/codec/unlock-script-codec.ts +193 -0
  86. package/src/codec/unsigned-tx-codec.ts +123 -0
  87. package/src/index.ts +1 -1
@@ -0,0 +1,98 @@
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 { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
20
+ import { Codec } from './codec'
21
+ import { ArrayCodec, DecodedArray } from './array-codec'
22
+
23
+ export interface PublicKeyHash {
24
+ publicKeyHash: Buffer
25
+ }
26
+
27
+ class PublicKeyHashCodec implements Codec<PublicKeyHash> {
28
+ parser = Parser.start().buffer('publicKeyHash', { length: 32 })
29
+
30
+ encode(input: PublicKeyHash): Buffer {
31
+ return input.publicKeyHash
32
+ }
33
+
34
+ decode(input: Buffer): PublicKeyHash {
35
+ return this.parser.parse(input)
36
+ }
37
+ }
38
+
39
+ const publicKeyHashCodec = new PublicKeyHashCodec()
40
+ const publicKeyHashesCodec = new ArrayCodec(publicKeyHashCodec)
41
+ const multiSigParser = Parser.start()
42
+ .nest('publicKeyHashes', { type: publicKeyHashesCodec.parser })
43
+ .nest('m', { type: compactUnsignedIntCodec.parser })
44
+ export interface MultiSig {
45
+ publicKeyHashes: DecodedArray<PublicKeyHash>
46
+ m: DecodedCompactInt
47
+ }
48
+
49
+ export interface P2SH {
50
+ scriptHash: Buffer
51
+ }
52
+
53
+ export interface P2C {
54
+ contractId: Buffer
55
+ }
56
+
57
+ export interface LockupScript {
58
+ scriptType: number
59
+ script: PublicKeyHash | MultiSig | P2SH | P2C
60
+ }
61
+
62
+ export class LockupScriptCodec implements Codec<LockupScript> {
63
+ parser = Parser.start()
64
+ .uint8('scriptType')
65
+ .choice('script', {
66
+ tag: 'scriptType',
67
+ choices: {
68
+ 0: publicKeyHashCodec.parser,
69
+ 1: multiSigParser,
70
+ 2: Parser.start().buffer('scriptHash', { length: 32 }),
71
+ 3: Parser.start().buffer('contractId', { length: 32 })
72
+ }
73
+ })
74
+
75
+ encode(input: LockupScript): Buffer {
76
+ const result: number[] = [input.scriptType]
77
+ if (input.scriptType === 0) {
78
+ result.push(...(input.script as PublicKeyHash).publicKeyHash)
79
+ } else if (input.scriptType === 1) {
80
+ result.push(...publicKeyHashesCodec.encode((input.script as MultiSig).publicKeyHashes.value))
81
+ result.push(...compactUnsignedIntCodec.encode((input.script as MultiSig).m))
82
+ } else if (input.scriptType === 2) {
83
+ result.push(...(input.script as P2SH).scriptHash)
84
+ } else if (input.scriptType === 3) {
85
+ result.push(...(input.script as P2C).contractId)
86
+ } else {
87
+ throw new Error(`Unsupported script type: ${input.scriptType}`)
88
+ }
89
+
90
+ return Buffer.from(result)
91
+ }
92
+
93
+ decode(input: Buffer): LockupScript {
94
+ return this.parser.parse(input)
95
+ }
96
+ }
97
+
98
+ export const lockupScriptCodec = new LockupScriptCodec()
@@ -0,0 +1,58 @@
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 { Codec, assert } from './codec'
20
+
21
+ export class LongCodec implements Codec<bigint> {
22
+ parser = Parser.start().buffer('value', {
23
+ length: 8
24
+ })
25
+
26
+ encode(input: bigint): Buffer {
27
+ const byteArray = new Uint8Array(8)
28
+
29
+ assert(byteArray.length <= 8, 'Length should be less than or equal to 8')
30
+ for (let index = 0; index < byteArray.length; index++) {
31
+ const byte = input & BigInt(0xff)
32
+ byteArray[byteArray.length - index - 1] = Number(byte)
33
+ input >>= BigInt(8)
34
+ }
35
+
36
+ return Buffer.from(byteArray)
37
+ }
38
+
39
+ decode(bytes: Buffer): bigint {
40
+ assert(bytes.length == 8, 'Length should be 8')
41
+ let int64 = BigInt(0)
42
+ let pow = BigInt(1)
43
+
44
+ for (let i = bytes.length - 1; i >= 0; i--) {
45
+ int64 += BigInt(bytes[i]) * pow
46
+ pow *= BigInt(256)
47
+ }
48
+
49
+ // Determine if the number is negative (check the sign bit of the first byte)
50
+ if (bytes[0] & 0x80) {
51
+ int64 -= BigInt(1) << BigInt(64)
52
+ }
53
+
54
+ return int64
55
+ }
56
+ }
57
+
58
+ export const longCodec = new LongCodec()
@@ -0,0 +1,85 @@
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 { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
21
+ import { Codec } from './codec'
22
+ import { instrsCodec, Instr } from './instr-codec'
23
+
24
+ export interface DecodedMethod {
25
+ isPublic: number
26
+ assetModifier: number
27
+ argsLength: DecodedCompactInt
28
+ localsLength: DecodedCompactInt
29
+ returnLength: DecodedCompactInt
30
+ instrs: DecodedArray<Instr>
31
+ }
32
+
33
+ export interface Method {
34
+ isPublic: boolean
35
+ assetModifier: number
36
+ argsLength: number
37
+ localsLength: number
38
+ returnLength: number
39
+ instrs: Instr[]
40
+ }
41
+
42
+ export class MethodCodec implements Codec<DecodedMethod> {
43
+ parser = Parser.start()
44
+ .uint8('isPublic')
45
+ .uint8('assetModifier')
46
+ .nest('argsLength', {
47
+ type: compactUnsignedIntCodec.parser
48
+ })
49
+ .nest('localsLength', {
50
+ type: compactUnsignedIntCodec.parser
51
+ })
52
+ .nest('returnLength', {
53
+ type: compactUnsignedIntCodec.parser
54
+ })
55
+ .nest('instrs', {
56
+ type: instrsCodec.parser
57
+ })
58
+
59
+ encode(input: DecodedMethod): Buffer {
60
+ const result = [input.isPublic, input.assetModifier]
61
+ result.push(...compactUnsignedIntCodec.encode(input.argsLength))
62
+ result.push(...compactUnsignedIntCodec.encode(input.localsLength))
63
+ result.push(...compactUnsignedIntCodec.encode(input.returnLength))
64
+ result.push(...instrsCodec.encode(input.instrs.value))
65
+ return Buffer.from(result)
66
+ }
67
+
68
+ decode(input: Buffer): DecodedMethod {
69
+ return this.parser.parse(input)
70
+ }
71
+
72
+ static toMethod(decodedMethod: DecodedMethod): Method {
73
+ return {
74
+ isPublic: decodedMethod.isPublic === 1,
75
+ assetModifier: decodedMethod.assetModifier,
76
+ argsLength: compactUnsignedIntCodec.toU32(decodedMethod.argsLength),
77
+ localsLength: compactUnsignedIntCodec.toU32(decodedMethod.localsLength),
78
+ returnLength: compactUnsignedIntCodec.toU32(decodedMethod.returnLength),
79
+ instrs: decodedMethod.instrs.value
80
+ }
81
+ }
82
+ }
83
+
84
+ export const methodCodec = new MethodCodec()
85
+ export const methodsCodec = new ArrayCodec(methodCodec)
@@ -0,0 +1,59 @@
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 { Codec } from './codec'
20
+
21
+ export interface Option<T> {
22
+ option: number
23
+ value?: T
24
+ }
25
+ export class OptionCodec<T> implements Codec<Option<T>> {
26
+ constructor(
27
+ private childCodec: Codec<T>,
28
+ public parser = new Parser().uint8('option').choice('value', {
29
+ tag: 'option',
30
+ choices: {
31
+ 0: new Parser(),
32
+ 1: childCodec.parser
33
+ }
34
+ })
35
+ ) {}
36
+
37
+ encode(input: Option<T>): Buffer {
38
+ const result = [input.option]
39
+ if (input.option === 1) {
40
+ result.push(...this.childCodec.encode(input.value!))
41
+ }
42
+ return Buffer.from(result)
43
+ }
44
+
45
+ decode(input: Buffer): Option<T> {
46
+ const result = this.parser.parse(input)
47
+ return {
48
+ ...result,
49
+ value: result.option ? this.childCodec.decode(result.value.value) : undefined
50
+ }
51
+ }
52
+
53
+ fromBuffer(input?: Buffer): Option<T> {
54
+ return {
55
+ option: input ? 1 : 0,
56
+ value: input ? this.childCodec.decode(input) : undefined
57
+ }
58
+ }
59
+ }
@@ -0,0 +1,26 @@
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
+ import { ArrayCodec } from './array-codec'
20
+ import { Either, EitherCodec } from './either-codec'
21
+ import { AssetOutput, assetOutputCodec } from './asset-output-codec'
22
+ import { ContractOutput, contractOutputCodec } from './contract-output-codec'
23
+
24
+ export type Output = Either<AssetOutput, ContractOutput>
25
+ export const outputCodec = new EitherCodec<AssetOutput, ContractOutput>(assetOutputCodec, contractOutputCodec)
26
+ export const outputsCodec = new ArrayCodec(outputCodec)
@@ -0,0 +1,44 @@
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 { DecodedArray } from './array-codec'
20
+ import { Codec } from './codec'
21
+ import { DecodedMethod, methodsCodec } from './method-codec'
22
+ import { OptionCodec } from './option-codec'
23
+
24
+ export interface Script {
25
+ methods: DecodedArray<DecodedMethod>
26
+ }
27
+
28
+ export class ScriptCodec implements Codec<Script> {
29
+ parser = Parser.start().nest('methods', {
30
+ type: methodsCodec.parser
31
+ })
32
+
33
+ encode(input: Script): Buffer {
34
+ const script = methodsCodec.encode(input.methods.value)
35
+ return Buffer.from(script)
36
+ }
37
+
38
+ decode(input: Buffer): Script {
39
+ return this.parser.parse(input)
40
+ }
41
+ }
42
+
43
+ export const scriptCodec = new ScriptCodec()
44
+ export const statefulScriptCodecOpt = new OptionCodec(scriptCodec)
@@ -0,0 +1,39 @@
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 } from './array-codec'
20
+ import { Codec } from './codec'
21
+
22
+ export interface Signature {
23
+ value: Buffer
24
+ }
25
+
26
+ export class SignatureCodec implements Codec<Signature> {
27
+ parser = Parser.start().buffer('value', { length: 64 })
28
+
29
+ encode(input: Signature): Buffer {
30
+ return input.value
31
+ }
32
+
33
+ decode(input: Buffer): Signature {
34
+ return this.parser.parse(input)
35
+ }
36
+ }
37
+
38
+ export const signatureCodec = new SignatureCodec()
39
+ export const signaturesCodec = new ArrayCodec(signatureCodec)
@@ -0,0 +1,36 @@
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 { Codec, assert } from './codec'
20
+
21
+ export class SignedIntCodec implements Codec<number> {
22
+ parser = Parser.start().buffer('value', {
23
+ length: 4
24
+ })
25
+
26
+ encode(value: number): Buffer {
27
+ return Buffer.from([(value >> 24) & 0xff, (value >> 16) & 0xff, (value >> 8) & 0xff, value & 0xff])
28
+ }
29
+
30
+ decode(bytes: Buffer): number {
31
+ assert(bytes.length === 4, 'Length should be 4')
32
+ return ((bytes[0] & 0xff) << 24) | ((bytes[1] & 0xff) << 16) | ((bytes[2] & 0xff) << 8) | (bytes[3] & 0xff)
33
+ }
34
+ }
35
+
36
+ export const signedIntCodec = new SignedIntCodec()
@@ -0,0 +1,49 @@
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 { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
20
+ import { Codec } from './codec'
21
+ import { ArrayCodec } from './array-codec'
22
+
23
+ export interface Token {
24
+ tokenId: Buffer
25
+ amount: DecodedCompactInt
26
+ }
27
+
28
+ export class TokenCodec implements Codec<Token> {
29
+ parser = Parser.start()
30
+ .buffer('tokenId', {
31
+ length: 32
32
+ })
33
+ .nest('amount', {
34
+ type: compactUnsignedIntCodec.parser
35
+ })
36
+
37
+ encode(input: Token): Buffer {
38
+ const tokenId = input.tokenId
39
+ const amount = Buffer.from(compactUnsignedIntCodec.encode(input.amount))
40
+ return Buffer.concat([tokenId, amount])
41
+ }
42
+
43
+ decode(input: Buffer): Token {
44
+ return this.parser.parse(input)
45
+ }
46
+ }
47
+
48
+ export const tokenCodec = new TokenCodec()
49
+ export const tokensCodec = new ArrayCodec(tokenCodec)
@@ -0,0 +1,146 @@
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
+ import { Parser } from 'binary-parser'
20
+ import { DecodedArray } from './array-codec'
21
+
22
+ import { UnsignedTxCodec, UnsignedTx, unsignedTxCodec } from './unsigned-tx-codec'
23
+ import { Signature, signaturesCodec } from './signature-codec'
24
+ import { ContractOutputRef, contractOutputRefsCodec } from './contract-output-ref-codec'
25
+ import { Either } from './either-codec'
26
+ import { AssetOutput, AssetOutputCodec } from './asset-output-codec'
27
+ import { ContractOutput, ContractOutputCodec } from './contract-output-codec'
28
+ import { FixedAssetOutput, Transaction as ApiTransaction } from '../api/api-alephium'
29
+ import { hexToBinUnsafe } from '..'
30
+ import { ContractOutput as ApiContractOutput } from '../api/api-alephium'
31
+ import { Codec } from './codec'
32
+ import { Output, outputCodec, outputsCodec } from './output-codec'
33
+
34
+ export interface Transaction {
35
+ unsigned: UnsignedTx
36
+ scriptExecutionOk: number
37
+ contractInputs: DecodedArray<ContractOutputRef>
38
+ generatedOutputs: DecodedArray<Output>
39
+ inputSignatures: DecodedArray<Signature>
40
+ scriptSignatures: DecodedArray<Signature>
41
+ }
42
+
43
+ export class TransactionCodec implements Codec<Transaction> {
44
+ parser = new Parser()
45
+ .nest('unsigned', {
46
+ type: unsignedTxCodec.parser
47
+ })
48
+ .uint8('scriptExecutionOk')
49
+ .nest('contractInputs', {
50
+ type: contractOutputRefsCodec.parser
51
+ })
52
+ .nest('generatedOutputs', {
53
+ type: outputsCodec.parser
54
+ })
55
+ .nest('inputSignatures', {
56
+ type: signaturesCodec.parser
57
+ })
58
+ .nest('scriptSignatures', {
59
+ type: signaturesCodec.parser
60
+ })
61
+
62
+ encode(decodedTx: Transaction): Buffer {
63
+ return Buffer.concat([
64
+ unsignedTxCodec.encode(decodedTx.unsigned),
65
+ Buffer.from([decodedTx.scriptExecutionOk]),
66
+ Buffer.from([...contractOutputRefsCodec.encode(decodedTx.contractInputs.value)]),
67
+ Buffer.from([...outputsCodec.encode(decodedTx.generatedOutputs.value)]),
68
+ Buffer.from([...signaturesCodec.encode(decodedTx.inputSignatures.value)]),
69
+ Buffer.from([...signaturesCodec.encode(decodedTx.scriptSignatures.value)])
70
+ ])
71
+ }
72
+
73
+ decode(input: Buffer): Transaction {
74
+ return this.parser.parse(input)
75
+ }
76
+
77
+ encodeApiTransaction(input: ApiTransaction): Buffer {
78
+ const decodedTx = TransactionCodec.fromApiTransaction(input)
79
+ return this.encode(decodedTx)
80
+ }
81
+
82
+ decodeApiTransaction(input: Buffer): ApiTransaction {
83
+ const decodedTx = this.parser.parse(input)
84
+ return TransactionCodec.toApiTransaction(decodedTx)
85
+ }
86
+
87
+ static toApiTransaction(transaction: Transaction): ApiTransaction {
88
+ const txId = UnsignedTxCodec.txId(transaction.unsigned)
89
+ const unsigned = UnsignedTxCodec.toApiUnsignedTx(transaction.unsigned)
90
+ const scriptExecutionOk = !!transaction.scriptExecutionOk
91
+ const contractInputs = transaction.contractInputs.value.map((contractInput) => {
92
+ const hint = contractInput.hint
93
+ const key = contractInput.key.toString('hex')
94
+ return { hint, key }
95
+ })
96
+ const txIdBytes = hexToBinUnsafe(txId)
97
+ const generatedOutputs = transaction.generatedOutputs.value.map((output, index) => {
98
+ if (output.either === 0) {
99
+ const fixedAssetOutput = AssetOutputCodec.toFixedAssetOutput(txIdBytes, output.value as AssetOutput, index)
100
+ return { ...fixedAssetOutput, type: 'AssetOutput' }
101
+ } else {
102
+ return ContractOutputCodec.convertToApiContractOutput(txIdBytes, output.value as ContractOutput, index)
103
+ }
104
+ })
105
+
106
+ const inputSignatures = transaction.inputSignatures.value.map((signature) => signature.value.toString('hex'))
107
+ const scriptSignatures = transaction.scriptSignatures.value.map((signature) => signature.value.toString('hex'))
108
+
109
+ return { unsigned, scriptExecutionOk, contractInputs, generatedOutputs, inputSignatures, scriptSignatures }
110
+ }
111
+
112
+ static fromApiTransaction(tx: ApiTransaction): Transaction {
113
+ const unsigned = UnsignedTxCodec.fromApiUnsignedTx(tx.unsigned)
114
+ const scriptExecutionOk = tx.scriptExecutionOk ? 1 : 0
115
+ const contractInputs: ContractOutputRef[] = tx.contractInputs.map((contractInput) => {
116
+ return { hint: contractInput.hint, key: Buffer.from(contractInput.key, 'hex') }
117
+ })
118
+ const generatedOutputs: Either<AssetOutput, ContractOutput>[] = tx.generatedOutputs.map((output) => {
119
+ if (output.type === 'AssetOutput') {
120
+ return outputCodec.fromLeft(AssetOutputCodec.fromFixedAssetOutput(output as FixedAssetOutput))
121
+ } else if (output.type === 'ContractOutput') {
122
+ return outputCodec.fromRight(ContractOutputCodec.convertToOutput(output as ApiContractOutput))
123
+ } else {
124
+ throw new Error('Invalid output type')
125
+ }
126
+ })
127
+
128
+ const inputSignatures: Signature[] = tx.inputSignatures.map((signature) => {
129
+ return { value: Buffer.from(signature, 'hex') }
130
+ })
131
+ const scriptSignatures: Signature[] = tx.scriptSignatures.map((signature) => {
132
+ return { value: Buffer.from(signature, 'hex') }
133
+ })
134
+
135
+ return {
136
+ unsigned,
137
+ scriptExecutionOk,
138
+ contractInputs: contractOutputRefsCodec.fromArray(contractInputs),
139
+ generatedOutputs: outputsCodec.fromArray(generatedOutputs),
140
+ inputSignatures: signaturesCodec.fromArray(inputSignatures),
141
+ scriptSignatures: signaturesCodec.fromArray(scriptSignatures)
142
+ }
143
+ }
144
+ }
145
+
146
+ export const transactionCodec = new TransactionCodec()