@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
@@ -17,30 +17,12 @@ You should have received a copy of the GNU Lesser General Public License
17
17
  along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.tokensCodec = exports.tokenCodec = exports.TokenCodec = void 0;
21
- const binary_parser_1 = require("binary-parser");
20
+ exports.tokensCodec = exports.tokenCodec = void 0;
22
21
  const compact_int_codec_1 = require("./compact-int-codec");
22
+ const codec_1 = require("./codec");
23
23
  const array_codec_1 = require("./array-codec");
24
- const utils_1 = require("../utils");
25
- class TokenCodec {
26
- constructor() {
27
- this.parser = binary_parser_1.Parser.start()
28
- .buffer('tokenId', {
29
- length: 32
30
- })
31
- .nest('amount', {
32
- type: compact_int_codec_1.compactUnsignedIntCodec.parser
33
- });
34
- }
35
- encode(input) {
36
- const tokenId = input.tokenId;
37
- const amount = compact_int_codec_1.compactUnsignedIntCodec.encode(input.amount);
38
- return (0, utils_1.concatBytes)([tokenId, amount]);
39
- }
40
- decode(input) {
41
- return this.parser.parse(input);
42
- }
43
- }
44
- exports.TokenCodec = TokenCodec;
45
- exports.tokenCodec = new TokenCodec();
24
+ exports.tokenCodec = new codec_1.ObjectCodec({
25
+ tokenId: codec_1.byte32Codec,
26
+ amount: compact_int_codec_1.u256Codec
27
+ });
46
28
  exports.tokensCodec = new array_codec_1.ArrayCodec(exports.tokenCodec);
@@ -1,23 +1,18 @@
1
- import { Parser } from 'binary-parser';
2
- import { DecodedArray } from './array-codec';
3
1
  import { UnsignedTx } from './unsigned-tx-codec';
4
2
  import { Signature } from './signature-codec';
5
3
  import { ContractOutputRef } from './contract-output-ref-codec';
6
4
  import { Transaction as ApiTransaction } from '../api/api-alephium';
7
- import { Codec } from './codec';
5
+ import { ObjectCodec } from './codec';
8
6
  import { Output } from './output-codec';
9
7
  export interface Transaction {
10
8
  unsigned: UnsignedTx;
11
9
  scriptExecutionOk: number;
12
- contractInputs: DecodedArray<ContractOutputRef>;
13
- generatedOutputs: DecodedArray<Output>;
14
- inputSignatures: DecodedArray<Signature>;
15
- scriptSignatures: DecodedArray<Signature>;
10
+ contractInputs: ContractOutputRef[];
11
+ generatedOutputs: Output[];
12
+ inputSignatures: Signature[];
13
+ scriptSignatures: Signature[];
16
14
  }
