-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
input.ts
211 lines (185 loc) · 6.07 KB
/
input.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* eslint-disable max-classes-per-file */
import { concat } from '@ethersproject/bytes';
import { Coder, B256Coder, NumberCoder } from '@fuel-ts/abi-coder';
import { ByteArrayCoder } from './byte-array';
import type { UtxoId } from './utxo-id';
import { UtxoIdCoder } from './utxo-id';
export enum InputType /* u8 */ {
Coin = 0,
Contract = 1,
}
export type InputCoin = {
type: InputType.Coin;
/** UTXO ID (UtxoId) */
utxoID: UtxoId;
/** Owning address or script hash (b256) */
owner: string;
/** Amount of coins (u64) */
amount: bigint;
/** Asset ID of the coins (b256) */
assetId: string;
/** Index of witness that authorizes spending the coin (u8) */
witnessIndex: number;
/** UTXO being spent must have been created at least this many blocks ago (u64) */
maturity: bigint;
/** Length of predicate, in instructions (u16) */
predicateLength: number;
/** Length of predicate input data, in bytes (u16) */
predicateDataLength: number;
/** Predicate bytecode (byte[]) */
predicate: string;
/** Predicate input data (parameters) (byte[]) */
predicateData: string;
};
export class InputCoinCoder extends Coder<InputCoin, InputCoin> {
constructor() {
super('InputCoin', 'struct InputCoin', 0);
}
encode(value: InputCoin): Uint8Array {
const parts: Uint8Array[] = [];
parts.push(new UtxoIdCoder().encode(value.utxoID));
parts.push(new B256Coder().encode(value.owner));
parts.push(new NumberCoder('u64').encode(value.amount));
parts.push(new B256Coder().encode(value.assetId));
parts.push(new NumberCoder('u8').encode(value.witnessIndex));
parts.push(new NumberCoder('u64').encode(value.maturity));
parts.push(new NumberCoder('u16').encode(value.predicateLength));
parts.push(new NumberCoder('u16').encode(value.predicateDataLength));
parts.push(new ByteArrayCoder(value.predicateLength).encode(value.predicate));
parts.push(new ByteArrayCoder(value.predicateDataLength).encode(value.predicateData));
return concat(parts);
}
decode(data: Uint8Array, offset: number): [InputCoin, number] {
let decoded;
let o = offset;
[decoded, o] = new UtxoIdCoder().decode(data, o);
const utxoID = decoded;
[decoded, o] = new B256Coder().decode(data, o);
const owner = decoded;
[decoded, o] = new NumberCoder('u64').decode(data, o);
const amount = decoded;
[decoded, o] = new B256Coder().decode(data, o);
const assetId = decoded;
[decoded, o] = new NumberCoder('u8').decode(data, o);
const witnessIndex = Number(decoded);
[decoded, o] = new NumberCoder('u64').decode(data, o);
const maturity = decoded;
[decoded, o] = new NumberCoder('u16').decode(data, o);
[decoded, o] = new NumberCoder('u16').decode(data, o);
const predicateLength = decoded;
const predicateDataLength = decoded;
[decoded, o] = new ByteArrayCoder(predicateLength).decode(data, o);
const predicate = decoded;
[decoded, o] = new ByteArrayCoder(predicateDataLength).decode(data, o);
const predicateData = decoded;
return [
{
type: InputType.Coin,
utxoID,
owner,
amount,
assetId,
witnessIndex,
maturity,
predicateLength,
predicateDataLength,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
predicate,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
predicateData,
},
o,
];
}
}
export type InputContract = {
type: InputType.Contract;
/** UTXO ID (UtxoId) */
utxoID: UtxoId;
/** Root of amount of coins owned by contract before transaction execution (b256) */
balanceRoot: string;
/** State root of contract before transaction execution (b256) */
stateRoot: string;
/** Contract ID (b256) */
contractID: string;
};
export class InputContractCoder extends Coder<InputContract, InputContract> {
constructor() {
super('InputContract', 'struct InputContract', 0);
}
encode(value: InputContract): Uint8Array {
const parts: Uint8Array[] = [];
parts.push(new UtxoIdCoder().encode(value.utxoID));
parts.push(new B256Coder().encode(value.balanceRoot));
parts.push(new B256Coder().encode(value.stateRoot));
parts.push(new B256Coder().encode(value.contractID));
return concat(parts);
}
decode(data: Uint8Array, offset: number): [InputContract, number] {
let decoded;
let o = offset;
[decoded, o] = new UtxoIdCoder().decode(data, o);
const utxoID = decoded;
[decoded, o] = new B256Coder().decode(data, o);
const balanceRoot = decoded;
[decoded, o] = new B256Coder().decode(data, o);
const stateRoot = decoded;
[decoded, o] = new B256Coder().decode(data, o);
const contractID = decoded;
return [
{
type: InputType.Contract,
utxoID,
balanceRoot,
stateRoot,
contractID,
},
o,
];
}
}
export type Input = InputCoin | InputContract;
export class InputCoder extends Coder<Input, Input> {
constructor() {
super('Input', 'struct Input', 0);
}
encode(value: Input): Uint8Array {
const parts: Uint8Array[] = [];
parts.push(new NumberCoder('u8').encode(value.type));
switch (value.type) {
case InputType.Coin: {
parts.push(new InputCoinCoder().encode(value));
break;
}
case InputType.Contract: {
parts.push(new InputContractCoder().encode(value));
break;
}
default: {
throw new Error('Invalid Input type');
}
}
return concat(parts);
}
decode(data: Uint8Array, offset: number): [Input, number] {
let decoded;
let o = offset;
[decoded, o] = new NumberCoder('u8').decode(data, o);
const type = decoded as InputType;
switch (type) {
case InputType.Coin: {
[decoded, o] = new InputCoinCoder().decode(data, o);
return [decoded, o];
}
case InputType.Contract: {
[decoded, o] = new InputContractCoder().decode(data, o);
return [decoded, o];
}
default: {
throw new Error('Invalid Input type');
}
}
}
}