@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,97 @@
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 { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
22
+ import { Codec } from './codec'
23
+ import { instrsCodec, Instr } from './instr-codec'
24
+
25
+ export interface DecodedMethod {
26
+ isPublic: number
27
+ assetModifier: number
28
+ argsLength: DecodedCompactInt
29
+ localsLength: DecodedCompactInt
30
+ returnLength: DecodedCompactInt
31
+ instrs: DecodedArray<Instr>
32
+ }
33
+
34
+ export interface Method {
35
+ isPublic: boolean
36
+ assetModifier: number
37
+ argsLength: number
38
+ localsLength: number
39
+ returnLength: number
40
+ instrs: Instr[]
41
+ }
42
+
43
+ export class MethodCodec implements Codec<DecodedMethod> {
44
+ parser = Parser.start()
45
+ .uint8('isPublic')
46
+ .uint8('assetModifier')
47
+ .nest('argsLength', {
48
+ type: compactUnsignedIntCodec.parser
49
+ })
50
+ .nest('localsLength', {
51
+ type: compactUnsignedIntCodec.parser
52
+ })
53
+ .nest('returnLength', {
54
+ type: compactUnsignedIntCodec.parser
55
+ })
56
+ .nest('instrs', {
57
+ type: instrsCodec.parser
58
+ })
59
+
60
+ encode(input: DecodedMethod): Buffer {
61
+ const result = [input.isPublic, input.assetModifier]
62
+ result.push(...compactUnsignedIntCodec.encode(input.argsLength))
63
+ result.push(...compactUnsignedIntCodec.encode(input.localsLength))
64
+ result.push(...compactUnsignedIntCodec.encode(input.returnLength))
65
+ result.push(...instrsCodec.encode(input.instrs.value))
66
+ return Buffer.from(result)
67
+ }
68
+
69
+ decode(input: Buffer): DecodedMethod {
70
+ return this.parser.parse(input)
71
+ }
72
+
73
+ static toMethod(decodedMethod: DecodedMethod): Method {
74
+ return {
75
+ isPublic: decodedMethod.isPublic === 1,
76
+ assetModifier: decodedMethod.assetModifier,
77
+ argsLength: compactUnsignedIntCodec.toU32(decodedMethod.argsLength),
78
+ localsLength: compactUnsignedIntCodec.toU32(decodedMethod.localsLength),
79
+ returnLength: compactUnsignedIntCodec.toU32(decodedMethod.returnLength),
80
+ instrs: decodedMethod.instrs.value
81
+ }
82
+ }
83
+
84
+ static fromMethod(method: Method): DecodedMethod {
85
+ return {
86
+ isPublic: method.isPublic ? 1 : 0,
87
+ assetModifier: method.assetModifier,
88
+ argsLength: compactUnsignedIntCodec.fromU32(method.argsLength),
89
+ localsLength: compactUnsignedIntCodec.fromU32(method.localsLength),
90
+ returnLength: compactUnsignedIntCodec.fromU32(method.returnLength),
91
+ instrs: instrsCodec.fromArray(method.instrs)
92
+ }
93
+ }
94
+ }
95
+
96
+ export const methodCodec = new MethodCodec()
97
+ export const methodsCodec = new ArrayCodec(methodCodec)
@@ -0,0 +1,60 @@
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 { Codec } from './codec'
21
+
22
+ export interface Option<T> {
23
+ option: number
24
+ value?: T
25
+ }
26
+ export class OptionCodec<T> implements Codec<Option<T>> {
27
+ constructor(
28
+ private childCodec: Codec<T>,
29
+ public parser = new Parser().uint8('option').choice('value', {
30
+ tag: 'option',
31
+ choices: {
32
+ 0: new Parser(),
33
+ 1: childCodec.parser
34
+ }
35
+ })
36
+ ) {}
37
+
38
+ encode(input: Option<T>): Buffer {
39
+ const result = [input.option]
40
+ if (input.option === 1) {
41
+ result.push(...this.childCodec.encode(input.value!))
42
+ }
43
+ return Buffer.from(result)
44
+ }
45
+
46
+ decode(input: Buffer): Option<T> {
47
+ const result = this.parser.parse(input)
48
+ return {
49
+ ...result,
50
+ value: result.option ? this.childCodec.decode(result.value.value) : undefined
51
+ }
52
+ }
53
+
54
+ fromBuffer(input?: Buffer): Option<T> {
55
+ return {
56
+ option: input ? 1 : 0,
57
+ value: input ? this.childCodec.decode(input) : undefined
58
+ }
59
+ }
60
+ }
@@ -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,45 @@
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 { DecodedArray } from './array-codec'
21
+ import { Codec } from './codec'
22
+ import { DecodedMethod, methodsCodec } from './method-codec'
23
+ import { OptionCodec } from './option-codec'
24
+
25
+ export interface Script {
26
+ methods: DecodedArray<DecodedMethod>
27
+ }
28
+
29
+ export class ScriptCodec implements Codec<Script> {
30
+ parser = Parser.start().nest('methods', {
31
+ type: methodsCodec.parser
32
+ })
33
+
34
+ encode(input: Script): Buffer {
35
+ const script = methodsCodec.encode(input.methods.value)
36
+ return Buffer.from(script)
37
+ }
38
+
39
+ decode(input: Buffer): Script {
40
+ return this.parser.parse(input)
41
+ }
42
+ }
43
+
44
+ export const scriptCodec = new ScriptCodec()
45
+ export const statefulScriptCodecOpt = new OptionCodec(scriptCodec)
@@ -0,0 +1,40 @@
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 } from './array-codec'
21
+ import { Codec } from './codec'
22
+
23
+ export interface Signature {
24
+ value: Buffer
25
+ }
26
+
27
+ export class SignatureCodec implements Codec<Signature> {
28
+ parser = Parser.start().buffer('value', { length: 64 })
29
+
30
+ encode(input: Signature): Buffer {
31
+ return input.value
32
+ }
33
+
34
+ decode(input: Buffer): Signature {
35
+ return this.parser.parse(input)
36
+ }
37
+ }
38
+
39
+ export const signatureCodec = new SignatureCodec()
40
+ export const signaturesCodec = new ArrayCodec(signatureCodec)
@@ -0,0 +1,37 @@
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
+ import { Buffer } from 'buffer/'
22
+ export class SignedIntCodec implements Codec<number> {
23
+ parser = Parser.start().buffer('value', {
24
+ length: 4
25
+ })
26
+
27
+ encode(value: number): Buffer {
28
+ return Buffer.from([(value >> 24) & 0xff, (value >> 16) & 0xff, (value >> 8) & 0xff, value & 0xff])
29
+ }
30
+
31
+ decode(bytes: Buffer): number {
32
+ assert(bytes.length === 4, 'Length should be 4')
33
+ return ((bytes[0] & 0xff) << 24) | ((bytes[1] & 0xff) << 16) | ((bytes[2] & 0xff) << 8) | (bytes[3] & 0xff)
34
+ }
35
+ }
36
+
37
+ export const signedIntCodec = new SignedIntCodec()
@@ -0,0 +1,51 @@
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 { Buffer } from 'buffer/'
20
+ import { Parser } from 'binary-parser'
21
+ import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
22
+ import { Codec } from './codec'
23
+ import { ArrayCodec } from './array-codec'
24
+
25
+ export interface Token {
26
+ tokenId: Buffer
27
+ amount: DecodedCompactInt
28
+ }
29
+
30
+ export class TokenCodec implements Codec<Token> {
31
+ parser = Parser.start()
32
+ .buffer('tokenId', {
33
+ length: 32
34
+ })
35
+ .nest('amount', {
36
+ type: compactUnsignedIntCodec.parser
37
+ })
38
+
39
+ encode(input: Token): Buffer {
40
+ const tokenId = input.tokenId
41
+ const amount = Buffer.from(compactUnsignedIntCodec.encode(input.amount))
42
+ return Buffer.concat([tokenId, amount])
43
+ }
44
+
45
+ decode(input: Buffer): Token {
46
+ return this.parser.parse(input)
47
+ }
48
+ }
49
+
50
+ export const tokenCodec = new TokenCodec()
51
+ export const tokensCodec = new ArrayCodec(tokenCodec)
@@ -0,0 +1,147 @@
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 { Buffer } from 'buffer/'
20
+ import { Parser } from 'binary-parser'
21
+ import { DecodedArray } from './array-codec'
22
+
23
+ import { UnsignedTxCodec, UnsignedTx, unsignedTxCodec } from './unsigned-tx-codec'
24
+ import { Signature, signaturesCodec } from './signature-codec'
25
+ import { ContractOutputRef, contractOutputRefsCodec } from './contract-output-ref-codec'
26
+ import { Either } from './either-codec'
27
+ import { AssetOutput, AssetOutputCodec } from './asset-output-codec'
28
+ import { ContractOutput, ContractOutputCodec } from './contract-output-codec'
29
+ import { FixedAssetOutput, Transaction as ApiTransaction } from '../api/api-alephium'
30
+ import { hexToBinUnsafe } from '..'
31
+ import { ContractOutput as ApiContractOutput } from '../api/api-alephium'
32
+ import { Codec } from './codec'
33
+ import { Output, outputCodec, outputsCodec } from './output-codec'
34
+
35
+ export interface Transaction {
36
+ unsigned: UnsignedTx
37
+ scriptExecutionOk: number
38
+ contractInputs: DecodedArray<ContractOutputRef>
39
+ generatedOutputs: DecodedArray<Output>
40
+ inputSignatures: DecodedArray<Signature>
41
+ scriptSignatures: DecodedArray<Signature>
42
+ }
43
+
44
+ export class TransactionCodec implements Codec<Transaction> {
45
+ parser = new Parser()
46
+ .nest('unsigned', {
47
+ type: unsignedTxCodec.parser
48
+ })
49
+ .uint8('scriptExecutionOk')
50
+ .nest('contractInputs', {
51
+ type: contractOutputRefsCodec.parser
52
+ })
53
+ .nest('generatedOutputs', {
54
+ type: outputsCodec.parser
55
+ })
56
+ .nest('inputSignatures', {
57
+ type: signaturesCodec.parser
58
+ })
59
+ .nest('scriptSignatures', {
60
+ type: signaturesCodec.parser
61
+ })
62
+
63
+ encode(decodedTx: Transaction): Buffer {
64
+ return Buffer.concat([
65
+ unsignedTxCodec.encode(decodedTx.unsigned),
66
+ Buffer.from([decodedTx.scriptExecutionOk]),
67
+ Buffer.from([...contractOutputRefsCodec.encode(decodedTx.contractInputs.value)]),
68
+ Buffer.from([...outputsCodec.encode(decodedTx.generatedOutputs.value)]),
69
+ Buffer.from([...signaturesCodec.encode(decodedTx.inputSignatures.value)]),
70
+ Buffer.from([...signaturesCodec.encode(decodedTx.scriptSignatures.value)])
71
+ ])
72
+ }
73
+
74
+ decode(input: Buffer): Transaction {
75
+ return this.parser.parse(input)
76
+ }
77
+
78
+ encodeApiTransaction(input: ApiTransaction): Buffer {
79
+ const decodedTx = TransactionCodec.fromApiTransaction(input)
80
+ return this.encode(decodedTx)
81
+ }
82
+
83
+ decodeApiTransaction(input: Buffer): ApiTransaction {
84
+ const decodedTx = this.parser.parse(input)
85
+ return TransactionCodec.toApiTransaction(decodedTx)
86
+ }
87
+
88
+ static toApiTransaction(transaction: Transaction): ApiTransaction {
89
+ const txId = UnsignedTxCodec.txId(transaction.unsigned)
90
+ const unsigned = UnsignedTxCodec.toApiUnsignedTx(transaction.unsigned)
91
+ const scriptExecutionOk = !!transaction.scriptExecutionOk
92
+ const contractInputs = transaction.contractInputs.value.map((contractInput) => {
93
+ const hint = contractInput.hint
94
+ const key = contractInput.key.toString('hex')
95
+ return { hint, key }
96
+ })
97
+ const txIdBytes = hexToBinUnsafe(txId)
98
+ const generatedOutputs = transaction.generatedOutputs.value.map((output, index) => {
99
+ if (output.either === 0) {
100
+ const fixedAssetOutput = AssetOutputCodec.toFixedAssetOutput(txIdBytes, output.value as AssetOutput, index)
101
+ return { ...fixedAssetOutput, type: 'AssetOutput' }
102
+ } else {
103
+ return ContractOutputCodec.convertToApiContractOutput(txIdBytes, output.value as ContractOutput, index)
104
+ }
105
+ })
106
+
107
+ const inputSignatures = transaction.inputSignatures.value.map((signature) => signature.value.toString('hex'))
108
+ const scriptSignatures = transaction.scriptSignatures.value.map((signature) => signature.value.toString('hex'))
109
+
110
+ return { unsigned, scriptExecutionOk, contractInputs, generatedOutputs, inputSignatures, scriptSignatures }
111
+ }
112
+
113
+ static fromApiTransaction(tx: ApiTransaction): Transaction {
114
+ const unsigned = UnsignedTxCodec.fromApiUnsignedTx(tx.unsigned)
115
+ const scriptExecutionOk = tx.scriptExecutionOk ? 1 : 0
116
+ const contractInputs: ContractOutputRef[] = tx.contractInputs.map((contractInput) => {
117
+ return { hint: contractInput.hint, key: Buffer.from(contractInput.key, 'hex') }
118
+ })
119
+ const generatedOutputs: Either<AssetOutput, ContractOutput>[] = tx.generatedOutputs.map((output) => {
120
+ if (output.type === 'AssetOutput') {
121
+ return outputCodec.fromLeft(AssetOutputCodec.fromFixedAssetOutput(output as FixedAssetOutput))
122
+ } else if (output.type === 'ContractOutput') {
123
+ return outputCodec.fromRight(ContractOutputCodec.convertToOutput(output as ApiContractOutput))
124
+ } else {
125
+ throw new Error('Invalid output type')
126
+ }
127
+ })
128
+
129
+ const inputSignatures: Signature[] = tx.inputSignatures.map((signature) => {
130
+ return { value: Buffer.from(signature, 'hex') }
131
+ })
132
+ const scriptSignatures: Signature[] = tx.scriptSignatures.map((signature) => {
133
+ return { value: Buffer.from(signature, 'hex') }
134
+ })
135
+
136
+ return {
137
+ unsigned,
138
+ scriptExecutionOk,
139
+ contractInputs: contractOutputRefsCodec.fromArray(contractInputs),
140
+ generatedOutputs: outputsCodec.fromArray(generatedOutputs),
141
+ inputSignatures: signaturesCodec.fromArray(inputSignatures),
142
+ scriptSignatures: signaturesCodec.fromArray(scriptSignatures)
143
+ }
144
+ }
145
+ }
146
+
147
+ export const transactionCodec = new TransactionCodec()