Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion borsh-ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,11 @@ function serializeField(schema: Schema, fieldName: string, value: any, fieldType
}
}

function serializeStruct(schema: Schema, obj: any, writer: any) {
function serializeStruct(schema: Schema, obj: any, writer: BinaryWriter) {
if (typeof obj.borshSerialize === 'function') {
obj.borshSerialize(writer);
return;
}
const structSchema = schema.get(obj.constructor);
if (!structSchema) {
throw new BorshError(`Class ${obj.constructor.name} is missing in schema`);
Expand Down Expand Up @@ -343,6 +347,10 @@ function deserializeField(schema: Schema, fieldName: string, fieldType: any, rea
}

function deserializeStruct(schema: Schema, classType: any, reader: BinaryReader) {
if (typeof classType.borshDeserialize === 'function') {
return classType.borshDeserialize(reader);
}

const structSchema = schema.get(classType);
if (!structSchema) {
throw new BorshError(`Class ${classType.name} is missing in schema`);
Expand Down
23 changes: 23 additions & 0 deletions borsh-ts/test/serialize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ class Assignable {

class Test extends Assignable { }

class Serializable {
constructor(data) {
this.data = data;
}

static borshDeserialize(reader) {
return new Serializable(reader.readU8());
}

borshSerialize(writer) {
writer.writeU8(this.data);
}
}

test('serialize object', async () => {
const value = new Test({ x: 255, y: 20, z: '123', q: [1, 2, 3] });
const schema = new Map([[Test, { kind: 'struct', fields: [['x', 'u8'], ['y', 'u64'], ['z', 'string'], ['q', [3]]] }]]);
Expand Down Expand Up @@ -68,6 +82,15 @@ test('serialize max uint', async () => {
expect(newValue.t.toString()).toEqual('13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084095');
});

test('serialize/deserialize with class methods', () => {
const item = new Serializable(10);

const buf = borsh.serialize(null, item);
const newValue = borsh.deserialize(null, Serializable, buf);

expect(newValue).toEqual(item);
});

test('baseEncode string test', async () => {
const encodedValue = borsh.baseEncode("244ZQ9cgj3CQ6bWBdytfrJMuMQ1jdXLFGnr4HhvtCTnM");
const expectedValue = "HKk9gqNj4xb4rLdJuzT5zzJbLa4vHBdYCxQT9H99csQh6nz3Hfpqn4jtWA92";
Expand Down
7 changes: 7 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ function serializeField(schema, fieldName, value, fieldType, writer) {
}
}
function serializeStruct(schema, obj, writer) {
if (typeof obj.borshSerialize === 'function') {
obj.borshSerialize(writer);
return;
}
const structSchema = schema.get(obj.constructor);
if (!structSchema) {
throw new BorshError(`Class ${obj.constructor.name} is missing in schema`);
Expand Down Expand Up @@ -351,6 +355,9 @@ function deserializeField(schema, fieldName, fieldType, reader) {
}
}
function deserializeStruct(schema, classType, reader) {
if (typeof classType.borshDeserialize === 'function') {
return classType.borshDeserialize(reader);
}
const structSchema = schema.get(classType);
if (!structSchema) {
throw new BorshError(`Class ${classType.name} is missing in schema`);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"devDependencies": {
"@types/babel__core": "^7.1.2",
"@types/babel__template": "^7.0.2",
"@types/bn.js": "^5.1.0",
"@types/node": "^12.7.3",
"@typescript-eslint/eslint-plugin": "^2.18.0",
"@typescript-eslint/parser": "^2.18.0",
Expand All @@ -45,8 +46,7 @@
"typescript": "^3.6.2"
},
"dependencies": {
"@types/bn.js": "^4.11.5",
"bn.js": "^5.0.0",
"bn.js": "^5.2.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice cleanup!

"bs58": "^4.0.0",
"text-encoding-utf-8": "^1.0.2"
}
Expand Down