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:
34
web3.js/src/util/borsh-schema.ts
Normal file
34
web3.js/src/util/borsh-schema.ts
Normal 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();
|
Reference in New Issue
Block a user