@alephium/web3 1.4.0 → 1.5.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 (106) 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/address/address.js +5 -5
  4. package/dist/src/api/api-alephium.d.ts +1 -1
  5. package/dist/src/api/api-alephium.js +1 -1
  6. package/dist/src/codec/array-codec.d.ts +4 -12
  7. package/dist/src/codec/array-codec.js +15 -28
  8. package/dist/src/codec/asset-output-codec.d.ts +6 -11
  9. package/dist/src/codec/asset-output-codec.js +32 -71
  10. package/dist/src/codec/bigint-codec.d.ts +2 -1
  11. package/dist/src/codec/bigint-codec.js +14 -2
  12. package/dist/src/codec/bytestring-codec.d.ts +6 -11
  13. package/dist/src/codec/bytestring-codec.js +9 -23
  14. package/dist/src/codec/codec.d.ts +54 -5
  15. package/dist/src/codec/codec.js +112 -14
  16. package/dist/src/codec/compact-int-codec.d.ts +65 -44
  17. package/dist/src/codec/compact-int-codec.js +222 -204
  18. package/dist/src/codec/contract-codec.d.ts +5 -8
  19. package/dist/src/codec/contract-codec.js +15 -29
  20. package/dist/src/codec/contract-output-codec.d.ts +4 -10
  21. package/dist/src/codec/contract-output-codec.js +20 -40
  22. package/dist/src/codec/contract-output-ref-codec.d.ts +2 -8
  23. package/dist/src/codec/contract-output-ref-codec.js +7 -17
  24. package/dist/src/codec/either-codec.d.ts +8 -15
  25. package/dist/src/codec/either-codec.js +5 -46
  26. package/dist/src/codec/index.d.ts +4 -3
  27. package/dist/src/codec/index.js +7 -4
  28. package/dist/src/codec/input-codec.d.ts +4 -10
  29. package/dist/src/codec/input-codec.js +11 -46
  30. package/dist/src/codec/instr-codec.d.ts +633 -40
  31. package/dist/src/codec/instr-codec.js +1040 -434
  32. package/dist/src/codec/int-as-4bytes-codec.d.ts +7 -0
  33. package/dist/src/codec/{signed-int-codec.js → int-as-4bytes-codec.js} +6 -12
  34. package/dist/src/codec/lockup-script-codec.d.ts +23 -26
  35. package/dist/src/codec/lockup-script-codec.js +12 -58
  36. package/dist/src/codec/method-codec.d.ts +6 -18
  37. package/dist/src/codec/method-codec.js +20 -48
  38. package/dist/src/codec/option-codec.d.ts +8 -13
  39. package/dist/src/codec/option-codec.js +14 -32
  40. package/dist/src/codec/output-codec.d.ts +2 -2
  41. package/dist/src/codec/output-codec.js +1 -1
  42. package/dist/src/codec/reader.d.ts +8 -0
  43. package/dist/src/codec/reader.js +48 -0
  44. package/dist/src/codec/script-codec.d.ts +6 -14
  45. package/dist/src/codec/script-codec.js +6 -22
  46. package/dist/src/codec/signature-codec.d.ts +4 -12
  47. package/dist/src/codec/signature-codec.js +3 -15
  48. package/dist/src/codec/timestamp-codec.d.ts +8 -0
  49. package/dist/src/codec/timestamp-codec.js +39 -0
  50. package/dist/src/codec/token-codec.d.ts +3 -10
  51. package/dist/src/codec/token-codec.js +6 -24
  52. package/dist/src/codec/transaction-codec.d.ts +6 -11
  53. package/dist/src/codec/transaction-codec.js +24 -49
  54. package/dist/src/codec/unlock-script-codec.d.ts +25 -36
  55. package/dist/src/codec/unlock-script-codec.js +26 -147
  56. package/dist/src/codec/unsigned-tx-codec.d.ts +8 -14
  57. package/dist/src/codec/unsigned-tx-codec.js +24 -66
  58. package/dist/src/codec/val.d.ts +27 -0
  59. package/dist/src/codec/val.js +33 -0
  60. package/dist/src/contract/contract.js +6 -6
  61. package/dist/src/contract/index.d.ts +1 -0
  62. package/dist/src/contract/index.js +1 -0
  63. package/dist/src/contract/ralph.d.ts +0 -4
  64. package/dist/src/contract/ralph.js +50 -179
  65. package/dist/src/contract/script-simulator.d.ts +27 -0
  66. package/dist/src/contract/script-simulator.js +757 -0
  67. package/dist/src/exchange/exchange.js +1 -1
  68. package/package.json +3 -4
  69. package/src/address/address.ts +8 -8
  70. package/src/api/api-alephium.ts +1 -1
  71. package/src/codec/array-codec.ts +16 -34
  72. package/src/codec/asset-output-codec.ts +38 -83
  73. package/src/codec/bigint-codec.ts +16 -2
  74. package/src/codec/bytestring-codec.ts +10 -28
  75. package/src/codec/codec.ts +121 -15
  76. package/src/codec/compact-int-codec.ts +230 -207
  77. package/src/codec/contract-codec.ts +20 -33
  78. package/src/codec/contract-output-codec.ts +22 -48
  79. package/src/codec/contract-output-ref-codec.ts +6 -17
  80. package/src/codec/either-codec.ts +4 -53
  81. package/src/codec/index.ts +4 -3
  82. package/src/codec/input-codec.ts +14 -36
  83. package/src/codec/instr-codec.ts +1229 -455
  84. package/src/codec/{signed-int-codec.ts → int-as-4bytes-codec.ts} +6 -10
  85. package/src/codec/lockup-script-codec.ts +28 -76
  86. package/src/codec/method-codec.ts +23 -61
  87. package/src/codec/option-codec.ts +13 -36
  88. package/src/codec/output-codec.ts +2 -2
  89. package/src/codec/reader.ts +56 -0
  90. package/src/codec/script-codec.ts +9 -31
  91. package/src/codec/signature-codec.ts +3 -18
  92. package/src/codec/timestamp-codec.ts +42 -0
  93. package/src/codec/token-codec.ts +7 -26
  94. package/src/codec/transaction-codec.ts +29 -58
  95. package/src/codec/unlock-script-codec.ts +44 -171
  96. package/src/codec/unsigned-tx-codec.ts +34 -63
  97. package/src/codec/val.ts +40 -0
  98. package/src/contract/contract.ts +9 -13
  99. package/src/contract/index.ts +1 -0
  100. package/src/contract/ralph.ts +76 -172
  101. package/src/contract/script-simulator.ts +838 -0
  102. package/src/exchange/exchange.ts +1 -1
  103. package/dist/src/codec/long-codec.d.ts +0 -8
  104. package/dist/src/codec/long-codec.js +0 -55
  105. package/dist/src/codec/signed-int-codec.d.ts +0 -8
  106. package/src/codec/long-codec.ts +0 -58
