diff --git a/ts/package.json b/ts/package.json index 05110902d..24c37df27 100644 --- a/ts/package.json +++ b/ts/package.json @@ -13,7 +13,7 @@ "node": ">=10" }, "scripts": { - "build": "yarn build:node", + "build": "rm -rf dist/ && yarn build:node", "build:node": "tsc && tsc -p tsconfig.cjs.json", "lint:fix": "prettier src/** -w", "watch": "tsc -p tsconfig.cjs.json --watch", diff --git a/ts/src/index.ts b/ts/src/index.ts index 366136559..26bb5c540 100644 --- a/ts/src/index.ts +++ b/ts/src/index.ts @@ -9,7 +9,7 @@ import Coder, { } from "./coder"; import { Idl } from "./idl"; import workspace from "./workspace"; -import utils from "./utils"; +import * as utils from "./utils"; import { Program } from "./program"; import { Address } from "./program/common"; import { Event } from "./program/event"; diff --git a/ts/src/program/index.ts b/ts/src/program/index.ts index 69c0cf403..e309e3c69 100644 --- a/ts/src/program/index.ts +++ b/ts/src/program/index.ts @@ -12,7 +12,7 @@ import NamespaceFactory, { SimulateNamespace, } from "./namespace"; import { getProvider } from "../"; -import { decodeUtf8 } from "../utils"; +import { utf8 } from "../utils/bytes"; import { EventParser } from "./event"; import { Address, translateAddress } from "./common"; @@ -300,7 +300,7 @@ export class Program { // Chop off account discriminator. let idlAccount = decodeIdlAccount(accountInfo.data.slice(8)); const inflatedIdl = inflate(idlAccount.data); - return JSON.parse(decodeUtf8(inflatedIdl)); + return JSON.parse(utf8.decode(inflatedIdl)); } /** diff --git a/ts/src/utils/bytes/base64.ts b/ts/src/utils/bytes/base64.ts new file mode 100644 index 000000000..33e936178 --- /dev/null +++ b/ts/src/utils/bytes/base64.ts @@ -0,0 +1,9 @@ +import * as base64 from "base64-js"; + +export function encode(data: Buffer): string { + return base64.fromByteArray(data); +} + +export function decode(data: string): Buffer { + return Buffer.from(base64.toByteArray(data)); +} diff --git a/ts/src/utils/bytes/bs58.ts b/ts/src/utils/bytes/bs58.ts new file mode 100644 index 000000000..08f5a5363 --- /dev/null +++ b/ts/src/utils/bytes/bs58.ts @@ -0,0 +1,9 @@ +import * as bs58 from "bs58"; + +export function encode(data: Buffer | number[] | Uint8Array) { + return bs58.encode(data); +} + +export function decode(data: string) { + return bs58.decode(data); +} diff --git a/ts/src/utils/bytes/hex.ts b/ts/src/utils/bytes/hex.ts new file mode 100644 index 000000000..30e0f27d9 --- /dev/null +++ b/ts/src/utils/bytes/hex.ts @@ -0,0 +1,23 @@ +export function encode(data: Buffer): string { + return data.reduce( + (str, byte) => str + byte.toString(16).padStart(2, "0"), + "0x" + ); +} + +export function decode(data: string): Buffer { + if (data.indexOf("0x") === 0) { + data = data.substr(2); + } + if (data.length % 2 === 1) { + data = "0" + data; + } + + let key = data.match(/.{2}/g); + + if (key === null) { + return Buffer.from([]); + } + + return Buffer.from(key.map((byte) => parseInt(byte, 16))); +} diff --git a/ts/src/utils/bytes/index.ts b/ts/src/utils/bytes/index.ts new file mode 100644 index 000000000..3613a9595 --- /dev/null +++ b/ts/src/utils/bytes/index.ts @@ -0,0 +1,4 @@ +export * as hex from "./hex"; +export * as utf8 from "./utf8"; +export * as bs58 from "./bs58"; +export * as base64 from "./base64"; diff --git a/ts/src/utils/bytes/utf8.ts b/ts/src/utils/bytes/utf8.ts new file mode 100644 index 000000000..2a20190ab --- /dev/null +++ b/ts/src/utils/bytes/utf8.ts @@ -0,0 +1,15 @@ +export function decode(array: Uint8Array): string { + const decoder = + typeof TextDecoder === "undefined" + ? new (require("util").TextDecoder)("utf-8") // Node. + : new TextDecoder("utf-8"); // Browser. + return decoder.decode(array); +} + +export function encode(input: string): Uint8Array { + const encoder = + typeof TextEncoder === "undefined" + ? new (require("util").TextEncoder)("utf-8") // Node. + : new TextEncoder(); // Browser. + return encoder.encode(input); +} diff --git a/ts/src/utils/index.ts b/ts/src/utils/index.ts index 9d8ee8319..a6565c7b6 100644 --- a/ts/src/utils/index.ts +++ b/ts/src/utils/index.ts @@ -1,14 +1,4 @@ -import { sha256 } from "crypto-hash"; -import * as bs58 from "bs58"; -import * as rpc from "./rpc"; -import * as publicKey from "./pubkey"; - -export function decodeUtf8(array: Uint8Array): string { - const decoder = - typeof TextDecoder === "undefined" - ? new (require("util").TextDecoder)("utf-8") // Node. - : new TextDecoder("utf-8"); // Browser. - return decoder.decode(array); -} - -export default { sha256, bs58, rpc, publicKey }; +export * as sha256 from "./sha256"; +export * as rpc from "./rpc"; +export * as publicKey from "./pubkey"; +export * as bytes from "./bytes"; diff --git a/ts/src/utils/sha256.ts b/ts/src/utils/sha256.ts new file mode 100644 index 000000000..64a6c23b6 --- /dev/null +++ b/ts/src/utils/sha256.ts @@ -0,0 +1,5 @@ +import { sha256 } from "js-sha256"; + +export function hash(data: string): string { + return sha256(data); +}