@alephium/web3 0.30.0-beta.1 → 0.30.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 (90) 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 +5 -1
  4. package/dist/src/api/api-alephium.js +1 -1
  5. package/dist/src/contract/contract.d.ts +1 -0
  6. package/dist/src/contract/contract.js +1 -0
  7. package/dist/src/index.d.ts +0 -1
  8. package/dist/src/index.js +1 -2
  9. package/package.json +3 -4
  10. package/src/api/api-alephium.ts +5 -1
  11. package/src/contract/contract.ts +2 -0
  12. package/src/index.ts +1 -1
  13. package/dist/src/codec/array-codec.d.ts +0 -17
  14. package/dist/src/codec/array-codec.js +0 -58
  15. package/dist/src/codec/asset-output-codec.d.ts +0 -27
  16. package/dist/src/codec/asset-output-codec.js +0 -135
  17. package/dist/src/codec/bigint-codec.d.ts +0 -5
  18. package/dist/src/codec/bigint-codec.js +0 -85
  19. package/dist/src/codec/bytestring-codec.d.ts +0 -16
  20. package/dist/src/codec/bytestring-codec.js +0 -49
  21. package/dist/src/codec/codec.d.ts +0 -8
  22. package/dist/src/codec/codec.js +0 -9
  23. package/dist/src/codec/compact-int-codec.d.ts +0 -47
  24. package/dist/src/codec/compact-int-codec.js +0 -287
  25. package/dist/src/codec/contract-codec.d.ts +0 -22
  26. package/dist/src/codec/contract-codec.js +0 -62
  27. package/dist/src/codec/contract-output-codec.d.ts +0 -21
  28. package/dist/src/codec/contract-output-codec.js +0 -81
  29. package/dist/src/codec/contract-output-ref-codec.d.ts +0 -15
  30. package/dist/src/codec/contract-output-ref-codec.js +0 -37
  31. package/dist/src/codec/either-codec.d.ts +0 -17
  32. package/dist/src/codec/either-codec.js +0 -66
  33. package/dist/src/codec/hash.d.ts +0 -4
  34. package/dist/src/codec/hash.js +0 -40
  35. package/dist/src/codec/index.d.ts +0 -23
  36. package/dist/src/codec/index.js +0 -69
  37. package/dist/src/codec/input-codec.d.ts +0 -22
  38. package/dist/src/codec/input-codec.js +0 -70
  39. package/dist/src/codec/instr-codec.d.ts +0 -223
  40. package/dist/src/codec/instr-codec.js +0 -459
  41. package/dist/src/codec/lockup-script-codec.d.ts +0 -28
  42. package/dist/src/codec/lockup-script-codec.js +0 -79
  43. package/dist/src/codec/long-codec.d.ts +0 -9
  44. package/dist/src/codec/long-codec.js +0 -55
  45. package/dist/src/codec/method-codec.d.ts +0 -30
  46. package/dist/src/codec/method-codec.js +0 -67
  47. package/dist/src/codec/option-codec.d.ts +0 -15
  48. package/dist/src/codec/option-codec.js +0 -54
  49. package/dist/src/codec/output-codec.d.ts +0 -7
  50. package/dist/src/codec/output-codec.js +0 -26
  51. package/dist/src/codec/script-codec.d.ts +0 -16
  52. package/dist/src/codec/script-codec.js +0 -40
  53. package/dist/src/codec/signature-codec.d.ts +0 -14
  54. package/dist/src/codec/signature-codec.js +0 -36
  55. package/dist/src/codec/signed-int-codec.d.ts +0 -9
  56. package/dist/src/codec/signed-int-codec.js +0 -38
  57. package/dist/src/codec/token-codec.d.ts +0 -16
  58. package/dist/src/codec/token-codec.js +0 -45
  59. package/dist/src/codec/transaction-codec.d.ts +0 -27
  60. package/dist/src/codec/transaction-codec.js +0 -127
  61. package/dist/src/codec/unlock-script-codec.d.ts +0 -40
  62. package/dist/src/codec/unlock-script-codec.js +0 -169
  63. package/dist/src/codec/unsigned-tx-codec.d.ts +0 -30
  64. package/dist/src/codec/unsigned-tx-codec.js +0 -102
  65. package/src/codec/array-codec.ts +0 -62
  66. package/src/codec/asset-output-codec.ts +0 -150
  67. package/src/codec/bigint-codec.ts +0 -91
  68. package/src/codec/bytestring-codec.ts +0 -55
  69. package/src/codec/codec.ts +0 -30
  70. package/src/codec/compact-int-codec.ts +0 -299
  71. package/src/codec/contract-codec.ts +0 -76
  72. package/src/codec/contract-output-codec.ts +0 -97
  73. package/src/codec/contract-output-ref-codec.ts +0 -41
  74. package/src/codec/either-codec.ts +0 -73
  75. package/src/codec/hash.ts +0 -34
  76. package/src/codec/index.ts +0 -41
  77. package/src/codec/input-codec.ts +0 -80
  78. package/src/codec/instr-codec.ts +0 -462
  79. package/src/codec/lockup-script-codec.ts +0 -98
  80. package/src/codec/long-codec.ts +0 -58
  81. package/src/codec/method-codec.ts +0 -85
  82. package/src/codec/option-codec.ts +0 -59
  83. package/src/codec/output-codec.ts +0 -26
  84. package/src/codec/script-codec.ts +0 -44
  85. package/src/codec/signature-codec.ts +0 -39
  86. package/src/codec/signed-int-codec.ts +0 -36
  87. package/src/codec/token-codec.ts +0 -49
  88. package/src/codec/transaction-codec.ts +0 -146
  89. package/src/codec/unlock-script-codec.ts +0 -193
  90. package/src/codec/unsigned-tx-codec.ts +0 -123
