-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
witness.ts
49 lines (40 loc) · 1.12 KB
/
witness.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
import { concat } from '@ethersproject/bytes';
import { Coder, NumberCoder } from '@fuel-ts/abi-coder';
import { ByteArrayCoder } from './byte-array';
export type Witness = {
/** Length of witness data, in bytes (u32) */
dataLength: number;
/** Witness data (byte[]) */
data: string;
};
export class WitnessCoder extends Coder<Witness, Witness> {
constructor() {
super(
'Witness',
// Types of dynamic length are not supported in the ABI
'unknown',
0
);
}
encode(value: Witness): Uint8Array {
const parts: Uint8Array[] = [];
parts.push(new NumberCoder('u32').encode(value.dataLength));
parts.push(new ByteArrayCoder(value.dataLength).encode(value.data));
return concat(parts);
}
decode(data: Uint8Array, offset: number): [Witness, number] {
let decoded;
let o = offset;
[decoded, o] = new NumberCoder('u32').decode(data, o);
const dataLength = decoded;
[decoded, o] = new ByteArrayCoder(dataLength).decode(data, o);
const witnessData = decoded;
return [
{
dataLength,
data: witnessData,
},
o,
];
}
}