feat: add decodeUnchecked to borsh-schema.ts (#17620)

This commit is contained in:
Lieu Zheng Hong 2021-06-02 19:43:01 +08:00 committed by GitHub
parent bbcdf073ba
commit d47990e753
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -1,5 +1,5 @@
import {Buffer} from 'buffer';
import {serialize, deserialize} from 'borsh';
import {serialize, deserialize, deserializeUnchecked} from 'borsh';
// Class wrapping a plain object
export class Struct {
@ -14,6 +14,10 @@ export class Struct {
static decode(data: Buffer): any {
return deserialize(SOLANA_SCHEMA, this, data);
}
static decodeUnchecked(data: Buffer): any {
return deserializeUnchecked(SOLANA_SCHEMA, this, data);
}
}
// Class representing a Rust-compatible enum, since enums are only strings or

View File

@ -228,4 +228,11 @@ describe('PublicKey', function () {
const decoded = PublicKey.decode(encoded);
expect(decoded.equals(publicKey)).to.be.true;
});
it('canBeDeserializedUncheckedWithBorsh', () => {
const publicKey = Keypair.generate().publicKey;
const encoded = Buffer.concat([publicKey.encode(), new Uint8Array(10)]);
const decoded = PublicKey.decodeUnchecked(encoded);
expect(decoded.equals(publicKey)).to.be.true;
});
});