Skip to content

Commit 2178692

Browse files
authored
add utils for safe computer (#384)
1 parent a64dae9 commit 2178692

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/client/utils/computer.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { parse } from 'uuid';
2+
import BigNumber from 'bignumber.js';
3+
import { base64RawURLEncode } from './base64';
4+
5+
export const MAX_BASE64_SIZE = 1644;
6+
7+
export const OperationTypeAddUser = 1;
8+
export const OperationTypeSystemCall = 2;
9+
10+
export const userIdToBytes = (uid: string) => {
11+
const x = BigNumber(uid);
12+
const bytes = [];
13+
let i = x;
14+
do {
15+
bytes.unshift(i.mod(256).toNumber());
16+
i = i.dividedToIntegerBy(256);
17+
} while (!i.isZero());
18+
do {
19+
bytes.unshift(0);
20+
} while (bytes.length < 8);
21+
return Buffer.from(bytes);
22+
};
23+
24+
// bytes of Solana transaction: Buffer.from(tx.serialize())
25+
export const checkSystemCallSize = (txBuf: Buffer) => txBuf.toString('base64').length < MAX_BASE64_SIZE;
26+
27+
export const buildSystemCallExtra = (uid: string, cid: string, skipPostProcess: boolean, fid?: string) => {
28+
const flag = skipPostProcess ? 1 : 0;
29+
const ib = userIdToBytes(uid);
30+
const cb = parse(cid);
31+
const data = [ib, cb, Buffer.from([flag])];
32+
if (fid) data.push(parse(fid));
33+
return Buffer.concat(data);
34+
};
35+
36+
export const buildComputerExtra = (operation: number, extra: Buffer) => Buffer.concat([Buffer.from([operation]), extra]);
37+
38+
export const encodeMtgExtra = (app_id: string, extra: Buffer) => {
39+
const data = Buffer.concat([parse(app_id), extra]);
40+
return base64RawURLEncode(data);
41+
};

0 commit comments

Comments
 (0)