diff --git a/web3.js/src/util/borsh-schema.ts b/web3.js/src/util/borsh-schema.ts index 1c61fd1bc..cbceb8c2b 100644 --- a/web3.js/src/util/borsh-schema.ts +++ b/web3.js/src/util/borsh-schema.ts @@ -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 diff --git a/web3.js/test/publickey.test.ts b/web3.js/test/publickey.test.ts index b58a48b72..8bdb164fd 100644 --- a/web3.js/test/publickey.test.ts +++ b/web3.js/test/publickey.test.ts @@ -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; + }); });