Skip to content

Commit

Permalink
fix: btoa isomorphic
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Oct 21, 2021
1 parent cd373cb commit f3eb0f5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
16 changes: 6 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { gzip } from 'pako';
import { btoa, randomAddress } from './utils';
import { randomAddress, btoaUniversal } from './utils';
import type {
GetBlockResponse,
GetCode,
Expand Down Expand Up @@ -184,15 +184,11 @@ export function addTransaction(tx: Transaction): Promise<AddTransactionResponse>
}

export function compressProgram(program: string): CompressedProgram {
try {
const json = JSON.parse(program);
const stringified = JSON.stringify(json);
const compressedProgram = gzip(stringified);
const base64 = btoa(compressedProgram);
return base64;
} catch {
throw new Error('couldnt compress program');
}
const json = JSON.parse(program);
const stringified = JSON.stringify(json);
const compressedProgram = gzip(stringified);
const base64 = btoaUniversal(compressedProgram);
return base64;
}

export function deployContract(
Expand Down
5 changes: 4 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { CONTRACT_ADDRESS_LOWER_BOUND, CONTRACT_ADDRESS_UPPER_BOUND } from './constants';

export const btoa = (b: ArrayBuffer): string => Buffer.from(b).toString('base64');
const isBrowser = typeof window !== 'undefined';

export const btoaUniversal = (b: ArrayBuffer): string =>
isBrowser ? btoa(String.fromCharCode.apply(null, b as any)) : Buffer.from(b).toString('base64');

export function randomIntFromInterval(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1) + min);
Expand Down

0 comments on commit f3eb0f5

Please sign in to comment.