@@ -0,0 +1,7 @@
1
+ import { Codec } from './codec';
2
+ import { Reader } from './reader';
3
+ export declare class IntAs4BytesCodec extends Codec<number> {
4
+ encode(value: number): Uint8Array;
5
+ _decode(input: Reader): number;
6
+ }
7
+ export declare const intAs4BytesCodec: IntAs4BytesCodec;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.signedIntCodec = exports.SignedIntCodec = void 0;
3
+ exports.intAs4BytesCodec = exports.IntAs4BytesCodec = void 0;
4
4
  /*
5
5
  Copyright 2018 - 2022 The Alephium Authors
6
6
  This file is part of the alephium project.
@@ -18,21 +18,15 @@ GNU Lesser General Public License for more details.
18
18
  You should have received a copy of the GNU Lesser General Public License
19
19
  along with the library. If not, see <http://www.gnu.org/licenses/>.
20
20
  */
21
- const binary_parser_1 = require("binary-parser");
22
21
  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
- }
22
+ class IntAs4BytesCodec extends codec_1.Codec {
29
23
  encode(value) {
30
24
  return new Uint8Array([(value >> 24) & 0xff, (value >> 16) & 0xff, (value >> 8) & 0xff, value & 0xff]);
31
25
  }
32
- decode(bytes) {
33
- (0, codec_1.assert)(bytes.length === 4, 'Length should be 4');
26
+ _decode(input) {
27
+ const bytes = input.consumeBytes(4);
34
28
  return ((bytes[0] & 0xff) << 24) | ((bytes[1] & 0xff) << 16) | ((bytes[2] & 0xff) << 8) | (bytes[3] & 0xff);
35
29
  }
36
30
  }
37
- exports.SignedIntCodec = SignedIntCodec;
38
- exports.signedIntCodec = new SignedIntCodec();
31
+ exports.IntAs4BytesCodec = IntAs4BytesCodec;
32
+ exports.intAs4BytesCodec = new IntAs4BytesCodec();
@@ -1,27 +1,24 @@
1
- import { Parser } from 'binary-parser';
2
- import { DecodedCompactInt } from './compact-int-codec';
3
- import { Codec } from './codec';
4
- import { DecodedArray } from './array-codec';
5
- export interface PublicKeyHash {
6
- publicKeyHash: Uint8Array;
1
+ import { EnumCodec } from './codec';
2
+ export type PublicKeyHash = Uint8Array;
3
+ export type P2PKH = Uint8Array;
4
+ export type P2SH = Uint8Array;
5
+ export type P2C = Uint8Array;
6
+ export declare const p2cCodec: import("./codec").FixedSizeCodec;
7
+ export interface P2MPKH {
8
+ publicKeyHashes: PublicKeyHash[];
9
+ m: number;
7
10
  }
8
- export interface MultiSig {
9
- publicKeyHashes: DecodedArray<PublicKeyHash>;
10
- m: DecodedCompactInt;
11
- }
12
- export interface P2SH {
13
- scriptHash: Uint8Array;
14
- }
15
- export interface P2C {
16
- contractId: Uint8Array;
17
- }
18
- export interface LockupScript {
19
- scriptType: number;
20
- script: PublicKeyHash | MultiSig | P2SH | P2C;
21
- }
22
- export declare class LockupScriptCodec implements Codec<LockupScript> {
23
- parser: Parser;
24
- encode(input: LockupScript): Uint8Array;
25
- decode(input: Uint8Array): LockupScript;
26
- }
27
- export declare const lockupScriptCodec: LockupScriptCodec;
11
+ export type LockupScript = {
12
+ kind: 'P2PKH';
13
+ value: P2PKH;
14
+ } | {
15
+ kind: 'P2MPKH';
16
+ value: P2MPKH;
17
+ } | {
18
+ kind: 'P2SH';
19
+ value: P2SH;
20
+ } | {
21
+ kind: 'P2C';
22
+ value: P2C;
23
+ };
24
+ export declare const lockupScriptCodec: EnumCodec<LockupScript>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.lockupScriptCodec = exports.LockupScriptCodec = void 0;
3
+ exports.lockupScriptCodec = exports.p2cCodec = void 0;
4
4
  /*
5
5
  Copyright 2018 - 2022 The Alephium Authors
6
6
  This file is part of the alephium project.
@@ -18,63 +18,17 @@ GNU Lesser General Public License for more details.
18
18
  You should have received a copy of the GNU Lesser General Public License
19
19
  along with the library. If not, see <http://www.gnu.org/licenses/>.
20
20
  */
21
- const binary_parser_1 = require("binary-parser");
22
21
  const compact_int_codec_1 = require("./compact-int-codec");
23
22
  const codec_1 = require("./codec");
24
23
  const array_codec_1 = require("./array-codec");
25
- class PublicKeyHashCodec {
26
- constructor() {
27
- this.parser = (0, codec_1.fixedSizeBytes)('publicKeyHash', 32);
28
- }
29
- encode(input) {
30
- return input.publicKeyHash;
31
- }
32
- decode(input) {
33
- return this.parser.parse(input);
34
- }
35
- }
36
- const publicKeyHashCodec = new PublicKeyHashCodec();
37
- const publicKeyHashesCodec = new array_codec_1.ArrayCodec(publicKeyHashCodec);
38
- const multiSigParser = binary_parser_1.Parser.start()
39
- .nest('publicKeyHashes', { type: publicKeyHashesCodec.parser })
40
- .nest('m', { type: compact_int_codec_1.compactSignedIntCodec.parser });
41
- class LockupScriptCodec {
42
- constructor() {
43
- this.parser = binary_parser_1.Parser.start()
44
- .uint8('scriptType')
45
- .choice('script', {
46
- tag: 'scriptType',
47
- choices: {
48
- 0: publicKeyHashCodec.parser,
49
- 1: multiSigParser,
50
- 2: binary_parser_1.Parser.start().buffer('scriptHash', { length: 32 }),
51
- 3: binary_parser_1.Parser.start().buffer('contractId', { length: 32 })
52
- }
53
- });
54
- }
55
- encode(input) {
56
- const result = [input.scriptType];
57
- if (input.scriptType === 0) {
58
- result.push(...input.script.publicKeyHash);
59
- }
60
- else if (input.scriptType === 1) {
61
- result.push(...publicKeyHashesCodec.encode(input.script.publicKeyHashes.value));
62
- result.push(...compact_int_codec_1.compactSignedIntCodec.encode(input.script.m));
63
- }
64
- else if (input.scriptType === 2) {
65
- result.push(...input.script.scriptHash);
66
- }
67
- else if (input.scriptType === 3) {
68
- result.push(...input.script.contractId);
69
- }
70
- else {
71
- throw new Error(`Unsupported script type: ${input.scriptType}`);
72
- }
73
- return new Uint8Array(result);
74
- }
75
- decode(input) {
76
- return this.parser.parse(input);
77
- }
78
- }
79
- exports.LockupScriptCodec = LockupScriptCodec;
80
- exports.lockupScriptCodec = new LockupScriptCodec();
24
+ exports.p2cCodec = codec_1.byte32Codec;
25
+ const p2mpkhCodec = new codec_1.ObjectCodec({
26
+ publicKeyHashes: new array_codec_1.ArrayCodec(codec_1.byte32Codec),
27
+ m: compact_int_codec_1.i32Codec
28
+ });
29
+ exports.lockupScriptCodec = new codec_1.EnumCodec('lockup script', {
30
+ P2PKH: codec_1.byte32Codec,
31
+ P2MPKH: p2mpkhCodec,
32
+ P2SH: codec_1.byte32Codec,
33
+ P2C: codec_1.byte32Codec
34
+ });
@@ -1,16 +1,7 @@
1
- import { Parser } from 'binary-parser';
2
- import { ArrayCodec, DecodedArray } from './array-codec';
3
- import { DecodedCompactInt } from './compact-int-codec';
1
+ import { ArrayCodec } from './array-codec';
4
2
  import { Codec } from './codec';
5
3
  import { Instr } from './instr-codec';
6
- export interface DecodedMethod {
7
- isPublic: number;
8
- assetModifier: number;
9
- argsLength: DecodedCompactInt;
10
- localsLength: DecodedCompactInt;
11
- returnLength: DecodedCompactInt;
12
- instrs: DecodedArray<Instr>;
13
- }
4
+ import { Reader } from './reader';
14
5
  export interface Method {
15
6
  isPublic: boolean;
16
7
  usePreapprovedAssets: boolean;
@@ -21,12 +12,9 @@ export interface Method {
21
12
  returnLength: number;
22
13
  instrs: Instr[];
23
14
  }
24
- export declare class MethodCodec implements Codec<DecodedMethod> {
25
- parser: Parser;
26
- encode(input: DecodedMethod): Uint8Array;
27
- decode(input: Uint8Array): DecodedMethod;
28
- static toMethod(decodedMethod: DecodedMethod): Method;
29
- static fromMethod(method: Method): DecodedMethod;
15
+ export declare class MethodCodec extends Codec<Method> {
16
+ encode(method: Method): Uint8Array;
17
+ _decode(input: Reader): Method;
30
18
  }
31
19
  export declare const methodCodec: MethodCodec;
32
- export declare const methodsCodec: ArrayCodec<DecodedMethod>;
20
+ export declare const methodsCodec: ArrayCodec<Method>;
@@ -18,10 +18,11 @@ GNU Lesser General Public License for more details.
18
18
  You should have received a copy of the GNU Lesser General Public License
19
19
  along with the library. If not, see <http://www.gnu.org/licenses/>.
20
20
  */
21
- const binary_parser_1 = require("binary-parser");
22
21
  const array_codec_1 = require("./array-codec");
23
22
  const compact_int_codec_1 = require("./compact-int-codec");
23
+ const codec_1 = require("./codec");
24
24
  const instr_codec_1 = require("./instr-codec");
25
+ const utils_1 = require("../utils");
25
26
  function decodeAssetModifier(encoded) {
26
27
  const usePayToContractOnly = (encoded & 4) !== 0;
27
28
  switch (encoded & 3) {
@@ -47,54 +48,25 @@ function encodeAssetModifier(arg) {
47
48
  : 3;
48
49
  return encoded | (arg.usePayToContractOnly ? 4 : 0);
49
50
  }
50
- class MethodCodec {
51
- constructor() {
52
- this.parser = binary_parser_1.Parser.start()
53
- .uint8('isPublic')
54
- .uint8('assetModifier')
55
- .nest('argsLength', {
56
- type: compact_int_codec_1.compactSignedIntCodec.parser
57
- })
58
- .nest('localsLength', {
59
- type: compact_int_codec_1.compactSignedIntCodec.parser
60
- })
61
- .nest('returnLength', {
62
- type: compact_int_codec_1.compactSignedIntCodec.parser
63
- })
64
- .nest('instrs', {
65
- type: instr_codec_1.instrsCodec.parser
66
- });
51
+ class MethodCodec extends codec_1.Codec {
52
+ encode(method) {
53
+ const bytes = [];
54
+ bytes.push(codec_1.boolCodec.encode(method.isPublic));
55
+ bytes.push(new Uint8Array([encodeAssetModifier(method)]));
56
+ bytes.push(compact_int_codec_1.i32Codec.encode(method.argsLength));
57
+ bytes.push(compact_int_codec_1.i32Codec.encode(method.localsLength));
58
+ bytes.push(compact_int_codec_1.i32Codec.encode(method.returnLength));
59
+ bytes.push(instr_codec_1.instrsCodec.encode(method.instrs));
60
+ return (0, utils_1.concatBytes)(bytes);
67
61
  }
68
- encode(input) {
69
- const result = [input.isPublic, input.assetModifier];
70
- result.push(...compact_int_codec_1.compactSignedIntCodec.encode(input.argsLength));
71
- result.push(...compact_int_codec_1.compactSignedIntCodec.encode(input.localsLength));
72
- result.push(...compact_int_codec_1.compactSignedIntCodec.encode(input.returnLength));
73
- result.push(...instr_codec_1.instrsCodec.encode(input.instrs.value));
74
- return new Uint8Array(result);
75
- }
76
- decode(input) {
77
- return this.parser.parse(input);
78
- }
79
- static toMethod(decodedMethod) {
80
- return {
81
- isPublic: decodedMethod.isPublic === 1,
82
- ...decodeAssetModifier(decodedMethod.assetModifier),
83
- argsLength: compact_int_codec_1.compactSignedIntCodec.toI32(decodedMethod.argsLength),
84
- localsLength: compact_int_codec_1.compactSignedIntCodec.toI32(decodedMethod.localsLength),
85
- returnLength: compact_int_codec_1.compactSignedIntCodec.toI32(decodedMethod.returnLength),
86
- instrs: decodedMethod.instrs.value
87
- };
88
- }
89
- static fromMethod(method) {
90
- return {
91
- isPublic: method.isPublic ? 1 : 0,
92
- assetModifier: encodeAssetModifier(method),
93
- argsLength: compact_int_codec_1.compactSignedIntCodec.fromI32(method.argsLength),
94
- localsLength: compact_int_codec_1.compactSignedIntCodec.fromI32(method.localsLength),
95
- returnLength: compact_int_codec_1.compactSignedIntCodec.fromI32(method.returnLength),
96
- instrs: instr_codec_1.instrsCodec.fromArray(method.instrs)
97
- };
62
+ _decode(input) {
63
+ const isPublic = codec_1.boolCodec._decode(input);
64
+ const assetModifier = decodeAssetModifier(codec_1.byteCodec._decode(input));
65
+ const argsLength = compact_int_codec_1.i32Codec._decode(input);
66
+ const localsLength = compact_int_codec_1.i32Codec._decode(input);
67
+ const returnLength = compact_int_codec_1.i32Codec._decode(input);
68
+ const instrs = instr_codec_1.instrsCodec._decode(input);
69
+ return { ...assetModifier, isPublic, argsLength, localsLength, returnLength, instrs };
98
70
  }
99
71
  }
100
72
  exports.MethodCodec = MethodCodec;
@@ -1,14 +1,9 @@
1
- import { Parser } from 'binary-parser';
2
1
  import { Codec } from './codec';
3
- export interface Option<T> {
4
- option: number;
5
- value?: T;
6
- }
7
- export declare class OptionCodec<T> implements Codec<Option<T>> {
8
- private childCodec;
9
- parser: Parser;
10
- constructor(childCodec: Codec<T>, parser?: Parser);
11
- encode(input: Option<T>): Uint8Array;
12
- decode(input: Uint8Array): Option<T>;
13
- fromBytes(input?: Uint8Array): Option<T>;
14
- }
2
+ export type Option<T> = {
3
+ kind: 'None';
4
+ value: undefined;
5
+ } | {
6
+ kind: 'Some';
7
+ value: T;
8
+ };
9
+ export declare function option<T>(codec: Codec<T>): Codec<Option<T>>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OptionCodec = void 0;
3
+ exports.option = void 0;
4
4
  /*
5
5
  Copyright 2018 - 2022 The Alephium Authors
6
6
  This file is part of the alephium project.
@@ -18,37 +18,19 @@ GNU Lesser General Public License for more details.
18
18
  You should have received a copy of the GNU Lesser General Public License
19
19
  along with the library. If not, see <http://www.gnu.org/licenses/>.
20
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;
21
+ const codec_1 = require("./codec");
22
+ const undefinedCodec = new (class extends codec_1.Codec {
23
+ encode() {
24
+ return new Uint8Array([]);
32
25
  }
33
- encode(input) {
34
- const result = [input.option];
35
- if (input.option === 1) {
36
- result.push(...this.childCodec.encode(input.value));
37
- }
38
- return new Uint8Array(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
- fromBytes(input) {
48
- return {
49
- option: input ? 1 : 0,
50
- value: input ? this.childCodec.decode(input) : undefined
51
- };
26
+ _decode() {
27
+ return undefined;
52
28
  }
29
+ })();
30
+ function option(codec) {
31
+ return new codec_1.EnumCodec('option', {
32
+ None: undefinedCodec,
33
+ Some: codec
34
+ });
53
35
  }
54
- exports.OptionCodec = OptionCodec;
36
+ exports.option = option;
@@ -1,7 +1,7 @@
1
1
  import { ArrayCodec } from './array-codec';
2
- import { Either, EitherCodec } from './either-codec';
2
+ import { Either } from './either-codec';
3
3
  import { AssetOutput } from './asset-output-codec';
4
4
  import { ContractOutput } from './contract-output-codec';
5
5
  export type Output = Either<AssetOutput, ContractOutput>;
6
- export declare const outputCodec: EitherCodec<AssetOutput, ContractOutput>;
6
+ export declare const outputCodec: import("./codec").Codec<Either<AssetOutput, ContractOutput>>;
7
7
  export declare const outputsCodec: ArrayCodec<Either<AssetOutput, ContractOutput>>;
@@ -22,5 +22,5 @@ const array_codec_1 = require("./array-codec");
22
22
  const either_codec_1 = require("./either-codec");
23
23
  const asset_output_codec_1 = require("./asset-output-codec");
24
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);
25
+ exports.outputCodec = (0, either_codec_1.either)('output', asset_output_codec_1.assetOutputCodec, contract_output_codec_1.contractOutputCodec);
26
26
  exports.outputsCodec = new array_codec_1.ArrayCodec(exports.outputCodec);
@@ -0,0 +1,8 @@
1
+ export declare class Reader {
2
+ private index;
3
+ private bytes;
4
+ constructor(bytes: Uint8Array);
5
+ consumeByte(): number;
6
+ consumeBytes(num: number): Uint8Array;
7
+ consumeAll(): Uint8Array;
8
+ }
@@ -0,0 +1,48 @@
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.Reader = void 0;
21
+ class Reader {
22
+ constructor(bytes) {
23
+ this.index = 0;
24
+ this.bytes = bytes;
25
+ }
26
+ consumeByte() {
27
+ if (this.index >= this.bytes.length) {
28
+ throw new Error(`Index out of range: unable to consume byte at index ${this.index}, data length: ${this.bytes.length}`);
29
+ }
30
+ const byte = this.bytes[`${this.index}`];
31
+ this.index += 1;
32
+ return byte;
33
+ }
34
+ consumeBytes(num) {
35
+ const from = this.index;
36
+ const to = this.index + num;
37
+ if (from > to || to > this.bytes.length) {
38
+ throw new Error(`Index out of range: unable to consume bytes from index ${from} to ${to}, data length: ${this.bytes.length}`);
39
+ }
40
+ const bytes = this.bytes.slice(from, to);
41
+ this.index = to;
42
+ return bytes;
43
+ }
44
+ consumeAll() {
45
+ return this.consumeBytes(this.bytes.length - this.index);
46
+ }
47
+ }
48
+ exports.Reader = Reader;
@@ -1,20 +1,12 @@
1
- import { Parser } from 'binary-parser';
2
- import { DecodedArray } from './array-codec';
3
1
  import { Codec } from './codec';
4
- import { DecodedMethod, Method } from './method-codec';
5
- import { OptionCodec } from './option-codec';
6
- export interface DecodedScript {
7
- methods: DecodedArray<DecodedMethod>;
8
- }
2
+ import { Method } from './method-codec';
3
+ import { Reader } from './reader';
9
4
  export interface Script {
10
5
  methods: Method[];
11
6
  }
12
- export declare class ScriptCodec implements Codec<DecodedScript> {
13
- parser: Parser;
14
- encode(input: DecodedScript): Uint8Array;
15
- decode(input: Uint8Array): DecodedScript;
16
- decodeScript(input: Uint8Array): Script;
17
- encodeScript(inputTxScript: Script): Uint8Array;
7
+ export declare class ScriptCodec extends Codec<Script> {
8
+ encode(input: Script): Uint8Array;
9
+ _decode(input: Reader): Script;
18
10
  }
19
11
  export declare const scriptCodec: ScriptCodec;
20
- export declare const statefulScriptCodecOpt: OptionCodec<DecodedScript>;
12
+ export declare const statefulScriptCodecOpt: Codec<import("./option-codec").Option<Script>>;
@@ -18,33 +18,17 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.statefulScriptCodecOpt = exports.scriptCodec = exports.ScriptCodec = void 0;
21
- const binary_parser_1 = require("binary-parser");
21
+ const codec_1 = require("./codec");
22
22
  const method_codec_1 = require("./method-codec");
23
23
  const option_codec_1 = require("./option-codec");
24
- const compact_int_codec_1 = require("./compact-int-codec");
25
- class ScriptCodec {
26
- constructor() {
27
- this.parser = binary_parser_1.Parser.start().nest('methods', {
28
- type: method_codec_1.methodsCodec.parser
29
- });
30
- }
24
+ class ScriptCodec extends codec_1.Codec {
31
25
  encode(input) {
32
- return method_codec_1.methodsCodec.encode(input.methods.value);
33
- }
34
- decode(input) {
35
- return this.parser.parse(input);
36
- }
37
- decodeScript(input) {
38
- const decodedTxScript = this.decode(input);
39
- const methods = decodedTxScript.methods.value.map((decodedMethod) => method_codec_1.MethodCodec.toMethod(decodedMethod));
40
- return { methods };
26
+ return method_codec_1.methodsCodec.encode(input.methods);
41
27
  }
42
- encodeScript(inputTxScript) {
43
- const methodLength = compact_int_codec_1.compactSignedIntCodec.fromI32(inputTxScript.methods.length);
44
- const decodedMethods = inputTxScript.methods.map((method) => method_codec_1.MethodCodec.fromMethod(method));
45
- return this.encode({ methods: { value: decodedMethods, length: methodLength } });
28
+ _decode(input) {
29
+ return { methods: method_codec_1.methodsCodec._decode(input) };
46
30
  }
47
31
  }
48
32
  exports.ScriptCodec = ScriptCodec;
49
33
  exports.scriptCodec = new ScriptCodec();
50
- exports.statefulScriptCodecOpt = new option_codec_1.OptionCodec(exports.scriptCodec);
34
+ exports.statefulScriptCodecOpt = (0, option_codec_1.option)(exports.scriptCodec);
@@ -1,13 +1,5 @@
1
- import { Parser } from 'binary-parser';
2
1
  import { ArrayCodec } from './array-codec';
3
- import { Codec } from './codec';
4
- export interface Signature {
5
- value: Uint8Array;
6
- }
7
- export declare class SignatureCodec implements Codec<Signature> {
8
- parser: Parser;
9
- encode(input: Signature): Uint8Array;
10
- decode(input: Uint8Array): Signature;
11
- }
12
- export declare const signatureCodec: SignatureCodec;
13
- export declare const signaturesCodec: ArrayCodec<Signature>;
2
+ import { FixedSizeCodec } from './codec';
3
+ export type Signature = Uint8Array;
4
+ export declare const signatureCodec: FixedSizeCodec;
5
+ export declare const signaturesCodec: ArrayCodec<Uint8Array>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.signaturesCodec = exports.signatureCodec = exports.SignatureCodec = void 0;
3
+ exports.signaturesCodec = exports.signatureCodec = void 0;
4
4
  /*
5
5
  Copyright 2018 - 2022 The Alephium Authors
6
6
  This file is part of the alephium project.
@@ -18,19 +18,7 @@ GNU Lesser General Public License for more details.
18
18
  You should have received a copy of the GNU Lesser General Public License
19
19
  along with the library. If not, see <http://www.gnu.org/licenses/>.
20
20
  */
21
- const binary_parser_1 = require("binary-parser");
22
21
  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();
22
+ const codec_1 = require("./codec");
23
+ exports.signatureCodec = new codec_1.FixedSizeCodec(64);
36
24
  exports.signaturesCodec = new array_codec_1.ArrayCodec(exports.signatureCodec);
@@ -0,0 +1,8 @@
1
+ import { Codec } from './codec';
2
+ import { Reader } from './reader';
3
+ export declare class TimestampCodec extends Codec<bigint> {
4
+ static max: bigint;
5
+ encode(input: bigint): Uint8Array;
6
+ _decode(input: Reader): bigint;
7
+ }
8
+ export declare const timestampCodec: TimestampCodec;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.timestampCodec = exports.TimestampCodec = 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 utils_1 = require("../utils");
22
+ const codec_1 = require("./codec");
23
+ class TimestampCodec extends codec_1.Codec {
24
+ encode(input) {
25
+ (0, codec_1.assert)(input >= 0n && input < TimestampCodec.max, `Invalid timestamp: ${input}`);
26
+ const byteArray = new Uint8Array(8);
27
+ for (let index = 0; index < 8; index += 1) {
28
+ byteArray[`${index}`] = Number((input >> BigInt((7 - index) * 8)) & BigInt(0xff));
29
+ }
30
+ return byteArray;
31
+ }
32
+ _decode(input) {
33
+ const bytes = input.consumeBytes(8);
34
+ return BigInt(`0x${(0, utils_1.binToHex)(bytes)}`);
35
+ }
36
+ }
37
+ exports.TimestampCodec = TimestampCodec;
38
+ TimestampCodec.max = 1n << 64n;
39
+ exports.timestampCodec = new TimestampCodec();
@@ -1,15 +1,8 @@
1
- import { Parser } from 'binary-parser';
2
- import { DecodedCompactInt } from './compact-int-codec';
3
- import { Codec } from './codec';
1
+ import { ObjectCodec } from './codec';
4
2
  import { ArrayCodec } from './array-codec';
5
3
  export interface Token {
6
4
  tokenId: Uint8Array;
7
- amount: DecodedCompactInt;
5
+ amount: bigint;
8
6
  }
9
- export declare class TokenCodec implements Codec<Token> {
10
- parser: Parser;
11
- encode(input: Token): Uint8Array;
12
- decode(input: Uint8Array): Token;
13
- }
14
- export declare const tokenCodec: TokenCodec;
7
+ export declare const tokenCodec: ObjectCodec<Token>;
15
8
  export declare const tokensCodec: ArrayCodec<Token>;