forked from starknet-io/starknet.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstark.ts
73 lines (64 loc) · 2.6 KB
/
stark.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
import { getStarkKey, utils } from 'micro-starknet';
import { gzip, ungzip } from 'pako';
import { ArraySignatureType, BigNumberish, CompressedProgram, Program, Signature } from '../types';
import { addHexPrefix, arrayBufferToString, atobUniversal, btoaUniversal } from './encode';
import { parse, stringify } from './json';
import {
bigNumberishArrayToDecimalStringArray,
bigNumberishArrayToHexadecimalStringArray,
toBigInt,
toHex,
} from './num';
/**
* Function to compress compiled cairo program
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/services/api/gateway/transaction.py#L54-L58)
* @param jsonProgram - json file representing the compiled cairo program
* @returns Compressed cairo program
*/
export function compressProgram(jsonProgram: Program | string): CompressedProgram {
const stringified = typeof jsonProgram === 'string' ? jsonProgram : stringify(jsonProgram);
const compressedProgram = gzip(stringified);
return btoaUniversal(compressedProgram);
}
/**
* Function to decompress compressed compiled cairo program
*
* @param base64 CompressedProgram
* @returns parsed decompressed compiled cairo program
*/
export function decompressProgram(base64: CompressedProgram) {
if (Array.isArray(base64)) return base64;
const decompressed = arrayBufferToString(ungzip(atobUniversal(base64)));
return parse(decompressed);
}
export function randomAddress(): string {
const randomKeyPair = utils.randomPrivateKey();
return getStarkKey(randomKeyPair);
}
export function makeAddress(input: string): string {
return addHexPrefix(input).toLowerCase();
}
export function formatSignature(sig?: Signature): ArraySignatureType {
if (!sig) throw Error('formatSignature: provided signature is undefined');
if (Array.isArray(sig)) {
return sig.map((it) => toHex(it));
}
try {
const { r, s } = sig;
return [toHex(r), toHex(s)];
} catch (e) {
throw new Error('Signature need to be weierstrass.SignatureType or an array for custom');
}
}
export function signatureToDecimalArray(sig?: Signature): ArraySignatureType {
return bigNumberishArrayToDecimalStringArray(formatSignature(sig));
}
export function signatureToHexArray(sig?: Signature): ArraySignatureType {
return bigNumberishArrayToHexadecimalStringArray(formatSignature(sig));
}
export function estimatedFeeToMaxFee(estimatedFee: BigNumberish, overhead: number = 0.5): bigint {
// BN can only handle Integers, so we need to do all calulations with integers
const overHeadPercent = Math.round((1 + overhead) * 100);
return (toBigInt(estimatedFee) * toBigInt(overHeadPercent)) / 100n;
}