feat: add decodeUnchecked to borsh-schema.ts (#17620)
This commit is contained in:
parent
bbcdf073ba
commit
d47990e753
|
@ -1,5 +1,5 @@
|
||||||
import {Buffer} from 'buffer';
|
import {Buffer} from 'buffer';
|
||||||
import {serialize, deserialize} from 'borsh';
|
import {serialize, deserialize, deserializeUnchecked} from 'borsh';
|
||||||
|
|
||||||
// Class wrapping a plain object
|
// Class wrapping a plain object
|
||||||
export class Struct {
|
export class Struct {
|
||||||
|
@ -14,6 +14,10 @@ export class Struct {
|
||||||
static decode(data: Buffer): any {
|
static decode(data: Buffer): any {
|
||||||
return deserialize(SOLANA_SCHEMA, this, data);
|
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
|
// Class representing a Rust-compatible enum, since enums are only strings or
|
||||||
|
|
|
@ -228,4 +228,11 @@ describe('PublicKey', function () {
|
||||||
const decoded = PublicKey.decode(encoded);
|
const decoded = PublicKey.decode(encoded);
|
||||||
expect(decoded.equals(publicKey)).to.be.true;
|
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;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue