What do you think about allowing classes to provide custom serialize/deserialize methods?
By adding something like this to serializeStruct
function serializeStruct(schema: Schema, obj: any, writer: any) {
if (typeof obj.borshSerialize === 'function') {
obj.borshSerialize(writer);
return;
}
.....
}
.....
function deserializeStruct(
schema: Schema,
classType: any,
reader: BinaryReader
) {
if(typeof classType.borshDeserialize) {
classType.borshDeserialize(reader);
return;
}
....
}
we could then do this:
export class PublicKey {
keyType: KeyType;
data: Uint8Array;
static borsheDeserialize(reader: BinaryReader) {
reader.readU8
}
static borshDeserialize(reader: BinaryReader): PublicKey {
const keyType = reader.readU8();
let data;
if(keyType === KeyType.ED25519) {
data = reader.readFixedArray(32);
} else if(keyType === KeyType.SECP256k1) {
data = reader.readFixedArray(64);
}
return new PublicKey({ keyType, data });
}
borshSerialize(writer: BinaryWriter) {
writer.writeU8(this.keyType);
writer.writeFixedArray(this.data);
}
}
If this looks good I'm happy to implement it and submit a PR.