From f1c9d5d9a4dae6f02b964ef74c36fdbc9ba80209 Mon Sep 17 00:00:00 2001 From: Vladimir Grichina Date: Thu, 24 Dec 2020 13:43:12 -0800 Subject: [PATCH] Use actual shim (accidentally undid in previous commit) --- lib/utils/serialize.d.ts | 42 +---- lib/utils/serialize.js | 346 +-------------------------------------- src/utils/serialize.ts | 336 ++----------------------------------- 3 files changed, 19 insertions(+), 705 deletions(-) diff --git a/lib/utils/serialize.d.ts b/lib/utils/serialize.d.ts index 6db96b5810..b5bb639a95 100644 --- a/lib/utils/serialize.d.ts +++ b/lib/utils/serialize.d.ts @@ -1,41 +1 @@ -/// -import BN from 'bn.js'; -export declare function base_encode(value: Uint8Array | string): string; -export declare function base_decode(value: string): Uint8Array; -export declare type Schema = Map; -export declare class BorshError extends Error { - originalMessage: string; - fieldPath: string[]; - constructor(message: string); - addToFieldPath(fieldName: string): void; -} -export declare class BinaryWriter { - buf: Buffer; - length: number; - constructor(); - maybe_resize(): void; - write_u8(value: number): void; - write_u32(value: number): void; - write_u64(value: BN): void; - write_u128(value: BN): void; - private write_buffer; - write_string(str: string): void; - write_fixed_array(array: Uint8Array): void; - write_array(array: any[], fn: any): void; - toArray(): Uint8Array; -} -export declare class BinaryReader { - buf: Buffer; - offset: number; - constructor(buf: Buffer); - read_u8(): number; - read_u32(): number; - read_u64(): BN; - read_u128(): BN; - private read_buffer; - read_string(): string; - read_fixed_array(len: number): Uint8Array; - read_array(fn: any): any[]; -} -export declare function serialize(schema: Schema, obj: any): Uint8Array; -export declare function deserialize(schema: Schema, classType: any, buffer: Buffer): any; +export { baseEncode as base_encode, baseDecode as base_decode, serialize, deserialize, Schema, BorshError, BinaryWriter, BinaryReader, } from 'borsh'; diff --git a/lib/utils/serialize.js b/lib/utils/serialize.js index f386af1419..acbb68add0 100644 --- a/lib/utils/serialize.js +++ b/lib/utils/serialize.js @@ -1,340 +1,10 @@ "use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { - var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; - if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); - else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; - return c > 3 && r && Object.defineProperty(target, key, r), r; -}; -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; Object.defineProperty(exports, "__esModule", { value: true }); -exports.deserialize = exports.serialize = exports.BinaryReader = exports.BinaryWriter = exports.BorshError = exports.base_decode = exports.base_encode = void 0; -const bs58_1 = __importDefault(require("bs58")); -const bn_js_1 = __importDefault(require("bn.js")); -// TODO: Make sure this polyfill not included when not required -const encoding = __importStar(require("text-encoding-utf-8")); -const TextDecoder = (typeof global.TextDecoder !== 'function') ? encoding.TextDecoder : global.TextDecoder; -const textDecoder = new TextDecoder('utf-8', { fatal: true }); -function base_encode(value) { - if (typeof (value) === 'string') { - value = Buffer.from(value, 'utf8'); - } - return bs58_1.default.encode(Buffer.from(value)); -} -exports.base_encode = base_encode; -function base_decode(value) { - return Buffer.from(bs58_1.default.decode(value)); -} -exports.base_decode = base_decode; -const INITIAL_LENGTH = 1024; -class BorshError extends Error { - constructor(message) { - super(message); - this.fieldPath = []; - this.originalMessage = message; - } - addToFieldPath(fieldName) { - this.fieldPath.splice(0, 0, fieldName); - // NOTE: Modifying message directly as jest doesn't use .toString() - this.message = this.originalMessage + ': ' + this.fieldPath.join('.'); - } -} -exports.BorshError = BorshError; -/// Binary encoder. -class BinaryWriter { - constructor() { - this.buf = Buffer.alloc(INITIAL_LENGTH); - this.length = 0; - } - maybe_resize() { - if (this.buf.length < 16 + this.length) { - this.buf = Buffer.concat([this.buf, Buffer.alloc(INITIAL_LENGTH)]); - } - } - write_u8(value) { - this.maybe_resize(); - this.buf.writeUInt8(value, this.length); - this.length += 1; - } - write_u32(value) { - this.maybe_resize(); - this.buf.writeUInt32LE(value, this.length); - this.length += 4; - } - write_u64(value) { - this.maybe_resize(); - this.write_buffer(Buffer.from(new bn_js_1.default(value).toArray('le', 8))); - } - write_u128(value) { - this.maybe_resize(); - this.write_buffer(Buffer.from(new bn_js_1.default(value).toArray('le', 16))); - } - write_buffer(buffer) { - // Buffer.from is needed as this.buf.subarray can return plain Uint8Array in browser - this.buf = Buffer.concat([Buffer.from(this.buf.subarray(0, this.length)), buffer, Buffer.alloc(INITIAL_LENGTH)]); - this.length += buffer.length; - } - write_string(str) { - this.maybe_resize(); - const b = Buffer.from(str, 'utf8'); - this.write_u32(b.length); - this.write_buffer(b); - } - write_fixed_array(array) { - this.write_buffer(Buffer.from(array)); - } - write_array(array, fn) { - this.maybe_resize(); - this.write_u32(array.length); - for (const elem of array) { - this.maybe_resize(); - fn(elem); - } - } - toArray() { - return this.buf.subarray(0, this.length); - } -} -exports.BinaryWriter = BinaryWriter; -function handlingRangeError(target, propertyKey, propertyDescriptor) { - const originalMethod = propertyDescriptor.value; - propertyDescriptor.value = function (...args) { - try { - return originalMethod.apply(this, args); - } - catch (e) { - if (e instanceof RangeError) { - const code = e.code; - if (['ERR_BUFFER_OUT_OF_BOUNDS', 'ERR_OUT_OF_RANGE'].indexOf(code) >= 0) { - throw new BorshError('Reached the end of buffer when deserializing'); - } - } - throw e; - } - }; -} -class BinaryReader { - constructor(buf) { - this.buf = buf; - this.offset = 0; - } - read_u8() { - const value = this.buf.readUInt8(this.offset); - this.offset += 1; - return value; - } - read_u32() { - const value = this.buf.readUInt32LE(this.offset); - this.offset += 4; - return value; - } - read_u64() { - const buf = this.read_buffer(8); - return new bn_js_1.default(buf, 'le'); - } - read_u128() { - const buf = this.read_buffer(16); - return new bn_js_1.default(buf, 'le'); - } - read_buffer(len) { - if ((this.offset + len) > this.buf.length) { - throw new BorshError(`Expected buffer length ${len} isn't within bounds`); - } - const result = this.buf.slice(this.offset, this.offset + len); - this.offset += len; - return result; - } - read_string() { - const len = this.read_u32(); - const buf = this.read_buffer(len); - try { - // NOTE: Using TextDecoder to fail on invalid UTF-8 - return textDecoder.decode(buf); - } - catch (e) { - throw new BorshError(`Error decoding UTF-8 string: ${e}`); - } - } - read_fixed_array(len) { - return new Uint8Array(this.read_buffer(len)); - } - read_array(fn) { - const len = this.read_u32(); - const result = Array(); - for (let i = 0; i < len; ++i) { - result.push(fn()); - } - return result; - } -} -__decorate([ - handlingRangeError -], BinaryReader.prototype, "read_u8", null); -__decorate([ - handlingRangeError -], BinaryReader.prototype, "read_u32", null); -__decorate([ - handlingRangeError -], BinaryReader.prototype, "read_u64", null); -__decorate([ - handlingRangeError -], BinaryReader.prototype, "read_u128", null); -__decorate([ - handlingRangeError -], BinaryReader.prototype, "read_string", null); -__decorate([ - handlingRangeError -], BinaryReader.prototype, "read_fixed_array", null); -__decorate([ - handlingRangeError -], BinaryReader.prototype, "read_array", null); -exports.BinaryReader = BinaryReader; -function serializeField(schema, fieldName, value, fieldType, writer) { - try { - // TODO: Handle missing values properly (make sure they never result in just skipped write) - if (typeof fieldType === 'string') { - writer[`write_${fieldType}`](value); - } - else if (fieldType instanceof Array) { - if (typeof fieldType[0] === 'number') { - if (value.length !== fieldType[0]) { - throw new BorshError(`Expecting byte array of length ${fieldType[0]}, but got ${value.length} bytes`); - } - writer.write_fixed_array(value); - } - else { - writer.write_array(value, (item) => { serializeField(schema, fieldName, item, fieldType[0], writer); }); - } - } - else if (fieldType.kind !== undefined) { - switch (fieldType.kind) { - case 'option': { - if (value === null) { - writer.write_u8(0); - } - else { - writer.write_u8(1); - serializeField(schema, fieldName, value, fieldType.type, writer); - } - break; - } - default: throw new BorshError(`FieldType ${fieldType} unrecognized`); - } - } - else { - serializeStruct(schema, value, writer); - } - } - catch (error) { - if (error instanceof BorshError) { - error.addToFieldPath(fieldName); - } - throw error; - } -} -function serializeStruct(schema, obj, writer) { - const structSchema = schema.get(obj.constructor); - if (!structSchema) { - throw new BorshError(`Class ${obj.constructor.name} is missing in schema`); - } - if (structSchema.kind === 'struct') { - structSchema.fields.map(([fieldName, fieldType]) => { - serializeField(schema, fieldName, obj[fieldName], fieldType, writer); - }); - } - else if (structSchema.kind === 'enum') { - const name = obj[structSchema.field]; - for (let idx = 0; idx < structSchema.values.length; ++idx) { - const [fieldName, fieldType] = structSchema.values[idx]; - if (fieldName === name) { - writer.write_u8(idx); - serializeField(schema, fieldName, obj[fieldName], fieldType, writer); - break; - } - } - } - else { - throw new BorshError(`Unexpected schema kind: ${structSchema.kind} for ${obj.constructor.name}`); - } -} -/// Serialize given object using schema of the form: -/// { class_name -> [ [field_name, field_type], .. ], .. } -function serialize(schema, obj) { - const writer = new BinaryWriter(); - serializeStruct(schema, obj, writer); - return writer.toArray(); -} -exports.serialize = serialize; -function deserializeField(schema, fieldName, fieldType, reader) { - try { - if (typeof fieldType === 'string') { - return reader[`read_${fieldType}`](); - } - if (fieldType instanceof Array) { - if (typeof fieldType[0] === 'number') { - return reader.read_fixed_array(fieldType[0]); - } - return reader.read_array(() => deserializeField(schema, fieldName, fieldType[0], reader)); - } - return deserializeStruct(schema, fieldType, reader); - } - catch (error) { - if (error instanceof BorshError) { - error.addToFieldPath(fieldName); - } - throw error; - } -} -function deserializeStruct(schema, classType, reader) { - const structSchema = schema.get(classType); - if (!structSchema) { - throw new BorshError(`Class ${classType.name} is missing in schema`); - } - if (structSchema.kind === 'struct') { - const result = {}; - for (const [fieldName, fieldType] of schema.get(classType).fields) { - result[fieldName] = deserializeField(schema, fieldName, fieldType, reader); - } - return new classType(result); - } - if (structSchema.kind === 'enum') { - const idx = reader.read_u8(); - if (idx >= structSchema.values.length) { - throw new BorshError(`Enum index: ${idx} is out of range`); - } - const [fieldName, fieldType] = structSchema.values[idx]; - const fieldValue = deserializeField(schema, fieldName, fieldType, reader); - return new classType({ [fieldName]: fieldValue }); - } - throw new BorshError(`Unexpected schema kind: ${structSchema.kind} for ${classType.constructor.name}`); -} -/// Deserializes object from bytes using schema. -function deserialize(schema, classType, buffer) { - const reader = new BinaryReader(buffer); - const result = deserializeStruct(schema, classType, reader); - if (reader.offset < buffer.length) { - throw new BorshError(`Unexpected ${buffer.length - reader.offset} bytes after deserialized data`); - } - return result; -} -exports.deserialize = deserialize; +var borsh_1 = require("borsh"); +Object.defineProperty(exports, "base_encode", { enumerable: true, get: function () { return borsh_1.baseEncode; } }); +Object.defineProperty(exports, "base_decode", { enumerable: true, get: function () { return borsh_1.baseDecode; } }); +Object.defineProperty(exports, "serialize", { enumerable: true, get: function () { return borsh_1.serialize; } }); +Object.defineProperty(exports, "deserialize", { enumerable: true, get: function () { return borsh_1.deserialize; } }); +Object.defineProperty(exports, "BorshError", { enumerable: true, get: function () { return borsh_1.BorshError; } }); +Object.defineProperty(exports, "BinaryWriter", { enumerable: true, get: function () { return borsh_1.BinaryWriter; } }); +Object.defineProperty(exports, "BinaryReader", { enumerable: true, get: function () { return borsh_1.BinaryReader; } }); diff --git a/src/utils/serialize.ts b/src/utils/serialize.ts index 179a32c850..8fa8a2542e 100644 --- a/src/utils/serialize.ts +++ b/src/utils/serialize.ts @@ -1,326 +1,10 @@ -import bs58 from 'bs58'; -import BN from 'bn.js'; - -// TODO: Make sure this polyfill not included when not required -import * as encoding from 'text-encoding-utf-8'; -const TextDecoder = (typeof (global as any).TextDecoder !== 'function') ? encoding.TextDecoder : (global as any).TextDecoder; -const textDecoder = new TextDecoder('utf-8', { fatal: true }); - -export function base_encode(value: Uint8Array | string): string { - if (typeof(value) === 'string') { - value = Buffer.from(value, 'utf8'); - } - return bs58.encode(Buffer.from(value)); -} - -export function base_decode(value: string): Uint8Array { - return Buffer.from(bs58.decode(value)); -} - -const INITIAL_LENGTH = 1024; - -export type Schema = Map; - -export class BorshError extends Error { - originalMessage: string; - fieldPath: string[] = []; - - constructor(message: string) { - super(message); - this.originalMessage = message; - } - - addToFieldPath(fieldName: string) { - this.fieldPath.splice(0, 0, fieldName); - // NOTE: Modifying message directly as jest doesn't use .toString() - this.message = this.originalMessage + ': ' + this.fieldPath.join('.'); - } -} - -/// Binary encoder. -export class BinaryWriter { - buf: Buffer; - length: number; - - public constructor() { - this.buf = Buffer.alloc(INITIAL_LENGTH); - this.length = 0; - } - - maybe_resize() { - if (this.buf.length < 16 + this.length) { - this.buf = Buffer.concat([this.buf, Buffer.alloc(INITIAL_LENGTH)]); - } - } - - public write_u8(value: number) { - this.maybe_resize(); - this.buf.writeUInt8(value, this.length); - this.length += 1; - } - - public write_u32(value: number) { - this.maybe_resize(); - this.buf.writeUInt32LE(value, this.length); - this.length += 4; - } - - public write_u64(value: BN) { - this.maybe_resize(); - this.write_buffer(Buffer.from(new BN(value).toArray('le', 8))); - } - - public write_u128(value: BN) { - this.maybe_resize(); - this.write_buffer(Buffer.from(new BN(value).toArray('le', 16))); - } - - private write_buffer(buffer: Buffer) { - // Buffer.from is needed as this.buf.subarray can return plain Uint8Array in browser - this.buf = Buffer.concat([Buffer.from(this.buf.subarray(0, this.length)), buffer, Buffer.alloc(INITIAL_LENGTH)]); - this.length += buffer.length; - } - - public write_string(str: string) { - this.maybe_resize(); - const b = Buffer.from(str, 'utf8'); - this.write_u32(b.length); - this.write_buffer(b); - } - - public write_fixed_array(array: Uint8Array) { - this.write_buffer(Buffer.from(array)); - } - - public write_array(array: any[], fn: any) { - this.maybe_resize(); - this.write_u32(array.length); - for (const elem of array) { - this.maybe_resize(); - fn(elem); - } - } - - public toArray(): Uint8Array { - return this.buf.subarray(0, this.length); - } -} - -function handlingRangeError(target: any, propertyKey: string, propertyDescriptor: PropertyDescriptor) { - const originalMethod = propertyDescriptor.value; - propertyDescriptor.value = function(...args: any[]) { - try { - return originalMethod.apply(this, args); - } catch (e) { - if (e instanceof RangeError ) { - const code = (e as any).code; - if (['ERR_BUFFER_OUT_OF_BOUNDS', 'ERR_OUT_OF_RANGE'].indexOf(code) >= 0) { - throw new BorshError('Reached the end of buffer when deserializing'); - } - } - throw e; - } - }; -} - -export class BinaryReader { - buf: Buffer; - offset: number; - - public constructor(buf: Buffer) { - this.buf = buf; - this.offset = 0; - } - - @handlingRangeError - read_u8(): number { - const value = this.buf.readUInt8(this.offset); - this.offset += 1; - return value; - } - - @handlingRangeError - read_u32(): number { - const value = this.buf.readUInt32LE(this.offset); - this.offset += 4; - return value; - } - - @handlingRangeError - read_u64(): BN { - const buf = this.read_buffer(8); - return new BN(buf, 'le'); - } - - @handlingRangeError - read_u128(): BN { - const buf = this.read_buffer(16); - return new BN(buf, 'le'); - } - - private read_buffer(len: number): Buffer { - if ((this.offset + len) > this.buf.length) { - throw new BorshError(`Expected buffer length ${len} isn't within bounds`); - } - const result = this.buf.slice(this.offset, this.offset + len); - this.offset += len; - return result; - } - - @handlingRangeError - read_string(): string { - const len = this.read_u32(); - const buf = this.read_buffer(len); - try { - // NOTE: Using TextDecoder to fail on invalid UTF-8 - return textDecoder.decode(buf); - } catch (e) { - throw new BorshError(`Error decoding UTF-8 string: ${e}`); - } - } - - @handlingRangeError - read_fixed_array(len: number): Uint8Array { - return new Uint8Array(this.read_buffer(len)); - } - - @handlingRangeError - read_array(fn: any): any[] { - const len = this.read_u32(); - const result = Array(); - for (let i = 0; i < len; ++i) { - result.push(fn()); - } - return result; - } -} - -function serializeField(schema: Schema, fieldName: string, value: any, fieldType: any, writer: any) { - try { - // TODO: Handle missing values properly (make sure they never result in just skipped write) - if (typeof fieldType === 'string') { - writer[`write_${fieldType}`](value); - } else if (fieldType instanceof Array) { - if (typeof fieldType[0] === 'number') { - if (value.length !== fieldType[0]) { - throw new BorshError(`Expecting byte array of length ${fieldType[0]}, but got ${value.length} bytes`); - } - writer.write_fixed_array(value); - } else { - writer.write_array(value, (item: any) => { serializeField(schema, fieldName, item, fieldType[0], writer); }); - } - } else if (fieldType.kind !== undefined) { - switch (fieldType.kind) { - case 'option': { - if (value === null) { - writer.write_u8(0); - } else { - writer.write_u8(1); - serializeField(schema, fieldName, value, fieldType.type, writer); - } - break; - } - default: throw new BorshError(`FieldType ${fieldType} unrecognized`); - } - } else { - serializeStruct(schema, value, writer); - } - } catch (error) { - if (error instanceof BorshError) { - error.addToFieldPath(fieldName); - } - throw error; - } -} - -function serializeStruct(schema: Schema, obj: any, writer: any) { - const structSchema = schema.get(obj.constructor); - if (!structSchema) { - throw new BorshError(`Class ${obj.constructor.name} is missing in schema`); - } - if (structSchema.kind === 'struct') { - structSchema.fields.map(([fieldName, fieldType]: [any, any]) => { - serializeField(schema, fieldName, obj[fieldName], fieldType, writer); - }); - } else if (structSchema.kind === 'enum') { - const name = obj[structSchema.field]; - for (let idx = 0; idx < structSchema.values.length; ++idx) { - const [fieldName, fieldType]: [any, any] = structSchema.values[idx]; - if (fieldName === name) { - writer.write_u8(idx); - serializeField(schema, fieldName, obj[fieldName], fieldType, writer); - break; - } - } - } else { - throw new BorshError(`Unexpected schema kind: ${structSchema.kind} for ${obj.constructor.name}`); - } -} - -/// Serialize given object using schema of the form: -/// { class_name -> [ [field_name, field_type], .. ], .. } -export function serialize(schema: Schema, obj: any): Uint8Array { - const writer = new BinaryWriter(); - serializeStruct(schema, obj, writer); - return writer.toArray(); -} - -function deserializeField(schema: Schema, fieldName: string, fieldType: any, reader: BinaryReader): any { - try { - if (typeof fieldType === 'string') { - return reader[`read_${fieldType}`](); - } - - if (fieldType instanceof Array) { - if (typeof fieldType[0] === 'number') { - return reader.read_fixed_array(fieldType[0]); - } - - return reader.read_array(() => deserializeField(schema, fieldName, fieldType[0], reader)); - } - - return deserializeStruct(schema, fieldType, reader); - } catch (error) { - if (error instanceof BorshError) { - error.addToFieldPath(fieldName); - } - throw error; - } -} - -function deserializeStruct(schema: Schema, classType: any, reader: BinaryReader) { - const structSchema = schema.get(classType); - if (!structSchema) { - throw new BorshError(`Class ${classType.name} is missing in schema`); - } - - if (structSchema.kind === 'struct') { - const result = {}; - for (const [fieldName, fieldType] of schema.get(classType).fields) { - result[fieldName] = deserializeField(schema, fieldName, fieldType, reader); - } - return new classType(result); - } - - if (structSchema.kind === 'enum') { - const idx = reader.read_u8(); - if (idx >= structSchema.values.length) { - throw new BorshError(`Enum index: ${idx} is out of range`); - } - const [fieldName, fieldType] = structSchema.values[idx]; - const fieldValue = deserializeField(schema, fieldName, fieldType, reader); - return new classType({ [fieldName]: fieldValue }); - } - - throw new BorshError(`Unexpected schema kind: ${structSchema.kind} for ${classType.constructor.name}`); -} - -/// Deserializes object from bytes using schema. -export function deserialize(schema: Schema, classType: any, buffer: Buffer): any { - const reader = new BinaryReader(buffer); - const result = deserializeStruct(schema, classType, reader); - if (reader.offset < buffer.length) { - throw new BorshError(`Unexpected ${buffer.length - reader.offset} bytes after deserialized data`); - } - return result; -} +export { + baseEncode as base_encode, + baseDecode as base_decode, + serialize, + deserialize, + Schema, + BorshError, + BinaryWriter, + BinaryReader, +} from 'borsh'; \ No newline at end of file