@@ -1,169 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.unlockScriptCodec = exports.UnlockScriptCodec = exports.P2SHCodec = 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 script_codec_1 = require("./script-codec");
25
- const bytestring_codec_1 = require("./bytestring-codec");
26
- const lockup_script_codec_1 = require("./lockup-script-codec");
27
- class P2PKHCodec {
28
- constructor() {
29
- this.parser = binary_parser_1.Parser.start().buffer('publicKey', { length: 33 });
30
- }
31
- encode(input) {
32
- return input.publicKey;
33
- }
34
- decode(input) {
35
- return this.parser.parse(input);
36
- }
37
- }
38
- const p2pkhCodec = new P2PKHCodec();
39
- class P2MPKHCodec {
40
- constructor() {
41
- this.parser = binary_parser_1.Parser.start().nest('publicKeys', {
42
- type: array_codec_1.ArrayCodec.arrayParser(binary_parser_1.Parser.start()
43
- .nest('publicKey', { type: p2pkhCodec.parser })
44
- .nest('index', { type: compact_int_codec_1.compactUnsignedIntCodec.parser }))
45
- });
46
- }
47
- encode(input) {
48
- return Buffer.concat([
49
- Buffer.from(compact_int_codec_1.compactUnsignedIntCodec.encode(input.publicKeys.length)),
50
- ...input.publicKeys.value.map((v) => {
51
- return Buffer.concat([v.publicKey.publicKey, Buffer.from(compact_int_codec_1.compactUnsignedIntCodec.encode(v.index))]);
52
- })
53
- ]);
54
- }
55
- decode(input) {
56
- return this.parser.parse(input);
57
- }
58
- }
59
- const p2mpkhCodec = new P2MPKHCodec();
60
- class ValCodec {
61
- constructor() {
62
- this.parser = binary_parser_1.Parser.start()
63
- .int8('type')
64
- .choice('val', {
65
- tag: 'type',
66
- choices: {
67
- 0x00: new binary_parser_1.Parser().uint8('value'),
68
- 0x01: compact_int_codec_1.compactSignedIntCodec.parser,
69
- 0x02: compact_int_codec_1.compactUnsignedIntCodec.parser,
70
- 0x03: bytestring_codec_1.byteStringCodec.parser,
71
- 0x04: lockup_script_codec_1.lockupScriptCodec.parser // Address
72
- }
73
- });
74
- }
75
- encode(input) {
76
- const valType = input.type;
77
- if (valType === 0x00) {
78
- // Boolean
79
- return Buffer.from([valType, input.val]);
80
- }
81
- else if (valType === 0x01) {
82
- // I256
83
- return Buffer.from([valType, ...compact_int_codec_1.compactUnsignedIntCodec.encode(input.val)]);
84
- }
85
- else if (valType === 0x02) {
86
- // U256
87
- return Buffer.from([valType, ...compact_int_codec_1.compactUnsignedIntCodec.encode(input.val)]);
88
- }
89
- else if (valType === 0x03) {
90
- // ByteVec
91
- return Buffer.from([valType, ...bytestring_codec_1.byteStringCodec.encode(input.val)]);
92
- }
93
- else if (valType === 0x04) {
94
- // Address
95
- return Buffer.from([valType, ...lockup_script_codec_1.lockupScriptCodec.encode(input.val)]);
96
- }
97
- else {
98
- throw new Error(`ValCodec: unsupported val type: ${valType}`);
99
- }
100
- }
101
- decode(input) {
102
- return this.parser.parse(input);
103
- }
104
- }
105
- const valCodec = new ValCodec();
106
- const valsCodec = new array_codec_1.ArrayCodec(valCodec);
107
- class P2SHCodec {
108
- constructor() {
109
- this.parser = binary_parser_1.Parser.start()
110
- .nest('script', {
111
- type: script_codec_1.scriptCodec.parser
112
- })
113
- .nest('params', {
114
- type: valsCodec.parser
115
- });
116
- }
117
- encode(input) {
118
- return Buffer.concat([script_codec_1.scriptCodec.encode(input.script), valsCodec.encode(input.params.value)]);
119
- }
120
- decode(input) {
121
- return this.parser.parse(input);
122
- }
123
- }
124
- exports.P2SHCodec = P2SHCodec;
125
- const p2shCodec = new P2SHCodec();
126
- class UnlockScriptCodec {
127
- constructor() {
128
- this.parser = binary_parser_1.Parser.start()
129
- .uint8('scriptType')
130
- .choice('script', {
131
- tag: 'scriptType',
132
- choices: {
133
- 0: p2pkhCodec.parser,
134
- 1: p2mpkhCodec.parser,
135
- 2: p2shCodec.parser,
136
- 3: binary_parser_1.Parser.start() // TODO: SameAsPrevious, FIXME
137
- }
138
- });
139
- }
140
- encode(input) {
141
- const scriptType = input.scriptType;
142
- const inputUnLockScript = input.script;
143
- const inputUnLockScriptType = Buffer.from([scriptType]);
144
- if (scriptType === 0) {
145
- // P2PKH
146
- return Buffer.concat([inputUnLockScriptType, p2pkhCodec.encode(inputUnLockScript)]);
147
- }
148
- else if (scriptType === 1) {
149
- // P2MPKH
150
- return Buffer.concat([inputUnLockScriptType, p2mpkhCodec.encode(inputUnLockScript)]);
151
- }
152
- else if (scriptType === 2) {
153
- // P2SH
154
- return Buffer.concat([inputUnLockScriptType, p2shCodec.encode(input.script)]);
155
- }
156
- else if (scriptType === 3) {
157
- // SameAsPrevious
158
- return inputUnLockScriptType;
159
- }
160
- else {
161
- throw new Error(`TODO: encode unlock script: ${scriptType}`);
162
- }
163
- }
164
- decode(input) {
165
- return this.parser.parse(input);
166
- }
167
- }
168
- exports.UnlockScriptCodec = UnlockScriptCodec;
169
- exports.unlockScriptCodec = new UnlockScriptCodec();
@@ -1,30 +0,0 @@
1
- /// <reference types="node" />
2
- import { Parser } from 'binary-parser';
3
- import { UnsignedTx as ApiUnsignedTx } from '../api/api-alephium';
4
- import { Script } from './script-codec';
5
- import { Option } from './option-codec';
6
- import { DecodedCompactInt } from './compact-int-codec';
7
- import { Input } from './input-codec';
8
- import { AssetOutput } from './asset-output-codec';
9
- import { DecodedArray } from './array-codec';
10
- import { Codec } from './codec';
11
- export interface UnsignedTx {
12
- version: number;
13
- networkId: number;
14
- statefulScript: Option<Script>;
15
- gasAmount: DecodedCompactInt;
16
- gasPrice: DecodedCompactInt;
17
- inputs: DecodedArray<Input>;
18
- fixedOutputs: DecodedArray<AssetOutput>;
19
- }
20
- export declare class UnsignedTxCodec implements Codec<UnsignedTx> {
21
- parser: Parser;
22
- encode(decodedUnsignedTx: UnsignedTx): Buffer;
23
- decode(input: Buffer): UnsignedTx;
24
- encodeApiUnsignedTx(input: ApiUnsignedTx): Buffer;
25
- decodeApiUnsignedTx(input: Buffer): ApiUnsignedTx;
26
- static txId(unsignedTx: UnsignedTx): string;
27
- static toApiUnsignedTx(unsigned: UnsignedTx): ApiUnsignedTx;
28
- static fromApiUnsignedTx(unsignedTx: ApiUnsignedTx): UnsignedTx;
29
- }
30
- export declare const unsignedTxCodec: UnsignedTxCodec;
@@ -1,102 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
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
- const utils_1 = require("../utils");
23
- const script_codec_1 = require("./script-codec");
24
- const compact_int_codec_1 = require("./compact-int-codec");
25
- const input_codec_1 = require("./input-codec");
26
- const asset_output_codec_1 = require("./asset-output-codec");
27
- 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 Buffer.concat([
51
- Buffer.from([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
- }
62
- encodeApiUnsignedTx(input) {
63
- const decoded = UnsignedTxCodec.fromApiUnsignedTx(input);
64
- return this.encode(decoded);
65
- }
66
- decodeApiUnsignedTx(input) {
67
- const decoded = this.parser.parse(input);
68
- return UnsignedTxCodec.toApiUnsignedTx(decoded);
69
- }
70
- static txId(unsignedTx) {
71
- return (0, utils_1.binToHex)((0, hash_1.blakeHash)(exports.unsignedTxCodec.encode(unsignedTx)));
72
- }
73
- static toApiUnsignedTx(unsigned) {
74
- const txId = UnsignedTxCodec.txId(unsigned);
75
- const txIdBytes = (0, utils_1.hexToBinUnsafe)(txId);
76
- const version = unsigned.version;
77
- 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);
82
- let scriptOpt = undefined;
83
- if (unsigned.statefulScript.option === 1) {
84
- scriptOpt = script_codec_1.scriptCodec.encode(unsigned.statefulScript.value).toString('hex');
85
- }
86
- return { txId, version, networkId, gasAmount, scriptOpt, gasPrice, inputs, fixedOutputs };
87
- }
88
- static fromApiUnsignedTx(unsignedTx) {
89
- const version = unsignedTx.version;
90
- const networkId = unsignedTx.networkId;
91
- const gasAmount = compact_int_codec_1.compactSignedIntCodec.decode(compact_int_codec_1.compactSignedIntCodec.encodeI32(unsignedTx.gasAmount));
92
- const gasPrice = compact_int_codec_1.compactUnsignedIntCodec.decode(compact_int_codec_1.compactUnsignedIntCodec.encodeU256(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.fromBuffer(unsignedTx.scriptOpt ? Buffer.from(unsignedTx.scriptOpt, 'hex') : undefined);
98
- return { version, networkId, gasAmount, gasPrice, inputs, fixedOutputs, statefulScript };
99
- }
100
- }
101
- exports.UnsignedTxCodec = UnsignedTxCodec;
102
- exports.unsignedTxCodec = new UnsignedTxCodec();
@@ -1,62 +0,0 @@
1
- /*
2
- Copyright 2018 - 2022 The Alephium Authors
3
- This file is part of the alephium project.
4
-
5
- The library is free software: you can redistribute it and/or modify
6
- it under the terms of the GNU Lesser General Public License as published by
7
- the Free Software Foundation, either version 3 of the License, or
8
- (at your option) any later version.
9
-
10
- The library is distributed in the hope that it will be useful,
11
- but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- GNU Lesser General Public License for more details.
14
-
15
- You should have received a copy of the GNU Lesser General Public License
16
- along with the library. If not, see <http://www.gnu.org/licenses/>.
17
- */
18
- import { Parser } from 'binary-parser'
19
- import { compactSignedIntCodec, compactUnsignedIntCodec, DecodedCompactInt } from './compact-int-codec'
20
- import { Codec } from './codec'
21
-
22
- export interface DecodedArray<T> {
23
- length: DecodedCompactInt
24
- value: T[]
25
- }
26
-
27
- export class ArrayCodec<T> implements Codec<T[]> {
28
- constructor(private childCodec: Codec<T>, public parser = ArrayCodec.arrayParser(childCodec.parser)) {}
29
-
30
- encode(input: T[]): Buffer {
31
- const result = [...compactSignedIntCodec.encodeI256(BigInt(input.length))]
32
- for (const element of input) {
33
- result.push(...this.childCodec.encode(element))
34
- }
35
- return Buffer.from(result)
36
- }
37
-
38
- decode(input: Buffer): T[] {
39
- const result = this.parser.parse(input)
40
- return result.value.map((v) => this.childCodec.decode(v.value))
41
- }
42
-
43
- static arrayParser(parser: Parser) {
44
- return new Parser()
45
- .nest('length', {
46
- type: compactSignedIntCodec.parser
47
- })
48
- .array('value', {
49
- length: function (ctx) {
50
- return compactSignedIntCodec.toI32(this['length']! as any as DecodedCompactInt)
51
- },
52
- type: parser
53
- })
54
- }
55
-
56
- fromArray(inputs: T[]): DecodedArray<T> {
57
- return {
58
- length: compactUnsignedIntCodec.decode(compactUnsignedIntCodec.encodeU32(inputs.length)),
59
- value: inputs
60
- }
61
- }
62
- }
@@ -1,150 +0,0 @@
1
- /*
2
- Copyright 2018 - 2022 The Alephium Authors
3
- This file is part of the alephium project.
4
-
5
- The library is free software: you can redistribute it and/or modify
6
- it under the terms of the GNU Lesser General Public License as published by
7
- the Free Software Foundation, either version 3 of the License, or
8
- (at your option) any later version.
9
-
10
- The library is distributed in the hope that it will be useful,
11
- but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- GNU Lesser General Public License for more details.
14
-
15
- You should have received a copy of the GNU Lesser General Public License
16
- along with the library. If not, see <http://www.gnu.org/licenses/>.
17
- */
18
- import { Parser } from 'binary-parser'
19
- import { ArrayCodec, DecodedArray } from './array-codec'
20
- import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
21
- import { signedIntCodec } from './signed-int-codec'
22
- import { longCodec } from './long-codec'
23
- import { ByteString, byteStringCodec } from './bytestring-codec'
24
- import { LockupScript, MultiSig, P2C, P2SH, lockupScriptCodec } from './lockup-script-codec'
25
- import { FixedAssetOutput } from '../api/api-alephium'
26
- import { blakeHash, createHint } from './hash'
27
- import { bs58, binToHex } from '../utils'
28
- import { Codec } from './codec'
29
- import { PublicKeyHash } from './lockup-script-codec'
30
- import { Token, tokensCodec } from './token-codec'
31
-
32
- export interface AssetOutput {
33
- amount: DecodedCompactInt
34
- lockupScript: LockupScript
35
- lockTime: Buffer
36
- tokens: DecodedArray<Token>
37
- additionalData: ByteString
38
- }
39
-
40
- export class AssetOutputCodec implements Codec<AssetOutput> {
41
- parser = Parser.start()
42
- .nest('amount', {
43
- type: compactUnsignedIntCodec.parser
44
- })
45
- .nest('lockupScript', {
46
- type: lockupScriptCodec.parser
47
- })
48
- .buffer('lockTime', {
49
- length: 8
50
- })
51
- .nest('tokens', {
52
- type: tokensCodec.parser
53
- })
54
- .nest('additionalData', {
55
- type: byteStringCodec.parser
56
- })
57
-
58
- encode(input: AssetOutput): Buffer {
59
- const amount = Buffer.from(compactUnsignedIntCodec.encode(input.amount))
60
- const lockupScript = lockupScriptCodec.encode(input.lockupScript)
61
- const lockTime = Buffer.from(input.lockTime)
62
- const tokens = Buffer.from(tokensCodec.encode(input.tokens.value))
63
- const additionalData = Buffer.from(byteStringCodec.encode(input.additionalData))
64
-
65
- return Buffer.concat([amount, lockupScript, lockTime, tokens, additionalData])
66
- }
67
-
68
- decode(input: Buffer): AssetOutput {
69
- return this.parser.parse(input)
70
- }
71
-
72
- static toFixedAssetOutputs(txIdBytes: Uint8Array, outputs: AssetOutput[]): FixedAssetOutput[] {
73
- return outputs.map((output, index) => AssetOutputCodec.toFixedAssetOutput(txIdBytes, output, index))
74
- }
75
-
76
- static toFixedAssetOutput(txIdBytes: Uint8Array, output: AssetOutput, index: number): FixedAssetOutput {
77
- const attoAlphAmount = compactUnsignedIntCodec.toU256(output.amount).toString()
78
- const lockTime = Number(longCodec.decode(output.lockTime))
79
- const tokens = output.tokens.value.map((token) => {
80
- return {
81
- id: token.tokenId.toString('hex'),
82
- amount: compactUnsignedIntCodec.toU256(token.amount).toString()
83
- }
84
- })
85
- const message = output.additionalData.value.toString('hex')
86
- const scriptType = output.lockupScript.scriptType
87
- const key = binToHex(blakeHash(Buffer.concat([txIdBytes, signedIntCodec.encode(index)])))
88
- const outputLockupScript = output.lockupScript.script
89
- const address = bs58.encode(lockupScriptCodec.encode(output.lockupScript))
90
-
91
- let hint: number | undefined = undefined
92
- if (scriptType === 0) {
93
- // P2PKH
94
- hint = createHint((outputLockupScript as PublicKeyHash).publicKeyHash)
95
- } else if (scriptType === 1) {
96
- // P2MPKH
97
- hint = createHint((outputLockupScript as MultiSig).publicKeyHashes.value[0].publicKeyHash)
98
- } else if (scriptType === 2) {
99
- // P2SH
100
- hint = createHint((outputLockupScript as P2SH).scriptHash)
101
- } else if (scriptType === 3) {
102
- // P2C
103
- hint = createHint((outputLockupScript as P2C).contractId)
104
- } else {
105
- throw new Error(`TODO: decode output script type: ${scriptType}`)
106
- }
107
-
108
- return { hint, key, attoAlphAmount, lockTime, tokens, address, message }
109
- }
110
-
111
- static fromFixedAssetOutputs(fixedOutputs: FixedAssetOutput[]): AssetOutput[] {
112
- return fixedOutputs.map((output) => {
113
- return AssetOutputCodec.fromFixedAssetOutput(output)
114
- })
115
- }
116
-
117
- static fromFixedAssetOutput(fixedOutput: FixedAssetOutput): AssetOutput {
118
- const amount: DecodedCompactInt = compactUnsignedIntCodec.decode(
119
- compactUnsignedIntCodec.encodeU256(BigInt(fixedOutput.attoAlphAmount))
120
- )
121
- const lockTime: Buffer = longCodec.encode(BigInt(fixedOutput.lockTime))
122
- const lockupScript: LockupScript = lockupScriptCodec.decode(Buffer.from(bs58.decode(fixedOutput.address)))
123
- const tokensValue = fixedOutput.tokens.map((token) => {
124
- return {
125
- tokenId: Buffer.from(token.id, 'hex'),
126
- amount: compactUnsignedIntCodec.decode(compactUnsignedIntCodec.encodeU256(BigInt(token.amount)))
127
- }
128
- })
129
- const tokens: DecodedArray<Token> = {
130
- length: compactUnsignedIntCodec.decode(compactUnsignedIntCodec.encodeU32(tokensValue.length)),
131
- value: tokensValue
132
- }
133
- const additionalDataValue = Buffer.from(fixedOutput.message, 'hex')
134
- const additionalData: ByteString = {
135
- length: compactUnsignedIntCodec.decode(compactUnsignedIntCodec.encodeU32(additionalDataValue.length)),
136
- value: additionalDataValue
137
- }
138
-
139
- return {
140
- amount,
141
- lockupScript,
142
- lockTime,
143
- tokens,
144
- additionalData
145
- }
146
- }
147
- }
148
-
149
- export const assetOutputCodec = new AssetOutputCodec()
150
- export const assetOutputsCodec = new ArrayCodec(assetOutputCodec)
@@ -1,91 +0,0 @@
1
- /*
2
- Copyright 2018 - 2022 The Alephium Authors
3
- This file is part of the alephium project.
4
-
5
- The library is free software: you can redistribute it and/or modify
6
- it under the terms of the GNU Lesser General Public License as published by
7
- the Free Software Foundation, either version 3 of the License, or
8
- (at your option) any later version.
9
-
10
- The library is distributed in the hope that it will be useful,
11
- but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- GNU Lesser General Public License for more details.
14
-
15
- You should have received a copy of the GNU Lesser General Public License
16
- along with the library. If not, see <http://www.gnu.org/licenses/>.
17
- */
18
- export class BigIntCodec {
19
- static encode(value: bigint): Buffer {
20
- // Special case for zero.
21
- if (value === 0n) {
22
- return Buffer.from([0])
23
- }
24
-
25
- const isNegative = value < 0n
26
- let absValue = isNegative ? -value : value
27
- const bytes: number[] = []
28
-
29
- // Extract bytes from absolute value.
30
- while (absValue > 0n) {
31
- bytes.push(Number(absValue & 0xffn))
32
- absValue >>= 8n
33
- }
34
-
35
- // If the bigint is positive and the most significant byte has its high bit set,
36
- // prefix the byte array with a zero byte to signify positive value.
37
- if (!isNegative && (bytes[bytes.length - 1] & 0x80) !== 0) {
38
- bytes.push(0)
39
- }
40
-
41
- // If the bigint is negative, compute the two's complement of the byte array.
42
- if (isNegative) {
43
- let carry = true
44
- for (let i = 0; i < bytes.length; i++) {
45
- bytes[i] = ~bytes[i] & 0xff // Invert the bits of the byte.
46
- if (carry) {
47
- if (bytes[i] === 0xff) {
48
- bytes[i] = 0
49
- } else {
50
- bytes[i] += 1
51
- carry = false
52
- }
53
- }
54
- }
55
-
56
- // If there's still a carry, and the most significant byte is not 0xFF (to store the negative sign bit),
57
- // or if no bytes were set (which means the value was -1), append a 0xFF byte to hold the carry.
58
- if (carry || bytes.length === 0 || (bytes[bytes.length - 1] & 0x80) === 0) {
59
- bytes.push(0xff)
60
- }
61
- }
62
-
63
- // The byte array needs to be reversed since we've constructed it in little-endian order.
64
- return Buffer.from(new Uint8Array(bytes.reverse()))
65
- }
66
-
67
- static decode(encoded: Buffer, signed: boolean): bigint {
68
- // Special case for zero.
69
- if (encoded.length === 1 && encoded[0] === 0) {
70
- return 0n
71
- }
72
-
73
- // Determine if the number is negative by checking the most significant byte (MSB)
74
- const isNegative = signed ? encoded[0] === 0xff : signed
75
-
76
- // Convert the byte array to a bigint
77
- let value = 0n
78
- for (const byte of encoded) {
79
- value = (value << 8n) | BigInt(byte)
80
- }
81
-
82
- // If the number is negative, convert from two's complement
83
- if (isNegative) {
84
- // Create a mask for the value's bit length
85
- const mask = (1n << (8n * BigInt(encoded.length))) - 1n
86
- value = -(~value & mask) - 1n
87
- }
88
-
89
- return value
90
- }
91
- }
@@ -1,55 +0,0 @@
1
- /*
2
- Copyright 2018 - 2022 The Alephium Authors
3
- This file is part of the alephium project.
4
-
5
- The library is free software: you can redistribute it and/or modify
6
- it under the terms of the GNU Lesser General Public License as published by
7
- the Free Software Foundation, either version 3 of the License, or
8
- (at your option) any later version.
9
-
10
- The library is distributed in the hope that it will be useful,
11
- but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- GNU Lesser General Public License for more details.
14
-
15
- You should have received a copy of the GNU Lesser General Public License
16
- along with the library. If not, see <http://www.gnu.org/licenses/>.
17
- */
18
- import { Parser } from 'binary-parser'
19
- import { DecodedCompactInt, compactUnsignedIntCodec } from './compact-int-codec'
20
- import { Codec } from './codec'
21
-
22
- export interface ByteString {
23
- length: DecodedCompactInt
24
- value: Buffer
25
- }
26
-
27
- export class ByteStringCodec implements Codec<ByteString> {
28
- parser = new Parser()
29
- .nest('length', {
30
- type: compactUnsignedIntCodec.parser
31
- })
32
- .buffer('value', {
33
- length: function (ctx) {
34
- return compactUnsignedIntCodec.toU32(this['length']! as any as DecodedCompactInt)
35
- }
36
- })
37
-
38
- encode(input: ByteString): Buffer {
39
- return Buffer.from([...compactUnsignedIntCodec.encode(input.length), ...input.value])
40
- }
41
-
42
- decode(input: Buffer): ByteString {
43
- return this.parser.parse(input)
44
- }
45
-
46
- encodeBuffer(input: Buffer): Buffer {
47
- return Buffer.from([...compactUnsignedIntCodec.encodeU32(input.length), ...input])
48
- }
49
-
50
- decodeBuffer(input: Buffer): Buffer {
51
- return this.decode(input).value
52
- }
53
- }
54
-
55
- export const byteStringCodec = new ByteStringCodec()