Skip to content

Commit 22bc06c

Browse files
committed
chore(ts): Update some uses of Buffer to TS 5.9 compatibility
1 parent 862858d commit 22bc06c

File tree

20 files changed

+79
-23
lines changed

20 files changed

+79
-23
lines changed

ts/packages/anchor/src/coder/borsh/accounts.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import { Idl, IdlDiscriminator } from "../../idl.js";
55
import { IdlCoder } from "./idl.js";
66
import { AccountsCoder } from "../index.js";
77

8+
function bytesEqual(a: Uint8Array, b: Uint8Array): boolean {
9+
if (a.length !== b.length) return false;
10+
for (let i = 0; i < a.length; i += 1) {
11+
if (a[i] !== b[i]) return false;
12+
}
13+
return true;
14+
}
15+
816
/**
917
* Encodes and decodes account objects.
1018
*/
@@ -56,22 +64,28 @@ export class BorshAccountsCoder<A extends string = string>
5664
const len = layout.layout.encode(account, buffer);
5765
const accountData = buffer.slice(0, len);
5866
const discriminator = this.accountDiscriminator(accountName);
59-
return Buffer.concat([discriminator, accountData]);
67+
return Buffer.from([...discriminator, ...accountData]);
6068
}
6169

6270
public decode<T = any>(accountName: A, data: Buffer): T {
6371
// Assert the account discriminator is correct.
6472
const discriminator = this.accountDiscriminator(accountName);
65-
if (discriminator.compare(data.slice(0, discriminator.length))) {
73+
const givenDisc = Uint8Array.from(data.subarray(0, discriminator.length));
74+
if (!bytesEqual(Uint8Array.from(discriminator), givenDisc)) {
6675
throw new Error("Invalid account discriminator");
6776
}
6877
return this.decodeUnchecked(accountName, data);
6978
}
7079

7180
public decodeAny<T = any>(data: Buffer): T {
7281
for (const [name, layout] of this.accountLayouts) {
73-
const givenDisc = data.subarray(0, layout.discriminator.length);
74-
const matches = givenDisc.equals(Buffer.from(layout.discriminator));
82+
const givenDisc = Uint8Array.from(
83+
data.subarray(0, layout.discriminator.length)
84+
);
85+
const matches = bytesEqual(
86+
Uint8Array.from(layout.discriminator),
87+
givenDisc
88+
);
7589
if (matches) return this.decodeUnchecked(name, data);
7690
}
7791

@@ -91,11 +105,12 @@ export class BorshAccountsCoder<A extends string = string>
91105

92106
public memcmp(accountName: A, appendData?: Buffer): any {
93107
const discriminator = this.accountDiscriminator(accountName);
108+
const bytes = appendData
109+
? Uint8Array.from([...discriminator, ...appendData])
110+
: Uint8Array.from(discriminator);
94111
return {
95112
offset: 0,
96-
bytes: bs58.encode(
97-
appendData ? Buffer.concat([discriminator, appendData]) : discriminator
98-
),
113+
bytes: bs58.encode(Buffer.from(bytes)),
99114
};
100115
}
101116

ts/packages/anchor/src/coder/borsh/event.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import { Idl, IdlDiscriminator } from "../../idl.js";
55
import { IdlCoder } from "./idl.js";
66
import { EventCoder } from "../index.js";
77

8+
function bytesEqual(a: Uint8Array, b: Uint8Array): boolean {
9+
if (a.length !== b.length) return false;
10+
for (let i = 0; i < a.length; i += 1) {
11+
if (a[i] !== b[i]) return false;
12+
}
13+
return true;
14+
}
15+
816
export class BorshEventCoder implements EventCoder {
917
/**
1018
* Maps account type identifier to a layout.
@@ -54,8 +62,13 @@ export class BorshEventCoder implements EventCoder {
5462
}
5563

5664
for (const [name, layout] of this.layouts) {
57-
const givenDisc = logArr.subarray(0, layout.discriminator.length);
58-
const matches = givenDisc.equals(Buffer.from(layout.discriminator));
65+
const givenDisc = Uint8Array.from(
66+
logArr.subarray(0, layout.discriminator.length)
67+
);
68+
const matches = bytesEqual(
69+
Uint8Array.from(layout.discriminator),
70+
givenDisc
71+
);
5972
if (matches) {
6073
return {
6174
name,

ts/packages/anchor/src/coder/borsh/instruction.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ import {
1818
import { IdlCoder } from "./idl.js";
1919
import { InstructionCoder } from "../index.js";
2020

21+
function bytesEqual(a: Uint8Array, b: Uint8Array): boolean {
22+
if (a.length !== b.length) return false;
23+
for (let i = 0; i < a.length; i += 1) {
24+
if (a[i] !== b[i]) return false;
25+
}
26+
return true;
27+
}
28+
2129
/**
2230
* Encodes and decodes program instructions.
2331
*/
@@ -53,7 +61,7 @@ export class BorshInstructionCoder implements InstructionCoder {
5361
const len = encoder.layout.encode(ix, buffer);
5462
const data = buffer.slice(0, len);
5563

56-
return Buffer.concat([Buffer.from(encoder.discriminator), data]);
64+
return Buffer.from([...Buffer.from(encoder.discriminator), ...data]);
5765
}
5866

5967
/**
@@ -64,12 +72,18 @@ export class BorshInstructionCoder implements InstructionCoder {
6472
encoding: "hex" | "base58" = "hex"
6573
): Instruction | null {
6674
if (typeof ix === "string") {
67-
ix = encoding === "hex" ? Buffer.from(ix, "hex") : bs58.decode(ix);
75+
ix =
76+
encoding === "hex"
77+
? Buffer.from(ix, "hex")
78+
: Buffer.from(bs58.decode(ix) as Uint8Array);
6879
}
6980

7081
for (const [name, layout] of this.ixLayouts) {
71-
const givenDisc = ix.subarray(0, layout.discriminator.length);
72-
const matches = givenDisc.equals(Buffer.from(layout.discriminator));
82+
const givenDisc = Uint8Array.from(ix.subarray(0, layout.discriminator.length));
83+
const matches = bytesEqual(
84+
Uint8Array.from(layout.discriminator),
85+
givenDisc
86+
);
7387
if (matches) {
7488
return {
7589
name,

ts/packages/anchor/src/nodewallet.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Buffer } from "buffer";
21
import {
32
Keypair,
43
PublicKey,
@@ -24,7 +23,7 @@ export default class NodeWallet implements Wallet {
2423
}
2524

2625
const payer = Keypair.fromSecretKey(
27-
Buffer.from(
26+
Uint8Array.from(
2827
JSON.parse(
2928
require("fs").readFileSync(process.env.ANCHOR_WALLET, {
3029
encoding: "utf-8",

ts/packages/anchor/src/program/token-account-layout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class u64 extends BN {
103103
}
104104

105105
const zeroPad = Buffer.alloc(8);
106-
b.copy(zeroPad);
106+
zeroPad.set(Uint8Array.from(b));
107107
return zeroPad;
108108
}
109109

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Buffer } from "buffer";
21
import { PublicKey } from "@solana/web3.js";
32
import { sha256 } from "@noble/hashes/sha256";
43

@@ -8,10 +7,12 @@ export function createWithSeedSync(
87
seed: string,
98
programId: PublicKey
109
): PublicKey {
11-
const buffer = Buffer.concat([
12-
fromPublicKey.toBuffer(),
13-
Buffer.from(seed),
14-
programId.toBuffer(),
15-
]);
16-
return new PublicKey(sha256(buffer));
10+
const fromKey = fromPublicKey.toBytes();
11+
const seedBytes = new TextEncoder().encode(seed);
12+
const program = programId.toBytes();
13+
const data = new Uint8Array(fromKey.length + seedBytes.length + program.length);
14+
data.set(fromKey, 0);
15+
data.set(seedBytes, fromKey.length);
16+
data.set(program, fromKey.length + seedBytes.length);
17+
return new PublicKey(sha256(data));
1718
}

ts/packages/anchor/tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"./src/**/*"
44
],
55
"compilerOptions": {
6+
"skipLibCheck": true,
67
"sourceMap": true,
78
"declaration": true,
89
"declarationMap": true,

ts/packages/spl-associated-token-account/tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
],
55
"compilerOptions": {
66
"sourceMap": true,
7+
"skipLibCheck": true,
78
"declaration": true,
89
"declarationMap": true,
910
"allowSyntheticDefaultImports": true,

ts/packages/spl-binary-option/tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
],
55
"compilerOptions": {
66
"sourceMap": true,
7+
"skipLibCheck": true,
78
"declaration": true,
89
"declarationMap": true,
910
"allowSyntheticDefaultImports": true,

ts/packages/spl-binary-oracle-pair/tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
],
55
"compilerOptions": {
66
"sourceMap": true,
7+
"skipLibCheck": true,
78
"declaration": true,
89
"declarationMap": true,
910
"allowSyntheticDefaultImports": true,

0 commit comments

Comments
 (0)