feat: add borsh utilities and public key support (#17239)

* feat: add borsh utilities and public key support

* fix: make bn internal for flow

* fix: add Buffer import in borsh file
This commit is contained in:
Jon Cinque
2021-05-18 19:33:06 +02:00
committed by GitHub
parent 75335b4f58
commit 00e198d169
7 changed files with 140 additions and 13 deletions

View File

@@ -0,0 +1,34 @@
import {Buffer} from 'buffer';
import {serialize, deserialize} from 'borsh';
// Class wrapping a plain object
export class Struct {
constructor(properties: any) {
Object.assign(this, properties);
}
encode(): Buffer {
return Buffer.from(serialize(SOLANA_SCHEMA, this));
}
static decode(data: Buffer): any {
return deserialize(SOLANA_SCHEMA, this, data);
}
}
// Class representing a Rust-compatible enum, since enums are only strings or
// numbers in pure JS
export class Enum extends Struct {
enum: string = '';
constructor(properties: any) {
super(properties);
if (Object.keys(properties).length !== 1) {
throw new Error('Enum can only take single value');
}
Object.keys(properties).map(key => {
this.enum = key;
});
}
}
export const SOLANA_SCHEMA: Map<Function, any> = new Map();