-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
utilities.ts
155 lines (131 loc) · 3.89 KB
/
utilities.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
import type { BytesLike } from '@ethersproject/bytes';
import { concat, arrayify } from '@ethersproject/bytes';
import { U64Coder } from './coders/u64';
import { VEC_CODER_TYPE, WORD_SIZE } from './constants';
export type DynamicData = {
[pointerIndex: number]: Uint8ArrayWithDynamicData;
};
export type Uint8ArrayWithDynamicData = Uint8Array & {
dynamicData?: DynamicData;
};
const VEC_PROPERTY_SPACE = 3; // ptr + cap + length
export const BASE_VECTOR_OFFSET = VEC_PROPERTY_SPACE * WORD_SIZE;
// this is a fork of @ethersproject/bytes:concat
// this collects individual dynamicData data and relocates it to top level
export function concatWithDynamicData(items: ReadonlyArray<BytesLike>): Uint8ArrayWithDynamicData {
const topLevelData: DynamicData = {};
let totalIndex = 0;
const objects = items.map((item) => {
const dynamicData = (item as Uint8ArrayWithDynamicData).dynamicData;
if (dynamicData) {
Object.entries(dynamicData).forEach(([pointerIndex, vData]) => {
topLevelData[parseInt(pointerIndex, 10) + totalIndex] = vData;
});
}
const byteArray = arrayify(item);
totalIndex += byteArray.byteLength / WORD_SIZE;
return byteArray;
});
const length = objects.reduce((accum, item) => accum + item.length, 0);
const result: Uint8ArrayWithDynamicData = new Uint8Array(length);
objects.reduce((offset, object) => {
result.set(object, offset);
return offset + object.length;
}, 0);
// store vector data and pointer indices, but only if data exist
if (Object.keys(topLevelData).length) {
result.dynamicData = topLevelData;
}
return result;
}
export function unpackDynamicData(
results: Uint8ArrayWithDynamicData,
baseOffset: number,
dataOffset: number
): Uint8Array {
if (!results.dynamicData) {
return concat([results]);
}
let cumulativeDynamicByteLength = 0;
let updatedResults = results;
Object.entries(results.dynamicData).forEach(([pointerIndex, vData]) => {
// update value of pointer
const pointerOffset = parseInt(pointerIndex, 10) * WORD_SIZE;
const adjustedValue = new U64Coder().encode(
dataOffset + baseOffset + cumulativeDynamicByteLength
);
updatedResults.set(adjustedValue, pointerOffset);
// append dynamic data at the end
const dataToAppend = vData.dynamicData
? // unpack child dynamic data
unpackDynamicData(
vData,
baseOffset,
dataOffset + vData.byteLength + cumulativeDynamicByteLength
)
: vData;
updatedResults = concat([updatedResults, dataToAppend]);
cumulativeDynamicByteLength += dataToAppend.byteLength;
});
return updatedResults;
}
/**
* Turns:
Uint8Array(24) [
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 24
]
Into:
Array [
Uint8Array(8) [
0, 0, 0, 0, 0, 0, 0, 1
],
Uint8Array(8) [
0, 0, 0, 0, 0, 0, 0, 2
],
Uint8Array(8) [
0, 0, 0, 0, 0, 0, 0, 24
]
]
*
*/
export const chunkByLength = (data: Uint8Array, length = WORD_SIZE): Uint8Array[] => {
const chunks = [];
let offset = 0;
let chunk = data.slice(offset, offset + length);
while (chunk.length) {
chunks.push(chunk);
offset += length;
chunk = data.slice(offset, offset + length);
}
return chunks;
};
/**
* Checks if a given type is a pointer type
* See: https://github.com/FuelLabs/sway/issues/1368
*/
export const isPointerType = (type: string) => {
switch (type) {
case 'u8':
case 'u16':
case 'u32':
case 'u64':
case 'bool': {
return false;
}
default: {
return true;
}
}
};
export const isHeapType = (type: string) => type === VEC_CODER_TYPE;
export function findOrThrow<T>(
arr: readonly T[],
predicate: (val: T) => boolean,
throwFn: () => never = () => {
throw new Error('element not found');
}
): T {
const found = arr.find(predicate);
if (found === undefined) throwFn();
return found;
}