@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.
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,30 @@
1
+ /// <reference types="node" />
2
+ import { Parser } from 'binary-parser';
3
+ import { ArrayCodec, DecodedArray } from './array-codec';
4
+ import { DecodedCompactInt } from './compact-int-codec';
5
+ import { Codec } from './codec';
6
+ import { Instr } from './instr-codec';
7
+ export interface DecodedMethod {
8
+ isPublic: number;
9
+ assetModifier: number;
10
+ argsLength: DecodedCompactInt;
11
+ localsLength: DecodedCompactInt;
12
+ returnLength: DecodedCompactInt;
13
+ instrs: DecodedArray<Instr>;
14
+ }
15
+ export interface Method {
16
+ isPublic: boolean;
17
+ assetModifier: number;
18
+ argsLength: number;
19
+ localsLength: number;
20
+ returnLength: number;
21
+ instrs: Instr[];
22
+ }
23
+ export declare class MethodCodec implements Codec<DecodedMethod> {
24
+ parser: Parser;
25
+ encode(input: DecodedMethod): Buffer;
26
+ decode(input: Buffer): DecodedMethod;
27
+ static toMethod(decodedMethod: DecodedMethod): Method;
28
+ }
29
+ export declare const methodCodec: MethodCodec;
30
+ export declare const methodsCodec: ArrayCodec<DecodedMethod>;
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.methodsCodec = exports.methodCodec = exports.MethodCodec = void 0;
4
+ /*
5
+ Copyright 2018 - 2022 The Alephium Authors
6
+ This file is part of the alephium project.
7
+
8
+ The library is free software: you can redistribute it and/or modify
9
+ it under the terms of the GNU Lesser General Public License as published by
10
+ the Free Software Foundation, either version 3 of the License, or
11
+ (at your option) any later version.
12
+
13
+ The library is distributed in the hope that it will be useful,
14
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ GNU Lesser General Public License for more details.
17
+
18
+ You should have received a copy of the GNU Lesser General Public License
19
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
20
+ */
21
+ const binary_parser_1 = require("binary-parser");
22
+ const array_codec_1 = require("./array-codec");
23
+ const compact_int_codec_1 = require("./compact-int-codec");
24
+ const instr_codec_1 = require("./instr-codec");
25
+ class MethodCodec {
26
+ constructor() {
27
+ this.parser = binary_parser_1.Parser.start()
28
+ .uint8('isPublic')
29
+ .uint8('assetModifier')
30
+ .nest('argsLength', {
31
+ type: compact_int_codec_1.compactUnsignedIntCodec.parser
32
+ })
33
+ .nest('localsLength', {
34
+ type: compact_int_codec_1.compactUnsignedIntCodec.parser
35
+ })
36
+ .nest('returnLength', {
37
+ type: compact_int_codec_1.compactUnsignedIntCodec.parser
38
+ })
39
+ .nest('instrs', {
40
+ type: instr_codec_1.instrsCodec.parser
41
+ });
42
+ }
43
+ encode(input) {
44
+ const result = [input.isPublic, input.assetModifier];
45
+ result.push(...compact_int_codec_1.compactUnsignedIntCodec.encode(input.argsLength));
46
+ result.push(...compact_int_codec_1.compactUnsignedIntCodec.encode(input.localsLength));
47
+ result.push(...compact_int_codec_1.compactUnsignedIntCodec.encode(input.returnLength));
48
+ result.push(...instr_codec_1.instrsCodec.encode(input.instrs.value));
49
+ return Buffer.from(result);
50
+ }
51
+ decode(input) {
52
+ return this.parser.parse(input);
53
+ }
54
+ static toMethod(decodedMethod) {
55
+ return {
56
+ isPublic: decodedMethod.isPublic === 1,
57
+ assetModifier: decodedMethod.assetModifier,
58
+ argsLength: compact_int_codec_1.compactUnsignedIntCodec.toU32(decodedMethod.argsLength),
59
+ localsLength: compact_int_codec_1.compactUnsignedIntCodec.toU32(decodedMethod.localsLength),
60
+ returnLength: compact_int_codec_1.compactUnsignedIntCodec.toU32(decodedMethod.returnLength),
61
+ instrs: decodedMethod.instrs.value
62
+ };
63
+ }
64
+ }
65
+ exports.MethodCodec = MethodCodec;
66
+ exports.methodCodec = new MethodCodec();
67
+ exports.methodsCodec = new array_codec_1.ArrayCodec(exports.methodCodec);
@@ -0,0 +1,15 @@
1
+ /// <reference types="node" />
2
+ import { Parser } from 'binary-parser';
3
+ import { Codec } from './codec';
4
+ export interface Option<T> {
5
+ option: number;
6
+ value?: T;
7
+ }
8
+ export declare class OptionCodec<T> implements Codec<Option<T>> {
9
+ private childCodec;
10
+ parser: Parser;
11
+ constructor(childCodec: Codec<T>, parser?: Parser);
12
+ encode(input: Option<T>): Buffer;
13
+ decode(input: Buffer): Option<T>;
14
+ fromBuffer(input?: Buffer): Option<T>;
15
+ }
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OptionCodec = void 0;
4
+ /*
5
+ Copyright 2018 - 2022 The Alephium Authors
6
+ This file is part of the alephium project.
7
+
8
+ The library is free software: you can redistribute it and/or modify
9
+ it under the terms of the GNU Lesser General Public License as published by
10
+ the Free Software Foundation, either version 3 of the License, or
11
+ (at your option) any later version.
12
+
13
+ The library is distributed in the hope that it will be useful,
14
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ GNU Lesser General Public License for more details.
17
+
18
+ You should have received a copy of the GNU Lesser General Public License
19
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
20
+ */
21
+ const binary_parser_1 = require("binary-parser");
22
+ class OptionCodec {
23
+ constructor(childCodec, parser = new binary_parser_1.Parser().uint8('option').choice('value', {
24
+ tag: 'option',
25
+ choices: {
26
+ 0: new binary_parser_1.Parser(),
27
+ 1: childCodec.parser
28
+ }
29
+ })) {
30
+ this.childCodec = childCodec;
31
+ this.parser = parser;
32
+ }
33
+ encode(input) {
34
+ const result = [input.option];
35
+ if (input.option === 1) {
36
+ result.push(...this.childCodec.encode(input.value));
37
+ }
38
+ return Buffer.from(result);
39
+ }
40
+ decode(input) {
41
+ const result = this.parser.parse(input);
42
+ return {
43
+ ...result,
44
+ value: result.option ? this.childCodec.decode(result.value.value) : undefined
45
+ };
46
+ }
47
+ fromBuffer(input) {
48
+ return {
49
+ option: input ? 1 : 0,
50
+ value: input ? this.childCodec.decode(input) : undefined
51
+ };
52
+ }
53
+ }
54
+ exports.OptionCodec = OptionCodec;
@@ -0,0 +1,7 @@
1
+ import { ArrayCodec } from './array-codec';
2
+ import { Either, EitherCodec } from './either-codec';
3
+ import { AssetOutput } from './asset-output-codec';
4
+ import { ContractOutput } from './contract-output-codec';
5
+ export type Output = Either<AssetOutput, ContractOutput>;
6
+ export declare const outputCodec: EitherCodec<AssetOutput, ContractOutput>;
7
+ export declare const outputsCodec: ArrayCodec<Either<AssetOutput, ContractOutput>>;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.outputsCodec = exports.outputCodec = void 0;
21
+ const array_codec_1 = require("./array-codec");
22
+ const either_codec_1 = require("./either-codec");
23
+ const asset_output_codec_1 = require("./asset-output-codec");
24
+ const contract_output_codec_1 = require("./contract-output-codec");
25
+ exports.outputCodec = new either_codec_1.EitherCodec(asset_output_codec_1.assetOutputCodec, contract_output_codec_1.contractOutputCodec);
26
+ exports.outputsCodec = new array_codec_1.ArrayCodec(exports.outputCodec);
@@ -0,0 +1,16 @@
1
+ /// <reference types="node" />
2
+ import { Parser } from 'binary-parser';
3
+ import { DecodedArray } from './array-codec';
4
+ import { Codec } from './codec';
5
+ import { DecodedMethod } from './method-codec';
6
+ import { OptionCodec } from './option-codec';
7
+ export interface Script {
8
+ methods: DecodedArray<DecodedMethod>;
9
+ }
10
+ export declare class ScriptCodec implements Codec<Script> {
11
+ parser: Parser;
12
+ encode(input: Script): Buffer;
13
+ decode(input: Buffer): Script;
14
+ }
15
+ export declare const scriptCodec: ScriptCodec;
16
+ export declare const statefulScriptCodecOpt: OptionCodec<Script>;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.statefulScriptCodecOpt = exports.scriptCodec = exports.ScriptCodec = void 0;
4
+ /*
5
+ Copyright 2018 - 2022 The Alephium Authors
6
+ This file is part of the alephium project.
7
+
8
+ The library is free software: you can redistribute it and/or modify
9
+ it under the terms of the GNU Lesser General Public License as published by
10
+ the Free Software Foundation, either version 3 of the License, or
11
+ (at your option) any later version.
12
+
13
+ The library is distributed in the hope that it will be useful,
14
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ GNU Lesser General Public License for more details.
17
+
18
+ You should have received a copy of the GNU Lesser General Public License
19
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
20
+ */
21
+ const binary_parser_1 = require("binary-parser");
22
+ const method_codec_1 = require("./method-codec");
23
+ const option_codec_1 = require("./option-codec");
24
+ class ScriptCodec {
25
+ constructor() {
26
+ this.parser = binary_parser_1.Parser.start().nest('methods', {
27
+ type: method_codec_1.methodsCodec.parser
28
+ });
29
+ }
30
+ encode(input) {
31
+ const script = method_codec_1.methodsCodec.encode(input.methods.value);
32
+ return Buffer.from(script);
33
+ }
34
+ decode(input) {
35
+ return this.parser.parse(input);
36
+ }
37
+ }
38
+ exports.ScriptCodec = ScriptCodec;
39
+ exports.scriptCodec = new ScriptCodec();
40
+ exports.statefulScriptCodecOpt = new option_codec_1.OptionCodec(exports.scriptCodec);
@@ -0,0 +1,14 @@
1
+ /// <reference types="node" />
2
+ import { Parser } from 'binary-parser';
3
+ import { ArrayCodec } from './array-codec';
4
+ import { Codec } from './codec';
5
+ export interface Signature {
6
+ value: Buffer;
7
+ }
8
+ export declare class SignatureCodec implements Codec<Signature> {
9
+ parser: Parser;
10
+ encode(input: Signature): Buffer;
11
+ decode(input: Buffer): Signature;
12
+ }
13
+ export declare const signatureCodec: SignatureCodec;
14
+ export declare const signaturesCodec: ArrayCodec<Signature>;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.signaturesCodec = exports.signatureCodec = exports.SignatureCodec = void 0;
4
+ /*
5
+ Copyright 2018 - 2022 The Alephium Authors
6
+ This file is part of the alephium project.
7
+
8
+ The library is free software: you can redistribute it and/or modify
9
+ it under the terms of the GNU Lesser General Public License as published by
10
+ the Free Software Foundation, either version 3 of the License, or
11
+ (at your option) any later version.
12
+
13
+ The library is distributed in the hope that it will be useful,
14
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ GNU Lesser General Public License for more details.
17
+
18
+ You should have received a copy of the GNU Lesser General Public License
19
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
20
+ */
21
+ const binary_parser_1 = require("binary-parser");
22
+ const array_codec_1 = require("./array-codec");
23
+ class SignatureCodec {
24
+ constructor() {
25
+ this.parser = binary_parser_1.Parser.start().buffer('value', { length: 64 });
26
+ }
27
+ encode(input) {
28
+ return input.value;
29
+ }
30
+ decode(input) {
31
+ return this.parser.parse(input);
32
+ }
33
+ }
34
+ exports.SignatureCodec = SignatureCodec;
35
+ exports.signatureCodec = new SignatureCodec();
36
+ exports.signaturesCodec = new array_codec_1.ArrayCodec(exports.signatureCodec);
@@ -0,0 +1,9 @@
1
+ /// <reference types="node" />
2
+ import { Parser } from 'binary-parser';
3
+ import { Codec } from './codec';
4
+ export declare class SignedIntCodec implements Codec<number> {
5
+ parser: Parser;
6
+ encode(value: number): Buffer;
7
+ decode(bytes: Buffer): number;
8
+ }
9
+ export declare const signedIntCodec: SignedIntCodec;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.signedIntCodec = exports.SignedIntCodec = void 0;
4
+ /*
5
+ Copyright 2018 - 2022 The Alephium Authors
6
+ This file is part of the alephium project.
7
+
8
+ The library is free software: you can redistribute it and/or modify
9
+ it under the terms of the GNU Lesser General Public License as published by
10
+ the Free Software Foundation, either version 3 of the License, or
11
+ (at your option) any later version.
12
+
13
+ The library is distributed in the hope that it will be useful,
14
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ GNU Lesser General Public License for more details.
17
+
18
+ You should have received a copy of the GNU Lesser General Public License
19
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
20
+ */
21
+ const binary_parser_1 = require("binary-parser");
22
+ const codec_1 = require("./codec");
23
+ class SignedIntCodec {
24
+ constructor() {
25
+ this.parser = binary_parser_1.Parser.start().buffer('value', {
26
+ length: 4
27
+ });
28
+ }
29
+ encode(value) {
30
+ return Buffer.from([(value >> 24) & 0xff, (value >> 16) & 0xff, (value >> 8) & 0xff, value & 0xff]);
31
+ }
32
+ decode(bytes) {
33
+ (0, codec_1.assert)(bytes.length === 4, 'Length should be 4');
34
+ return ((bytes[0] & 0xff) << 24) | ((bytes[1] & 0xff) << 16) | ((bytes[2] & 0xff) << 8) | (bytes[3] & 0xff);
35
+ }
36
+ }
37
+ exports.SignedIntCodec = SignedIntCodec;
38
+ exports.signedIntCodec = new SignedIntCodec();
@@ -0,0 +1,16 @@
1
+ /// <reference types="node" />
2
+ import { Parser } from 'binary-parser';
3
+ import { DecodedCompactInt } from './compact-int-codec';
4
+ import { Codec } from './codec';
5
+ import { ArrayCodec } from './array-codec';
6
+ export interface Token {
7
+ tokenId: Buffer;
8
+ amount: DecodedCompactInt;
9
+ }
10
+ export declare class TokenCodec implements Codec<Token> {
11
+ parser: Parser;
12
+ encode(input: Token): Buffer;
13
+ decode(input: Buffer): Token;
14
+ }
15
+ export declare const tokenCodec: TokenCodec;
16
+ export declare const tokensCodec: ArrayCodec<Token>;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.tokensCodec = exports.tokenCodec = exports.TokenCodec = void 0;
4
+ /*
5
+ Copyright 2018 - 2022 The Alephium Authors
6
+ This file is part of the alephium project.
7
+
8
+ The library is free software: you can redistribute it and/or modify
9
+ it under the terms of the GNU Lesser General Public License as published by
10
+ the Free Software Foundation, either version 3 of the License, or
11
+ (at your option) any later version.
12
+
13
+ The library is distributed in the hope that it will be useful,
14
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ GNU Lesser General Public License for more details.
17
+
18
+ You should have received a copy of the GNU Lesser General Public License
19
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
20
+ */
21
+ const binary_parser_1 = require("binary-parser");
22
+ const compact_int_codec_1 = require("./compact-int-codec");
23
+ const array_codec_1 = require("./array-codec");
24
+ class TokenCodec {
25
+ constructor() {
26
+ this.parser = binary_parser_1.Parser.start()
27
+ .buffer('tokenId', {
28
+ length: 32
29
+ })
30
+ .nest('amount', {
31
+ type: compact_int_codec_1.compactUnsignedIntCodec.parser
32
+ });
33
+ }
34
+ encode(input) {
35
+ const tokenId = input.tokenId;
36
+ const amount = Buffer.from(compact_int_codec_1.compactUnsignedIntCodec.encode(input.amount));
37
+ return Buffer.concat([tokenId, amount]);
38
+ }
39
+ decode(input) {
40
+ return this.parser.parse(input);
41
+ }
42
+ }
43
+ exports.TokenCodec = TokenCodec;
44
+ exports.tokenCodec = new TokenCodec();
45
+ exports.tokensCodec = new array_codec_1.ArrayCodec(exports.tokenCodec);
@@ -0,0 +1,27 @@
1
+ /// <reference types="node" />
2
+ import { Parser } from 'binary-parser';
3
+ import { DecodedArray } from './array-codec';
4
+ import { UnsignedTx } from './unsigned-tx-codec';
5
+ import { Signature } from './signature-codec';
6
+ import { ContractOutputRef } from './contract-output-ref-codec';
7
+ import { Transaction as ApiTransaction } from '../api/api-alephium';
8
+ import { Codec } from './codec';
9
+ import { Output } from './output-codec';
10
+ export interface Transaction {
11
+ unsigned: UnsignedTx;
12
+ scriptExecutionOk: number;
13
+ contractInputs: DecodedArray<ContractOutputRef>;
14
+ generatedOutputs: DecodedArray<Output>;
15
+ inputSignatures: DecodedArray<Signature>;
16
+ scriptSignatures: DecodedArray<Signature>;
17
+ }
18
+ export declare class TransactionCodec implements Codec<Transaction> {
19
+ parser: Parser;
20
+ encode(decodedTx: Transaction): Buffer;
21
+ decode(input: Buffer): Transaction;
22
+ encodeApiTransaction(input: ApiTransaction): Buffer;
23
+ decodeApiTransaction(input: Buffer): ApiTransaction;
24
+ static toApiTransaction(transaction: Transaction): ApiTransaction;
25
+ static fromApiTransaction(tx: ApiTransaction): Transaction;
26
+ }
27
+ export declare const transactionCodec: TransactionCodec;
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.transactionCodec = exports.TransactionCodec = void 0;
21
+ const binary_parser_1 = require("binary-parser");
22
+ const unsigned_tx_codec_1 = require("./unsigned-tx-codec");
23
+ const signature_codec_1 = require("./signature-codec");
24
+ const contract_output_ref_codec_1 = require("./contract-output-ref-codec");
25
+ const asset_output_codec_1 = require("./asset-output-codec");
26
+ const contract_output_codec_1 = require("./contract-output-codec");
27
+ const __1 = require("..");
28
+ const output_codec_1 = require("./output-codec");
29
+ class TransactionCodec {
30
+ constructor() {
31
+ this.parser = new binary_parser_1.Parser()
32
+ .nest('unsigned', {
33
+ type: unsigned_tx_codec_1.unsignedTxCodec.parser
34
+ })
35
+ .uint8('scriptExecutionOk')
36
+ .nest('contractInputs', {
37
+ type: contract_output_ref_codec_1.contractOutputRefsCodec.parser
38
+ })
39
+ .nest('generatedOutputs', {
40
+ type: output_codec_1.outputsCodec.parser
41
+ })
42
+ .nest('inputSignatures', {
43
+ type: signature_codec_1.signaturesCodec.parser
44
+ })
45
+ .nest('scriptSignatures', {
46
+ type: signature_codec_1.signaturesCodec.parser
47
+ });
48
+ }
49
+ encode(decodedTx) {
50
+ return Buffer.concat([
51
+ unsigned_tx_codec_1.unsignedTxCodec.encode(decodedTx.unsigned),
52
+ Buffer.from([decodedTx.scriptExecutionOk]),
53
+ Buffer.from([...contract_output_ref_codec_1.contractOutputRefsCodec.encode(decodedTx.contractInputs.value)]),
54
+ Buffer.from([...output_codec_1.outputsCodec.encode(decodedTx.generatedOutputs.value)]),
55
+ Buffer.from([...signature_codec_1.signaturesCodec.encode(decodedTx.inputSignatures.value)]),
56
+ Buffer.from([...signature_codec_1.signaturesCodec.encode(decodedTx.scriptSignatures.value)])
57
+ ]);
58
+ }
59
+ decode(input) {
60
+ return this.parser.parse(input);
61
+ }
62
+ encodeApiTransaction(input) {
63
+ const decodedTx = TransactionCodec.fromApiTransaction(input);
64
+ return this.encode(decodedTx);
65
+ }
66
+ decodeApiTransaction(input) {
67
+ const decodedTx = this.parser.parse(input);
68
+ return TransactionCodec.toApiTransaction(decodedTx);
69
+ }
70
+ static toApiTransaction(transaction) {
71
+ const txId = unsigned_tx_codec_1.UnsignedTxCodec.txId(transaction.unsigned);
72
+ const unsigned = unsigned_tx_codec_1.UnsignedTxCodec.toApiUnsignedTx(transaction.unsigned);
73
+ const scriptExecutionOk = !!transaction.scriptExecutionOk;
74
+ const contractInputs = transaction.contractInputs.value.map((contractInput) => {
75
+ const hint = contractInput.hint;
76
+ const key = contractInput.key.toString('hex');
77
+ return { hint, key };
78
+ });
79
+ const txIdBytes = (0, __1.hexToBinUnsafe)(txId);
80
+ const generatedOutputs = transaction.generatedOutputs.value.map((output, index) => {
81
+ if (output.either === 0) {
82
+ const fixedAssetOutput = asset_output_codec_1.AssetOutputCodec.toFixedAssetOutput(txIdBytes, output.value, index);
83
+ return { ...fixedAssetOutput, type: 'AssetOutput' };
84
+ }
85
+ else {
86
+ return contract_output_codec_1.ContractOutputCodec.convertToApiContractOutput(txIdBytes, output.value, index);
87
+ }
88
+ });
89
+ const inputSignatures = transaction.inputSignatures.value.map((signature) => signature.value.toString('hex'));
90
+ const scriptSignatures = transaction.scriptSignatures.value.map((signature) => signature.value.toString('hex'));
91
+ return { unsigned, scriptExecutionOk, contractInputs, generatedOutputs, inputSignatures, scriptSignatures };
92
+ }
93
+ static fromApiTransaction(tx) {
94
+ const unsigned = unsigned_tx_codec_1.UnsignedTxCodec.fromApiUnsignedTx(tx.unsigned);
95
+ const scriptExecutionOk = tx.scriptExecutionOk ? 1 : 0;
96
+ const contractInputs = tx.contractInputs.map((contractInput) => {
97
+ return { hint: contractInput.hint, key: Buffer.from(contractInput.key, 'hex') };
98
+ });
99
+ const generatedOutputs = tx.generatedOutputs.map((output) => {
100
+ if (output.type === 'AssetOutput') {
101
+ return output_codec_1.outputCodec.fromLeft(asset_output_codec_1.AssetOutputCodec.fromFixedAssetOutput(output));
102
+ }
103
+ else if (output.type === 'ContractOutput') {
104
+ return output_codec_1.outputCodec.fromRight(contract_output_codec_1.ContractOutputCodec.convertToOutput(output));
105
+ }
106
+ else {
107
+ throw new Error('Invalid output type');
108
+ }
109
+ });
110
+ const inputSignatures = tx.inputSignatures.map((signature) => {
111
+ return { value: Buffer.from(signature, 'hex') };
112
+ });
113
+ const scriptSignatures = tx.scriptSignatures.map((signature) => {
114
+ return { value: Buffer.from(signature, 'hex') };
115
+ });
116
+ return {
117
+ unsigned,
118
+ scriptExecutionOk,
119
+ contractInputs: contract_output_ref_codec_1.contractOutputRefsCodec.fromArray(contractInputs),
120
+ generatedOutputs: output_codec_1.outputsCodec.fromArray(generatedOutputs),
121
+ inputSignatures: signature_codec_1.signaturesCodec.fromArray(inputSignatures),
122
+ scriptSignatures: signature_codec_1.signaturesCodec.fromArray(scriptSignatures)
123
+ };
124
+ }
125
+ }
126
+ exports.TransactionCodec = TransactionCodec;
127
+ exports.transactionCodec = new TransactionCodec();
@@ -0,0 +1,40 @@
1
+ /// <reference types="node" />
2
+ import { Parser } from 'binary-parser';
3
+ import { DecodedArray } from './array-codec';
4
+ import { DecodedCompactInt } from './compact-int-codec';
5
+ import { Codec } from './codec';
6
+ import { Script } from './script-codec';
7
+ import { ByteString } from './bytestring-codec';
8
+ import { LockupScript } from './lockup-script-codec';
9
+ export interface P2PKH {
10
+ publicKey: Buffer;
11
+ }
12
+ export interface P2MPKH {
13
+ publicKeys: DecodedArray<{
14
+ publicKey: P2PKH;
15
+ index: DecodedCompactInt;
16
+ }>;
17
+ }
18
+ export interface Val {
19
+ type: number;
20
+ val: number | DecodedCompactInt | ByteString | LockupScript;
21
+ }
22
+ export interface P2SH {
23
+ script: Script;
24
+ params: DecodedArray<Val>;
25
+ }
26
+ export declare class P2SHCodec implements Codec<P2SH> {
27
+ parser: Parser;
28
+ encode(input: P2SH): Buffer;
29
+ decode(input: Buffer): P2SH;
30
+ }
31
+ export interface UnlockScript {
32
+ scriptType: number;
33
+ script: P2PKH | P2MPKH | P2SH;
34
+ }
35
+ export declare class UnlockScriptCodec implements Codec<UnlockScript> {
36
+ parser: Parser;
37
+ encode(input: UnlockScript): Buffer;
38
+ decode(input: Buffer): UnlockScript;
39
+ }
40
+ export declare const unlockScriptCodec: UnlockScriptCodec;