17
- export declare class TransactionCodec implements Codec<Transaction> {
18
- parser: Parser;
19
- encode(decodedTx: Transaction): Uint8Array;
20
- decode(input: Uint8Array): Transaction;
15
+ export declare class TransactionCodec extends ObjectCodec<Transaction> {
21
16
  encodeApiTransaction(input: ApiTransaction): Uint8Array;
22
17
  decodeApiTransaction(input: Uint8Array): ApiTransaction;
23
18
  static toApiTransaction(transaction: Transaction): ApiTransaction;
@@ -18,67 +18,35 @@ 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.transactionCodec = exports.TransactionCodec = void 0;
21
- const binary_parser_1 = require("binary-parser");
22
21
  const unsigned_tx_codec_1 = require("./unsigned-tx-codec");
23
22
  const signature_codec_1 = require("./signature-codec");
24
23
  const contract_output_ref_codec_1 = require("./contract-output-ref-codec");
25
24
  const asset_output_codec_1 = require("./asset-output-codec");
26
25
  const contract_output_codec_1 = require("./contract-output-codec");
27
26
  const utils_1 = require("../utils");
27
+ const codec_1 = require("./codec");
28
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 (0, utils_1.concatBytes)([
51
- unsigned_tx_codec_1.unsignedTxCodec.encode(decodedTx.unsigned),
52
- new Uint8Array([decodedTx.scriptExecutionOk]),
53
- contract_output_ref_codec_1.contractOutputRefsCodec.encode(decodedTx.contractInputs.value),
54
- output_codec_1.outputsCodec.encode(decodedTx.generatedOutputs.value),
55
- signature_codec_1.signaturesCodec.encode(decodedTx.inputSignatures.value),
56
- signature_codec_1.signaturesCodec.encode(decodedTx.scriptSignatures.value)
57
- ]);
58
- }
59
- decode(input) {
60
- return this.parser.parse(input);
61
- }
29
+ class TransactionCodec extends codec_1.ObjectCodec {
62
30
  encodeApiTransaction(input) {
63
31
  const decodedTx = TransactionCodec.fromApiTransaction(input);
64
32
  return this.encode(decodedTx);
65
33
  }
66
34
  decodeApiTransaction(input) {
67
- const decodedTx = this.parser.parse(input);
35
+ const decodedTx = this.decode(input);
68
36
  return TransactionCodec.toApiTransaction(decodedTx);
69
37
  }
70
38
  static toApiTransaction(transaction) {
71
39
  const txId = unsigned_tx_codec_1.UnsignedTxCodec.txId(transaction.unsigned);
72
40
  const unsigned = unsigned_tx_codec_1.UnsignedTxCodec.toApiUnsignedTx(transaction.unsigned);
73
41
  const scriptExecutionOk = !!transaction.scriptExecutionOk;
74
- const contractInputs = transaction.contractInputs.value.map((contractInput) => {
42
+ const contractInputs = transaction.contractInputs.map((contractInput) => {
75
43
  const hint = contractInput.hint;
76
44
  const key = (0, utils_1.binToHex)(contractInput.key);
77
45
  return { hint, key };
78
46
  });
79
47
  const txIdBytes = (0, utils_1.hexToBinUnsafe)(txId);
80
- const generatedOutputs = transaction.generatedOutputs.value.map((output, index) => {
81
- if (output.either === 0) {
48
+ const generatedOutputs = transaction.generatedOutputs.map((output, index) => {
49
+ if (output.kind === 'Left') {
82
50
  const fixedAssetOutput = asset_output_codec_1.AssetOutputCodec.toFixedAssetOutput(txIdBytes, output.value, index);
83
51
  return { ...fixedAssetOutput, type: 'AssetOutput' };
84
52
  }
@@ -86,8 +54,8 @@ class TransactionCodec {
86
54
  return contract_output_codec_1.ContractOutputCodec.convertToApiContractOutput(txIdBytes, output.value, index);
87
55
  }
88
56
  });
89
- const inputSignatures = transaction.inputSignatures.value.map((signature) => (0, utils_1.binToHex)(signature.value));
90
- const scriptSignatures = transaction.scriptSignatures.value.map((signature) => (0, utils_1.binToHex)(signature.value));
57
+ const inputSignatures = transaction.inputSignatures.map((signature) => (0, utils_1.binToHex)(signature));
58
+ const scriptSignatures = transaction.scriptSignatures.map((signature) => (0, utils_1.binToHex)(signature));
91
59
  return { unsigned, scriptExecutionOk, contractInputs, generatedOutputs, inputSignatures, scriptSignatures };
92
60
  }
93
61
  static fromApiTransaction(tx) {
@@ -98,30 +66,37 @@ class TransactionCodec {
98
66
  });
99
67
  const generatedOutputs = tx.generatedOutputs.map((output) => {
100
68
  if (output.type === 'AssetOutput') {
101
- return output_codec_1.outputCodec.fromLeft(asset_output_codec_1.AssetOutputCodec.fromFixedAssetOutput(output));
69
+ return { kind: 'Left', value: asset_output_codec_1.AssetOutputCodec.fromFixedAssetOutput(output) };
102
70
  }
103
71
  else if (output.type === 'ContractOutput') {
104
- return output_codec_1.outputCodec.fromRight(contract_output_codec_1.ContractOutputCodec.convertToOutput(output));
72
+ return { kind: 'Right', value: contract_output_codec_1.ContractOutputCodec.convertToOutput(output) };
105
73
  }
106
74
  else {
107
75
  throw new Error('Invalid output type');
108
76
  }
109
77
  });
110
78
  const inputSignatures = tx.inputSignatures.map((signature) => {
111
- return { value: (0, utils_1.hexToBinUnsafe)(signature) };
79
+ return (0, utils_1.hexToBinUnsafe)(signature);
112
80
  });
113
81
  const scriptSignatures = tx.scriptSignatures.map((signature) => {
114
- return { value: (0, utils_1.hexToBinUnsafe)(signature) };
82
+ return (0, utils_1.hexToBinUnsafe)(signature);
115
83
  });
116
84
  return {
117
85
  unsigned,
118
86
  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)
87
+ contractInputs: contractInputs,
88
+ generatedOutputs: generatedOutputs,
89
+ inputSignatures: inputSignatures,
90
+ scriptSignatures: scriptSignatures
123
91
  };
124
92
  }
125
93
  }
126
94
  exports.TransactionCodec = TransactionCodec;
127
- exports.transactionCodec = new TransactionCodec();
95
+ exports.transactionCodec = new TransactionCodec({
96
+ unsigned: unsigned_tx_codec_1.unsignedTxCodec,
97
+ scriptExecutionOk: codec_1.byteCodec,
98
+ contractInputs: contract_output_ref_codec_1.contractOutputRefsCodec,
99
+ generatedOutputs: output_codec_1.outputsCodec,
100
+ inputSignatures: signature_codec_1.signaturesCodec,
101
+ scriptSignatures: signature_codec_1.signaturesCodec
102
+ });
@@ -1,39 +1,28 @@
1
- import { Parser } from 'binary-parser';
2
- import { DecodedArray } from './array-codec';
3
- import { DecodedCompactInt } from './compact-int-codec';
4
- import { Codec } from './codec';
5
- import { DecodedScript } from './script-codec';
6
- import { ByteString } from './bytestring-codec';
7
- import { LockupScript } from './lockup-script-codec';
8
- export interface P2PKH {
9
- publicKey: Uint8Array;
10
- }
11
- export interface P2MPKH {
12
- publicKeys: DecodedArray<{
13
- publicKey: P2PKH;
14
- index: DecodedCompactInt;
15
- }>;
16
- }
17
- export interface Val {
18
- type: number;
19
- val: number | DecodedCompactInt | ByteString | LockupScript;
1
+ import { EnumCodec } from './codec';
2
+ import { Script } from './script-codec';
3
+ import { Val } from './val';
4
+ export type P2PKH = Uint8Array;
5
+ export interface KeyWithIndex {
6
+ publicKey: P2PKH;
7
+ index: number;
20
8
  }
9
+ export type P2MPKH = KeyWithIndex[];
21
10
  export interface P2SH {
22
- script: DecodedScript;
23
- params: DecodedArray<Val>;
24
- }
25
- export declare class P2SHCodec implements Codec<P2SH> {
26
- parser: Parser;
27
- encode(input: P2SH): Uint8Array;
28
- decode(input: Uint8Array): P2SH;
29
- }
30
- export interface UnlockScript {
31
- scriptType: number;
32
- script: P2PKH | P2MPKH | P2SH;
33
- }
34
- export declare class UnlockScriptCodec implements Codec<UnlockScript> {
35
- parser: Parser;
36
- encode(input: UnlockScript): Uint8Array;
37
- decode(input: Uint8Array): UnlockScript;
11
+ script: Script;
12
+ params: Val[];
38
13
  }
39
- export declare const unlockScriptCodec: UnlockScriptCodec;
14
+ export type SameAsPrevious = 'SameAsPrevious';
15
+ export type UnlockScript = {
16
+ kind: 'P2PKH';
17
+ value: P2PKH;
18
+ } | {
19
+ kind: 'P2MPKH';
20
+ value: P2MPKH;
21
+ } | {
22
+ kind: 'P2SH';
23
+ value: P2SH;
24
+ } | {
25
+ kind: 'SameAsPrevious';
26
+ value: SameAsPrevious;
27
+ };
28
+ export declare const unlockScriptCodec: EnumCodec<UnlockScript>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.unlockScriptCodec = exports.UnlockScriptCodec = exports.P2SHCodec = void 0;
3
+ exports.unlockScriptCodec = void 0;
4
4
  /*
5
5
  Copyright 2018 - 2022 The Alephium Authors
6
6
  This file is part of the alephium project.
@@ -18,153 +18,32 @@ 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");
24
23
  const codec_1 = require("./codec");
25
24
  const script_codec_1 = require("./script-codec");
26
- const bytestring_codec_1 = require("./bytestring-codec");
27
- const lockup_script_codec_1 = require("./lockup-script-codec");
28
- const utils_1 = require("../utils");
29
- class P2PKHCodec {
30
- constructor() {
31
- this.parser = (0, codec_1.fixedSizeBytes)('publicKey', 33);
32
- }
33
- encode(input) {
34
- return input.publicKey;
35
- }
36
- decode(input) {
37
- return this.parser.parse(input);
38
- }
39
- }
40
- const p2pkhCodec = new P2PKHCodec();
41
- class P2MPKHCodec {
42
- constructor() {
43
- this.parser = binary_parser_1.Parser.start().nest('publicKeys', {
44
- type: array_codec_1.ArrayCodec.arrayParser(binary_parser_1.Parser.start()
45
- .nest('publicKey', { type: p2pkhCodec.parser })
46
- .nest('index', { type: compact_int_codec_1.compactUnsignedIntCodec.parser }))
47
- });
48
- }
49
- encode(input) {
50
- return (0, utils_1.concatBytes)([
51
- compact_int_codec_1.compactUnsignedIntCodec.encode(input.publicKeys.length),
52
- ...input.publicKeys.value.map((v) => {
53
- return (0, utils_1.concatBytes)([v.publicKey.publicKey, compact_int_codec_1.compactUnsignedIntCodec.encode(v.index)]);
54
- })
55
- ]);
56
- }
57
- decode(input) {
58
- return this.parser.parse(input);
59
- }
60
- }
61
- const p2mpkhCodec = new P2MPKHCodec();
62
- class ValCodec {
63
- constructor() {
64
- this.parser = binary_parser_1.Parser.start()
65
- .int8('type')
66
- .choice('val', {
67
- tag: 'type',
68
- choices: {
69
- 0x00: new binary_parser_1.Parser().uint8('value'),
70
- 0x01: compact_int_codec_1.compactSignedIntCodec.parser,
71
- 0x02: compact_int_codec_1.compactUnsignedIntCodec.parser,
72
- 0x03: bytestring_codec_1.byteStringCodec.parser,
73
- 0x04: lockup_script_codec_1.lockupScriptCodec.parser // Address
74
- }
75
- });
76
- }
77
- encode(input) {
78
- const valType = input.type;
79
- if (valType === 0x00) {
80
- // Boolean
81
- return new Uint8Array([valType, input.val]);
82
- }
83
- else if (valType === 0x01) {
84
- // I256
85
- return new Uint8Array([valType, ...compact_int_codec_1.compactUnsignedIntCodec.encode(input.val)]);
86
- }
87
- else if (valType === 0x02) {
88
- // U256
89
- return new Uint8Array([valType, ...compact_int_codec_1.compactUnsignedIntCodec.encode(input.val)]);
90
- }
91
- else if (valType === 0x03) {
92
- // ByteVec
93
- return new Uint8Array([valType, ...bytestring_codec_1.byteStringCodec.encode(input.val)]);
94
- }
95
- else if (valType === 0x04) {
96
- // Address
97
- return new Uint8Array([valType, ...lockup_script_codec_1.lockupScriptCodec.encode(input.val)]);
98
- }
99
- else {
100
- throw new Error(`ValCodec: unsupported val type: ${valType}`);
101
- }
102
- }
103
- decode(input) {
104
- return this.parser.parse(input);
105
- }
106
- }
107
- const valCodec = new ValCodec();
108
- const valsCodec = new array_codec_1.ArrayCodec(valCodec);
109
- class P2SHCodec {
110
- constructor() {
111
- this.parser = binary_parser_1.Parser.start()
112
- .nest('script', {
113
- type: script_codec_1.scriptCodec.parser
114
- })
115
- .nest('params', {
116
- type: valsCodec.parser
117
- });
118
- }
119
- encode(input) {
120
- return (0, utils_1.concatBytes)([script_codec_1.scriptCodec.encode(input.script), valsCodec.encode(input.params.value)]);
121
- }
122
- decode(input) {
123
- return this.parser.parse(input);
124
- }
125
- }
126
- exports.P2SHCodec = P2SHCodec;
127
- const p2shCodec = new P2SHCodec();
128
- class UnlockScriptCodec {
129
- constructor() {
130
- this.parser = binary_parser_1.Parser.start()
131
- .uint8('scriptType')
132
- .choice('script', {
133
- tag: 'scriptType',
134
- choices: {
135
- 0: p2pkhCodec.parser,
136
- 1: p2mpkhCodec.parser,
137
- 2: p2shCodec.parser,
138
- 3: binary_parser_1.Parser.start() // TODO: SameAsPrevious, FIXME
139
- }
140
- });
141
- }
142
- encode(input) {
143
- const scriptType = input.scriptType;
144
- const inputUnLockScript = input.script;
145
- if (scriptType === 0) {
146
- // P2PKH
147
- return new Uint8Array([scriptType, ...p2pkhCodec.encode(inputUnLockScript)]);
148
- }
149
- else if (scriptType === 1) {
150
- // P2MPKH
151
- return new Uint8Array([scriptType, ...p2mpkhCodec.encode(inputUnLockScript)]);
152
- }
153
- else if (scriptType === 2) {
154
- // P2SH
155
- return new Uint8Array([scriptType, ...p2shCodec.encode(inputUnLockScript)]);
156
- }
157
- else if (scriptType === 3) {
158
- // SameAsPrevious
159
- return new Uint8Array([scriptType]);
160
- }
161
- else {
162
- throw new Error(`TODO: encode unlock script: ${scriptType}`);
163
- }
164
- }
165
- decode(input) {
166
- return this.parser.parse(input);
167
- }
168
- }
169
- exports.UnlockScriptCodec = UnlockScriptCodec;
170
- exports.unlockScriptCodec = new UnlockScriptCodec();
25
+ const val_1 = require("./val");
26
+ const p2pkhCodec = new codec_1.FixedSizeCodec(33);
27
+ const keyWithIndexCodec = new codec_1.ObjectCodec({
28
+ publicKey: p2pkhCodec,
29
+ index: compact_int_codec_1.i32Codec
30
+ });
31
+ const p2mpkhCodec = new array_codec_1.ArrayCodec(keyWithIndexCodec);
32
+ const p2shCodec = new codec_1.ObjectCodec({
33
+ script: script_codec_1.scriptCodec,
34
+ params: val_1.valsCodec
35
+ });
36
+ const sameAsPreviousCodec = new (class extends codec_1.Codec {
37
+ encode() {
38
+ return new Uint8Array([]);
39
+ }
40
+ _decode() {
41
+ return 'SameAsPrevious';
42
+ }
43
+ })();
44
+ exports.unlockScriptCodec = new codec_1.EnumCodec('unlock script', {
45
+ P2PKH: p2pkhCodec,
46
+ P2MPKH: p2mpkhCodec,
47
+ P2SH: p2shCodec,
48
+ SameAsPrevious: sameAsPreviousCodec
49
+ });
@@ -1,25 +1,19 @@
1
- import { Parser } from 'binary-parser';
2
1
  import { UnsignedTx as ApiUnsignedTx } from '../api/api-alephium';
3
- import { DecodedScript } from './script-codec';
2
+ import { Script } from './script-codec';
4
3
  import { Option } from './option-codec';
5
- import { DecodedCompactInt } from './compact-int-codec';
6
4
  import { Input } from './input-codec';
7
5
  import { AssetOutput } from './asset-output-codec';
8
- import { DecodedArray } from './array-codec';
9
- import { Codec } from './codec';
6
+ import { ObjectCodec } from './codec';
10
7
  export interface UnsignedTx {
11
8
  version: number;
12
9
  networkId: number;
13
- statefulScript: Option<DecodedScript>;
14
- gasAmount: DecodedCompactInt;
15
- gasPrice: DecodedCompactInt;
16
- inputs: DecodedArray<Input>;
17
- fixedOutputs: DecodedArray<AssetOutput>;
10
+ statefulScript: Option<Script>;
11
+ gasAmount: number;
12
+ gasPrice: bigint;
13
+ inputs: Input[];
14
+ fixedOutputs: AssetOutput[];
18
15
  }
19
- export declare class UnsignedTxCodec implements Codec<UnsignedTx> {
20
- parser: Parser;
21
- encode(decodedUnsignedTx: UnsignedTx): Uint8Array;
22
- decode(input: Uint8Array): UnsignedTx;
16
+ export declare class UnsignedTxCodec extends ObjectCodec<UnsignedTx> {
23
17
  encodeApiUnsignedTx(input: ApiUnsignedTx): Uint8Array;
24
18
  decodeApiUnsignedTx(input: Uint8Array): ApiUnsignedTx;
25
19
  static txId(unsignedTx: UnsignedTx): string;
@@ -1,70 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.unsignedTxCodec = exports.UnsignedTxCodec = 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
4
  const utils_1 = require("../utils");
23
5
  const script_codec_1 = require("./script-codec");
24
6
  const compact_int_codec_1 = require("./compact-int-codec");
25
7
  const input_codec_1 = require("./input-codec");
26
8
  const asset_output_codec_1 = require("./asset-output-codec");
27
9
  const hash_1 = require("./hash");
28
- class UnsignedTxCodec {
29
- constructor() {
30
- this.parser = new binary_parser_1.Parser()
31
- .uint8('version')
32
- .uint8('networkId')
33
- .nest('statefulScript', {
34
- type: script_codec_1.statefulScriptCodecOpt.parser
35
- })
36
- .nest('gasAmount', {
37
- type: compact_int_codec_1.compactSignedIntCodec.parser
38
- })
39
- .nest('gasPrice', {
40
- type: compact_int_codec_1.compactUnsignedIntCodec.parser
41
- })
42
- .nest('inputs', {
43
- type: input_codec_1.inputsCodec.parser
44
- })
45
- .nest('fixedOutputs', {
46
- type: asset_output_codec_1.assetOutputsCodec.parser
47
- });
48
- }
49
- encode(decodedUnsignedTx) {
50
- return (0, utils_1.concatBytes)([
51
- new Uint8Array([decodedUnsignedTx.version, decodedUnsignedTx.networkId]),
52
- script_codec_1.statefulScriptCodecOpt.encode(decodedUnsignedTx.statefulScript),
53
- compact_int_codec_1.compactSignedIntCodec.encode(decodedUnsignedTx.gasAmount),
54
- compact_int_codec_1.compactUnsignedIntCodec.encode(decodedUnsignedTx.gasPrice),
55
- input_codec_1.inputsCodec.encode(decodedUnsignedTx.inputs.value),
56
- asset_output_codec_1.assetOutputsCodec.encode(decodedUnsignedTx.fixedOutputs.value)
57
- ]);
58
- }
59
- decode(input) {
60
- return this.parser.parse(input);
61
- }
10
+ const codec_1 = require("./codec");
11
+ class UnsignedTxCodec extends codec_1.ObjectCodec {
62
12
  encodeApiUnsignedTx(input) {
63
13
  const decoded = UnsignedTxCodec.fromApiUnsignedTx(input);
64
14
  return this.encode(decoded);
65
15
  }
66
16
  decodeApiUnsignedTx(input) {
67
- const decoded = this.parser.parse(input);
17
+ const decoded = this.decode(input);
68
18
  return UnsignedTxCodec.toApiUnsignedTx(decoded);
69
19
  }
70
20
  static txId(unsignedTx) {
@@ -75,12 +25,12 @@ class UnsignedTxCodec {
75
25
  const txIdBytes = (0, utils_1.hexToBinUnsafe)(txId);
76
26
  const version = unsigned.version;
77
27
  const networkId = unsigned.networkId;
78
- const gasAmount = compact_int_codec_1.compactSignedIntCodec.toI32(unsigned.gasAmount);
79
- const gasPrice = compact_int_codec_1.compactUnsignedIntCodec.toU256(unsigned.gasPrice).toString();
80
- const inputs = input_codec_1.InputCodec.toAssetInputs(unsigned.inputs.value);
81
- const fixedOutputs = asset_output_codec_1.AssetOutputCodec.toFixedAssetOutputs(txIdBytes, unsigned.fixedOutputs.value);
28
+ const gasAmount = unsigned.gasAmount;
29
+ const gasPrice = unsigned.gasPrice.toString();
30
+ const inputs = input_codec_1.InputCodec.toAssetInputs(unsigned.inputs);
31
+ const fixedOutputs = asset_output_codec_1.AssetOutputCodec.toFixedAssetOutputs(txIdBytes, unsigned.fixedOutputs);
82
32
  let scriptOpt = undefined;
83
- if (unsigned.statefulScript.option === 1) {
33
+ if (unsigned.statefulScript.kind === 'Some') {
84
34
  scriptOpt = (0, utils_1.binToHex)(script_codec_1.scriptCodec.encode(unsigned.statefulScript.value));
85
35
  }
86
36
  return { txId, version, networkId, gasAmount, scriptOpt, gasPrice, inputs, fixedOutputs };
@@ -88,15 +38,23 @@ class UnsignedTxCodec {
88
38
  static fromApiUnsignedTx(unsignedTx) {
89
39
  const version = unsignedTx.version;
90
40
  const networkId = unsignedTx.networkId;
91
- const gasAmount = compact_int_codec_1.compactSignedIntCodec.fromI32(unsignedTx.gasAmount);
92
- const gasPrice = compact_int_codec_1.compactUnsignedIntCodec.fromU256(BigInt(unsignedTx.gasPrice));
93
- const inputsValue = input_codec_1.InputCodec.fromAssetInputs(unsignedTx.inputs);
94
- const inputs = input_codec_1.inputsCodec.fromArray(inputsValue);
95
- const fixedOutputsValue = asset_output_codec_1.AssetOutputCodec.fromFixedAssetOutputs(unsignedTx.fixedOutputs);
96
- const fixedOutputs = asset_output_codec_1.assetOutputsCodec.fromArray(fixedOutputsValue);
97
- const statefulScript = script_codec_1.statefulScriptCodecOpt.fromBytes(unsignedTx.scriptOpt ? (0, utils_1.hexToBinUnsafe)(unsignedTx.scriptOpt) : undefined);
41
+ const gasAmount = unsignedTx.gasAmount;
42
+ const gasPrice = BigInt(unsignedTx.gasPrice);
43
+ const inputs = input_codec_1.InputCodec.fromAssetInputs(unsignedTx.inputs);
44
+ const fixedOutputs = asset_output_codec_1.AssetOutputCodec.fromFixedAssetOutputs(unsignedTx.fixedOutputs);
45
+ const statefulScript = unsignedTx.scriptOpt !== undefined
46
+ ? { kind: 'Some', value: script_codec_1.scriptCodec.decode((0, utils_1.hexToBinUnsafe)(unsignedTx.scriptOpt)) }
47
+ : { kind: 'None', value: undefined };
98
48
  return { version, networkId, gasAmount, gasPrice, inputs, fixedOutputs, statefulScript };
99
49
  }
100
50
  }
101
51
  exports.UnsignedTxCodec = UnsignedTxCodec;
102
- exports.unsignedTxCodec = new UnsignedTxCodec();
52
+ exports.unsignedTxCodec = new UnsignedTxCodec({
53
+ version: codec_1.byteCodec,
54
+ networkId: codec_1.byteCodec,
55
+ statefulScript: script_codec_1.statefulScriptCodecOpt,
56
+ gasAmount: compact_int_codec_1.i32Codec,
57
+ gasPrice: compact_int_codec_1.u256Codec,
58
+ inputs: input_codec_1.inputsCodec,
59
+ fixedOutputs: asset_output_codec_1.assetOutputsCodec
60
+ });
@@ -0,0 +1,27 @@
1
+ import { ByteString } from './bytestring-codec';
2
+ import { EnumCodec } from './codec';
3
+ import { LockupScript } from './lockup-script-codec';
4
+ import { ArrayCodec } from './array-codec';
5
+ export type ValBool = {
6
+ kind: 'Bool';
7
+ value: boolean;
8
+ };
9
+ export type ValI256 = {
10
+ kind: 'I256';
11
+ value: bigint;
12
+ };
13
+ export type ValU256 = {
14
+ kind: 'U256';
15
+ value: bigint;
16
+ };
17
+ export type ValByteVec = {
18
+ kind: 'ByteVec';
19
+ value: ByteString;
20
+ };
21
+ export type ValAddress = {
22
+ kind: 'Address';
23
+ value: LockupScript;
24
+ };
25
+ export type Val = ValBool | ValI256 | ValU256 | ValByteVec | ValAddress;
26
+ export declare const valCodec: EnumCodec<Val>;
27
+ export declare const valsCodec: ArrayCodec<Val>;