near / borsh-js

TypeScript/JavaScript implementation of Binary Object Representation Serializer for Hashing
Apache License 2.0
112 stars 38 forks source link

Unhandled Runtime Error - TypeError: reader[capitalizeFirstLetter(...)] is not a function #34

Closed GrimLothar closed 3 years ago

GrimLothar commented 3 years ago

Hi! I'm trying to use the deserializeUnchecked method but it seems to be failing with this error, any idea what might be going on? Does borsh work in browsers or only node environments?

Screen Shot 2021-09-17 at 12 07 58 PM !

Thank you

ymc182 commented 3 years ago

I would like to add my use case as well, I am using node environment with metaplex metadata , throwing error when serializing the metadata image

GrimLothar commented 3 years ago

That would answer my last question on wether borsh was node env only.

ymc182 commented 3 years ago

I have solved it myself , you might want to see if you have import and called borch.ts extend methods in common/src/util , metaplex extended custom writter and reader for those pubkey addresses

Xavier59 commented 3 years ago

borsh.ts :

import { PublicKey } from '@solana/web3.js';
import { BinaryReader, BinaryWriter } from 'borsh';
import basex from 'base-x';
export type StringPublicKey = string;

export const extendBorsh = () => {
  (BinaryReader.prototype as any).readPubkey = function () {
    const reader = this as unknown as BinaryReader;
    const array = reader.readFixedArray(32);
    return new PublicKey(array);
  };

  (BinaryWriter.prototype as any).writePubkey = function (value: PublicKey) {
    const writer = this as unknown as BinaryWriter;
    writer.writeFixedArray(value.toBuffer());
  };

  (BinaryReader.prototype as any).readPubkeyAsString = function () {
    const reader = this as unknown as BinaryReader;
    const array = reader.readFixedArray(32);
    return basex('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz').encode(array) as StringPublicKey;
  };

  (BinaryWriter.prototype as any).writePubkeyAsString = function (
    value: StringPublicKey,
  ) {
    const writer = this as unknown as BinaryWriter;
    writer.writeFixedArray(basex('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz').decode(value));
  };
};

extendBorsh();

In your metadata.ts : export * from './borsh';

GrimLothar commented 3 years ago

Like @Xavier59 and @ymc182 commented, looks like i forgot to include this file: https://github.com/metaplex-foundation/metaplex/blob/81023eb3e52c31b605e1dcf2eb1e7425153600cd/js/packages/common/src/utils/borsh.ts

Looks like reader and writer are being added to borsh in there

All good!