From 9eb34dc0641fe6a432241a1e214e24f472b921f2 Mon Sep 17 00:00:00 2001 From: DR497 <47689875+dr497@users.noreply.github.com> Date: Thu, 18 Jan 2024 19:24:42 +0800 Subject: [PATCH] upgrade to borsh `1.0.0` --- js/package-lock.json | 278 +++++++++++++++++++++++++++++------ js/package.json | 2 +- js/src/favorite-domain.ts | 21 +-- js/src/instructions.ts | 100 +++++-------- js/src/nft/name-tokenizer.ts | 26 ++-- js/src/record.ts | 4 +- js/src/state.ts | 42 ++---- js/src/twitter_bindings.ts | 38 ++--- 8 files changed, 318 insertions(+), 193 deletions(-) diff --git a/js/package-lock.json b/js/package-lock.json index 170a1d86..ef6264df 100644 --- a/js/package-lock.json +++ b/js/package-lock.json @@ -14,7 +14,7 @@ "@scure/base": "^1.1.5", "@solana/buffer-layout": "^4.0.1", "@solana/spl-token": "0.3.9", - "borsh": "^0.7.0", + "borsh": "1.0.0", "buffer": "^6.0.3", "graphemesplit": "^2.4.4", "ipaddr.js": "^2.1.0", @@ -586,11 +586,6 @@ "@solana/web3.js": "^1.87.3" } }, - "node_modules/@bonfida/sns-records/node_modules/borsh": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/borsh/-/borsh-1.0.0.tgz", - "integrity": "sha512-fSVWzzemnyfF89EPwlUNsrS5swF5CrtiN4e+h0/lLf4dz2he4L3ndM20PS9wj7ICSkXJe/TQUHdaPTq15b1mNQ==" - }, "node_modules/@coral-xyz/anchor": { "version": "0.28.1-beta.2", "resolved": "https://registry.npmjs.org/@coral-xyz/anchor/-/anchor-0.28.1-beta.2.tgz", @@ -2616,6 +2611,26 @@ "tweetnacl": "^1.0.1" } }, + "node_modules/@near-js/crypto/node_modules/borsh": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/borsh/-/borsh-0.7.0.tgz", + "integrity": "sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==", + "dev": true, + "dependencies": { + "bn.js": "^5.2.0", + "bs58": "^4.0.0", + "text-encoding-utf-8": "^1.0.2" + } + }, + "node_modules/@near-js/crypto/node_modules/bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha512-Ok3Wdf5vOIlBrgCvTq96gBkJw+JUEzdBgyaza5HLtPm7yTHkjRy8+JzNyHF7BHa0bNWOQIp3m5YF0nnFcOIKLw==", + "dev": true, + "dependencies": { + "base-x": "^3.0.2" + } + }, "node_modules/@near-js/keystores": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/@near-js/keystores/-/keystores-0.0.3.tgz", @@ -2679,6 +2694,26 @@ "js-sha256": "^0.9.0" } }, + "node_modules/@near-js/providers/node_modules/borsh": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/borsh/-/borsh-0.7.0.tgz", + "integrity": "sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==", + "dev": true, + "dependencies": { + "bn.js": "^5.2.0", + "bs58": "^4.0.0", + "text-encoding-utf-8": "^1.0.2" + } + }, + "node_modules/@near-js/providers/node_modules/bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha512-Ok3Wdf5vOIlBrgCvTq96gBkJw+JUEzdBgyaza5HLtPm7yTHkjRy8+JzNyHF7BHa0bNWOQIp3m5YF0nnFcOIKLw==", + "dev": true, + "dependencies": { + "base-x": "^3.0.2" + } + }, "node_modules/@near-js/signers": { "version": "0.0.4", "resolved": "https://registry.npmjs.org/@near-js/signers/-/signers-0.0.4.tgz", @@ -2721,6 +2756,26 @@ "bn.js": "5.2.1" } }, + "node_modules/@near-js/signers/node_modules/borsh": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/borsh/-/borsh-0.7.0.tgz", + "integrity": "sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==", + "dev": true, + "dependencies": { + "bn.js": "^5.2.0", + "bs58": "^4.0.0", + "text-encoding-utf-8": "^1.0.2" + } + }, + "node_modules/@near-js/signers/node_modules/bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha512-Ok3Wdf5vOIlBrgCvTq96gBkJw+JUEzdBgyaza5HLtPm7yTHkjRy8+JzNyHF7BHa0bNWOQIp3m5YF0nnFcOIKLw==", + "dev": true, + "dependencies": { + "base-x": "^3.0.2" + } + }, "node_modules/@near-js/transactions": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/@near-js/transactions/-/transactions-0.1.1.tgz", @@ -2769,6 +2824,26 @@ "mustache": "^4.0.0" } }, + "node_modules/@near-js/transactions/node_modules/borsh": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/borsh/-/borsh-0.7.0.tgz", + "integrity": "sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==", + "dev": true, + "dependencies": { + "bn.js": "^5.2.0", + "bs58": "^4.0.0", + "text-encoding-utf-8": "^1.0.2" + } + }, + "node_modules/@near-js/transactions/node_modules/bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha512-Ok3Wdf5vOIlBrgCvTq96gBkJw+JUEzdBgyaza5HLtPm7yTHkjRy8+JzNyHF7BHa0bNWOQIp3m5YF0nnFcOIKLw==", + "dev": true, + "dependencies": { + "base-x": "^3.0.2" + } + }, "node_modules/@near-js/types": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/@near-js/types/-/types-0.0.3.tgz", @@ -3241,6 +3316,26 @@ "debug": "^4.3.4" } }, + "node_modules/@solana/spl-account-compression/node_modules/borsh": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/borsh/-/borsh-0.7.0.tgz", + "integrity": "sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==", + "dev": true, + "dependencies": { + "bn.js": "^5.2.0", + "bs58": "^4.0.0", + "text-encoding-utf-8": "^1.0.2" + } + }, + "node_modules/@solana/spl-account-compression/node_modules/borsh/node_modules/bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha512-Ok3Wdf5vOIlBrgCvTq96gBkJw+JUEzdBgyaza5HLtPm7yTHkjRy8+JzNyHF7BHa0bNWOQIp3m5YF0nnFcOIKLw==", + "dev": true, + "dependencies": { + "base-x": "^3.0.2" + } + }, "node_modules/@solana/spl-token": { "version": "0.3.9", "resolved": "https://registry.npmjs.org/@solana/spl-token/-/spl-token-0.3.9.tgz", @@ -3279,6 +3374,16 @@ "superstruct": "^0.14.2" } }, + "node_modules/@solana/web3.js/node_modules/borsh": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/borsh/-/borsh-0.7.0.tgz", + "integrity": "sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==", + "dependencies": { + "bn.js": "^5.2.0", + "bs58": "^4.0.0", + "text-encoding-utf-8": "^1.0.2" + } + }, "node_modules/@solana/web3.js/node_modules/bs58": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", @@ -4291,22 +4396,9 @@ "integrity": "sha512-eXRvHzWyYPBuB4NBy0cmYQjGitUrtqwbvlzP3G6VFnNRbsZQIxQ10PbKKHt8gZ/HW/D/747aDl+QkDqg3KQLMQ==" }, "node_modules/borsh": { - "version": "0.7.0", - "resolved": "https://registry.npmjs.org/borsh/-/borsh-0.7.0.tgz", - "integrity": "sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==", - "dependencies": { - "bn.js": "^5.2.0", - "bs58": "^4.0.0", - "text-encoding-utf-8": "^1.0.2" - } - }, - "node_modules/borsh/node_modules/bs58": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", - "integrity": "sha512-Ok3Wdf5vOIlBrgCvTq96gBkJw+JUEzdBgyaza5HLtPm7yTHkjRy8+JzNyHF7BHa0bNWOQIp3m5YF0nnFcOIKLw==", - "dependencies": { - "base-x": "^3.0.2" - } + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/borsh/-/borsh-1.0.0.tgz", + "integrity": "sha512-fSVWzzemnyfF89EPwlUNsrS5swF5CrtiN4e+h0/lLf4dz2he4L3ndM20PS9wj7ICSkXJe/TQUHdaPTq15b1mNQ==" }, "node_modules/brace-expansion": { "version": "1.1.11", @@ -12194,13 +12286,6 @@ "borsh": "1.0.0", "bs58": "5.0.0", "buffer": "^6.0.3" - }, - "dependencies": { - "borsh": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/borsh/-/borsh-1.0.0.tgz", - "integrity": "sha512-fSVWzzemnyfF89EPwlUNsrS5swF5CrtiN4e+h0/lLf4dz2he4L3ndM20PS9wj7ICSkXJe/TQUHdaPTq15b1mNQ==" - } } }, "@coral-xyz/anchor": { @@ -13687,6 +13772,28 @@ "bn.js": "5.2.1", "borsh": "^0.7.0", "tweetnacl": "^1.0.1" + }, + "dependencies": { + "borsh": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/borsh/-/borsh-0.7.0.tgz", + "integrity": "sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==", + "dev": true, + "requires": { + "bn.js": "^5.2.0", + "bs58": "^4.0.0", + "text-encoding-utf-8": "^1.0.2" + } + }, + "bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha512-Ok3Wdf5vOIlBrgCvTq96gBkJw+JUEzdBgyaza5HLtPm7yTHkjRy8+JzNyHF7BHa0bNWOQIp3m5YF0nnFcOIKLw==", + "dev": true, + "requires": { + "base-x": "^3.0.2" + } + } } }, "@near-js/keystores": { @@ -13749,6 +13856,26 @@ "borsh": "^0.7.0", "js-sha256": "^0.9.0" } + }, + "borsh": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/borsh/-/borsh-0.7.0.tgz", + "integrity": "sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==", + "dev": true, + "requires": { + "bn.js": "^5.2.0", + "bs58": "^4.0.0", + "text-encoding-utf-8": "^1.0.2" + } + }, + "bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha512-Ok3Wdf5vOIlBrgCvTq96gBkJw+JUEzdBgyaza5HLtPm7yTHkjRy8+JzNyHF7BHa0bNWOQIp3m5YF0nnFcOIKLw==", + "dev": true, + "requires": { + "base-x": "^3.0.2" + } } } }, @@ -13793,6 +13920,26 @@ "requires": { "bn.js": "5.2.1" } + }, + "borsh": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/borsh/-/borsh-0.7.0.tgz", + "integrity": "sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==", + "dev": true, + "requires": { + "bn.js": "^5.2.0", + "bs58": "^4.0.0", + "text-encoding-utf-8": "^1.0.2" + } + }, + "bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha512-Ok3Wdf5vOIlBrgCvTq96gBkJw+JUEzdBgyaza5HLtPm7yTHkjRy8+JzNyHF7BHa0bNWOQIp3m5YF0nnFcOIKLw==", + "dev": true, + "requires": { + "base-x": "^3.0.2" + } } } }, @@ -13843,6 +13990,26 @@ "depd": "^2.0.0", "mustache": "^4.0.0" } + }, + "borsh": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/borsh/-/borsh-0.7.0.tgz", + "integrity": "sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==", + "dev": true, + "requires": { + "bn.js": "^5.2.0", + "bs58": "^4.0.0", + "text-encoding-utf-8": "^1.0.2" + } + }, + "bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha512-Ok3Wdf5vOIlBrgCvTq96gBkJw+JUEzdBgyaza5HLtPm7yTHkjRy8+JzNyHF7BHa0bNWOQIp3m5YF0nnFcOIKLw==", + "dev": true, + "requires": { + "base-x": "^3.0.2" + } } } }, @@ -14159,6 +14326,28 @@ "bs58": "^5.0.0", "debug": "^4.3.4" } + }, + "borsh": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/borsh/-/borsh-0.7.0.tgz", + "integrity": "sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==", + "dev": true, + "requires": { + "bn.js": "^5.2.0", + "bs58": "^4.0.0", + "text-encoding-utf-8": "^1.0.2" + }, + "dependencies": { + "bs58": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", + "integrity": "sha512-Ok3Wdf5vOIlBrgCvTq96gBkJw+JUEzdBgyaza5HLtPm7yTHkjRy8+JzNyHF7BHa0bNWOQIp3m5YF0nnFcOIKLw==", + "dev": true, + "requires": { + "base-x": "^3.0.2" + } + } + } } } }, @@ -14194,6 +14383,16 @@ "superstruct": "^0.14.2" }, "dependencies": { + "borsh": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/borsh/-/borsh-0.7.0.tgz", + "integrity": "sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==", + "requires": { + "bn.js": "^5.2.0", + "bs58": "^4.0.0", + "text-encoding-utf-8": "^1.0.2" + } + }, "bs58": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", @@ -15034,24 +15233,9 @@ "integrity": "sha512-eXRvHzWyYPBuB4NBy0cmYQjGitUrtqwbvlzP3G6VFnNRbsZQIxQ10PbKKHt8gZ/HW/D/747aDl+QkDqg3KQLMQ==" }, "borsh": { - "version": "0.7.0", - "resolved": "https://registry.npmjs.org/borsh/-/borsh-0.7.0.tgz", - "integrity": "sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==", - "requires": { - "bn.js": "^5.2.0", - "bs58": "^4.0.0", - "text-encoding-utf-8": "^1.0.2" - }, - "dependencies": { - "bs58": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/bs58/-/bs58-4.0.1.tgz", - "integrity": "sha512-Ok3Wdf5vOIlBrgCvTq96gBkJw+JUEzdBgyaza5HLtPm7yTHkjRy8+JzNyHF7BHa0bNWOQIp3m5YF0nnFcOIKLw==", - "requires": { - "base-x": "^3.0.2" - } - } - } + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/borsh/-/borsh-1.0.0.tgz", + "integrity": "sha512-fSVWzzemnyfF89EPwlUNsrS5swF5CrtiN4e+h0/lLf4dz2he4L3ndM20PS9wj7ICSkXJe/TQUHdaPTq15b1mNQ==" }, "brace-expansion": { "version": "1.1.11", diff --git a/js/package.json b/js/package.json index 18cff1a5..0ea38c97 100644 --- a/js/package.json +++ b/js/package.json @@ -69,7 +69,7 @@ "@scure/base": "^1.1.5", "@solana/buffer-layout": "^4.0.1", "@solana/spl-token": "0.3.9", - "borsh": "^0.7.0", + "borsh": "1.0.0", "buffer": "^6.0.3", "graphemesplit": "^2.4.4", "ipaddr.js": "^2.1.0", diff --git a/js/src/favorite-domain.ts b/js/src/favorite-domain.ts index e0621851..4debe601 100644 --- a/js/src/favorite-domain.ts +++ b/js/src/favorite-domain.ts @@ -21,19 +21,12 @@ export const NAME_OFFERS_ID = new PublicKey( export class FavouriteDomain { tag: number; nameAccount: PublicKey; - - static schema: Schema = new Map([ - [ - FavouriteDomain, - { - kind: "struct", - fields: [ - ["tag", "u8"], - ["nameAccount", [32]], - ], - }, - ], - ]); + static schema = { + struct: { + tag: "u8", + nameAccount: { array: { type: "u8", len: 32 } }, + }, + }; constructor(obj: { tag: number; nameAccount: Uint8Array }) { this.tag = obj.tag; @@ -46,7 +39,7 @@ export class FavouriteDomain { * @returns */ static deserialize(data: Buffer) { - return deserialize(this.schema, FavouriteDomain, data); + return new FavouriteDomain(deserialize(this.schema, data) as any); } /** diff --git a/js/src/instructions.ts b/js/src/instructions.ts index 8490b88f..a040c6dd 100644 --- a/js/src/instructions.ts +++ b/js/src/instructions.ts @@ -6,7 +6,7 @@ import { } from "@solana/web3.js"; import { TOKEN_PROGRAM_ID } from "@solana/spl-token"; import { Numberu32, Numberu64 } from "./int"; -import { Schema, serialize } from "borsh"; +import { serialize } from "borsh"; export interface AccountKey { pubkey: PublicKey; @@ -231,19 +231,13 @@ export class createV2Instruction { name: string; space: number; - static schema: Schema = new Map([ - [ - createV2Instruction, - { - kind: "struct", - fields: [ - ["tag", "u8"], - ["name", "string"], - ["space", "u32"], - ], - }, - ], - ]); + static schema = { + struct: { + tag: "u8", + name: "string", + space: "u32", + }, + }; constructor(obj: { name: string; space: number }) { this.tag = 9; @@ -343,18 +337,13 @@ export class createV2Instruction { export class createReverseInstruction { tag: number; name: string; - static schema: Schema = new Map([ - [ - createReverseInstruction, - { - kind: "struct", - fields: [ - ["tag", "u8"], - ["name", "string"], - ], - }, - ], - ]); + static schema = { + struct: { + tag: "u8", + name: "string", + }, + }; + constructor(obj: { name: string }) { this.tag = 12; this.name = obj.name; @@ -437,20 +426,15 @@ export class createInstructionV3 { name: string; space: number; referrerIdxOpt: number | null; - static schema: Schema = new Map([ - [ - createInstructionV3, - { - kind: "struct", - fields: [ - ["tag", "u8"], - ["name", "string"], - ["space", "u32"], - ["referrerIdxOpt", { kind: "option", type: "u16" }], - ], - }, - ], - ]); + static schema = { + struct: { + tag: "u8", + name: "string", + space: "u32", + referrerIdxOpt: { option: "u16" }, + }, + }; + constructor(obj: { name: string; space: number; @@ -579,19 +563,14 @@ export class createWithNftInstruction { tag: number; name: string; space: number; - static schema: Schema = new Map([ - [ - createWithNftInstruction, - { - kind: "struct", - fields: [ - ["tag", "u8"], - ["name", "string"], - ["space", "u32"], - ], - }, - ], - ]); + static schema = { + struct: { + tag: "u8", + name: "string", + space: "u32", + }, + }; + constructor(obj: { name: string; space: number }) { this.tag = 17; this.name = obj.name; @@ -711,15 +690,12 @@ export class createWithNftInstruction { export class burnInstruction { tag: number; - static schema: Schema = new Map([ - [ - burnInstruction, - { - kind: "struct", - fields: [["tag", "u8"]], - }, - ], - ]); + static schema = { + struct: { + tag: "u8", + }, + }; + constructor() { this.tag = 16; } diff --git a/js/src/nft/name-tokenizer.ts b/js/src/nft/name-tokenizer.ts index 2a588e30..2074853a 100644 --- a/js/src/nft/name-tokenizer.ts +++ b/js/src/nft/name-tokenizer.ts @@ -30,21 +30,15 @@ export class NftRecord { owner: PublicKey; nftMint: PublicKey; - static schema: Schema = new Map([ - [ - NftRecord, - { - kind: "struct", - fields: [ - ["tag", "u8"], - ["nonce", "u8"], - ["nameAccount", [32]], - ["owner", [32]], - ["nftMint", [32]], - ], - }, - ], - ]); + static schema = { + struct: { + tag: "u8", + nonce: "u8", + nameAccount: { array: { type: "u8", len: 32 } }, + owner: { array: { type: "u8", len: 32 } }, + nftMint: { array: { type: "u8", len: 32 } }, + }, + }; constructor(obj: { tag: number; @@ -61,7 +55,7 @@ export class NftRecord { } static deserialize(data: Buffer): NftRecord { - return deserialize(this.schema, NftRecord, data); + return new NftRecord(deserialize(this.schema, data) as any); } static async retrieve(connection: Connection, key: PublicKey) { diff --git a/js/src/record.ts b/js/src/record.ts index 445a660b..3a458757 100644 --- a/js/src/record.ts +++ b/js/src/record.ts @@ -3,7 +3,7 @@ import { Connection, PublicKey } from "@solana/web3.js"; import { getDomainKeySync } from "./utils"; import { NameRegistryState } from "./state"; import { Buffer } from "buffer"; -import base58 from "bs58"; +import { encode as bs58Encode } from "bs58"; import { isValid as isValidIp, fromByteArray as ipFromByteArray, @@ -413,7 +413,7 @@ export const deserializeRecord = ( registry.owner, ); if (valid) { - return base58.encode(buffer.slice(0, 32)); + return bs58Encode(buffer.slice(0, 32)); } } diff --git a/js/src/state.ts b/js/src/state.ts index 7648aaa6..502b5756 100644 --- a/js/src/state.ts +++ b/js/src/state.ts @@ -1,8 +1,8 @@ import { Connection, PublicKey } from "@solana/web3.js"; -import { deserializeUnchecked, Schema } from "borsh"; import { retrieveNftOwner } from "./nft"; import { Buffer } from "buffer"; import { ErrorType, SNSError } from "./error"; +import { deserialize } from "borsh"; export class NameRegistryState { static HEADER_LEN = 96; @@ -11,19 +11,14 @@ export class NameRegistryState { class: PublicKey; data: Buffer | undefined; - static schema: Schema = new Map([ - [ - NameRegistryState, - { - kind: "struct", - fields: [ - ["parentName", [32]], - ["owner", [32]], - ["class", [32]], - ], - }, - ], - ]); + static schema = { + struct: { + parentName: { array: { type: "u8", len: 32 } }, + owner: { array: { type: "u8", len: 32 } }, + class: { array: { type: "u8", len: 32 } }, + }, + }; + constructor(obj: { parentName: Uint8Array; owner: Uint8Array; @@ -35,11 +30,7 @@ export class NameRegistryState { } static deserialize(data: Buffer) { - let res: NameRegistryState = deserializeUnchecked( - this.schema, - NameRegistryState, - data, - ); + const res = new NameRegistryState(deserialize(this.schema, data) as any); res.data = data?.slice(this.HEADER_LEN); return res; @@ -54,12 +45,9 @@ export class NameRegistryState { throw new SNSError(ErrorType.AccountDoesNotExist); } - let res: NameRegistryState = deserializeUnchecked( - this.schema, - NameRegistryState, - nameAccount.data, + const res = new NameRegistryState( + deserialize(this.schema, nameAccount.data) as any, ); - res.data = nameAccount.data?.slice(this.HEADER_LEN); const nftOwner = await retrieveNftOwner(connection, nameAccountKey); @@ -75,11 +63,7 @@ export class NameRegistryState { await connection.getMultipleAccountsInfo(nameAccountKeys); const fn = (data: Buffer | undefined) => { if (!data) return undefined; - const res: NameRegistryState = deserializeUnchecked( - this.schema, - NameRegistryState, - data, - ); + const res = new NameRegistryState(deserialize(this.schema, data) as any); res.data = data?.slice(this.HEADER_LEN); return res; }; diff --git a/js/src/twitter_bindings.ts b/js/src/twitter_bindings.ts index 21a5253d..b18505af 100644 --- a/js/src/twitter_bindings.ts +++ b/js/src/twitter_bindings.ts @@ -19,7 +19,7 @@ import { import { NameRegistryState } from "./state"; import { getHashedNameSync, getNameAccountKeySync } from "./utils"; import { Numberu32, Numberu64 } from "./int"; -import { deserializeUnchecked, Schema, serialize } from "borsh"; +import { deserialize, serialize } from "borsh"; import { Buffer } from "buffer"; import { ErrorType, SNSError } from "./error"; @@ -279,11 +279,9 @@ export async function getTwitterHandleandRegistryKeyViaFilters( for (const f of filteredAccounts) { if (f.account.data.length > NameRegistryState.HEADER_LEN + 32) { - let data = f.account.data.slice(NameRegistryState.HEADER_LEN); - let state: ReverseTwitterRegistryState = deserializeUnchecked( - ReverseTwitterRegistryState.schema, - ReverseTwitterRegistryState, - data, + const data = f.account.data.slice(NameRegistryState.HEADER_LEN); + const state = new ReverseTwitterRegistryState( + deserialize(ReverseTwitterRegistryState.schema, data) as any, ); return [state.twitterHandle, new PublicKey(state.twitterRegistryKey)]; } @@ -337,18 +335,13 @@ export class ReverseTwitterRegistryState { twitterRegistryKey: Uint8Array; twitterHandle: string; - static schema: Schema = new Map([ - [ - ReverseTwitterRegistryState, - { - kind: "struct", - fields: [ - ["twitterRegistryKey", [32]], - ["twitterHandle", "string"], - ], - }, - ], - ]); + static schema = { + struct: { + twitterRegistryKey: { array: { type: "u8", len: 32 } }, + twitterHandle: "string", + }, + }; + constructor(obj: { twitterRegistryKey: Uint8Array; twitterHandle: string }) { this.twitterRegistryKey = obj.twitterRegistryKey; this.twitterHandle = obj.twitterHandle; @@ -366,10 +359,11 @@ export class ReverseTwitterRegistryState { throw new SNSError(ErrorType.InvalidReverseTwitter); } - let res: ReverseTwitterRegistryState = deserializeUnchecked( - this.schema, - ReverseTwitterRegistryState, - reverseTwitterAccount.data.slice(NameRegistryState.HEADER_LEN), + const res = new ReverseTwitterRegistryState( + deserialize( + ReverseTwitterRegistryState.schema, + reverseTwitterAccount.data.slice(NameRegistryState.HEADER_LEN), + ) as any, ); return res;