ts: Add bytes to utils namespace (#330)

This commit is contained in:
Armani Ferrante 2021-05-26 23:55:21 -07:00 committed by GitHub
parent 2f780e0d27
commit 617d10aef2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 73 additions and 18 deletions

View File

@ -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",

View File

@ -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";

View File

@ -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));
}
/**

View File

@ -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));
}

View File

@ -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);
}

23
ts/src/utils/bytes/hex.ts Normal file
View File

@ -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)));
}

View File

@ -0,0 +1,4 @@
export * as hex from "./hex";
export * as utf8 from "./utf8";
export * as bs58 from "./bs58";
export * as base64 from "./base64";

View File

@ -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);
}

View File

@ -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";

5
ts/src/utils/sha256.ts Normal file
View File

@ -0,0 +1,5 @@
import { sha256 } from "js-sha256";
export function hash(data: string): string {
return sha256(data);
}