Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
refactor(experimental): encode to ReadonlyUint8Array in codecs
Browse files Browse the repository at this point in the history
  • Loading branch information
mcintyre94 committed Mar 28, 2024
1 parent a89252f commit 615f470
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 21 deletions.
11 changes: 6 additions & 5 deletions packages/accounts/src/__tests__/decode-account-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import '@solana/test-matchers/toBeFrozenObject';

import { Address } from '@solana/addresses';
import { ReadonlyUint8Array } from '@solana/codecs-core';
import {
SOLANA_ERROR__ACCOUNTS__EXPECTED_ALL_ACCOUNTS_TO_BE_DECODED,
SOLANA_ERROR__ACCOUNTS__EXPECTED_DECODED_ACCOUNT,
Expand All @@ -17,7 +18,7 @@ describe('decodeAccount', () => {
// Given an encoded account.
const encodedAccount = <EncodedAccount>{
address: '1111',
data: new Uint8Array([1, 2, 3]),
data: new Uint8Array([1, 2, 3]) as ReadonlyUint8Array,
executable: false,
lamports: 1_000_000_000n,
programAddress: '9999',
Expand Down Expand Up @@ -46,7 +47,7 @@ describe('decodeAccount', () => {
// Given an encoded account.
const encodedAccount = <EncodedAccount>{
address: '1111',
data: new Uint8Array([1, 2, 3]),
data: new Uint8Array([1, 2, 3]) as ReadonlyUint8Array,
executable: false,
lamports: 1_000_000_000n,
programAddress: '9999',
Expand Down Expand Up @@ -84,7 +85,7 @@ describe('assertDecodedAccount', () => {
// Given an account with Uint8Array data
const account = <EncodedAccount>{
address: '1111' as Address,
data: new Uint8Array([]),
data: new Uint8Array([]) as ReadonlyUint8Array,
};

// When we assert that the account is decoded
Expand Down Expand Up @@ -135,11 +136,11 @@ describe('assertDecodedAccounts', () => {
const accounts = [
<EncodedAccount>{
address: '1111' as Address,
data: new Uint8Array([]),
data: new Uint8Array([]) as ReadonlyUint8Array,
},
<EncodedAccount>{
address: '2222' as Address,
data: new Uint8Array([]),
data: new Uint8Array([]) as ReadonlyUint8Array,
},
<Account<MockData>>{
address: '3333' as Address,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address } from '@solana/addresses';
import { Decoder } from '@solana/codecs-core';
import { Decoder, ReadonlyUint8Array } from '@solana/codecs-core';

import { Account, EncodedAccount } from '../account';
import { assertAccountDecoded, assertAccountsDecoded, decodeAccount } from '../decode-account';
Expand Down Expand Up @@ -33,9 +33,9 @@ type MockDataDecoder = Decoder<MockData>;
{
// It narrows a list of accounts with data MockData | Uint8Array to MockData
const accounts = [
{} as unknown as Account<MockData | Uint8Array, '1111'>,
{} as unknown as Account<MockData | Uint8Array, '2222'>,
{} as unknown as Account<MockData | Uint8Array, '3333'>,
{} as unknown as Account<MockData | ReadonlyUint8Array, '1111'>,
{} as unknown as Account<MockData | ReadonlyUint8Array, '2222'>,
{} as unknown as Account<MockData | ReadonlyUint8Array, '3333'>,
];
assertAccountsDecoded(accounts);
accounts satisfies Account<MockData, Address>[];
Expand Down
3 changes: 2 additions & 1 deletion packages/accounts/src/account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Address } from '@solana/addresses';
import { ReadonlyUint8Array } from '@solana/codecs-core';
import type { LamportsUnsafeBeyond2Pow53Minus1 } from '@solana/rpc-types';

/** The amount of bytes required to store the base account information without its data. */
Expand All @@ -18,4 +19,4 @@ export type Account<TData extends Uint8Array | object, TAddress extends string =
};

/** Defines a Solana account with its generic details and encoded data. */
export type EncodedAccount<TAddress extends string = string> = Account<Uint8Array, TAddress>;
export type EncodedAccount<TAddress extends string = string> = Account<ReadonlyUint8Array, TAddress>;
8 changes: 4 additions & 4 deletions packages/accounts/src/decode-account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Decoder } from '@solana/codecs-core';
import type { Decoder, ReadonlyUint8Array } from '@solana/codecs-core';
import {
SOLANA_ERROR__ACCOUNTS__EXPECTED_ALL_ACCOUNTS_TO_BE_DECODED,
SOLANA_ERROR__ACCOUNTS__EXPECTED_DECODED_ACCOUNT,
Expand Down Expand Up @@ -57,13 +57,13 @@ export function assertAccountDecoded<TData extends object, TAddress extends stri

/** Asserts that all accounts have been decoded. */
export function assertAccountsDecoded<TData extends object, TAddress extends string = string>(
accounts: Account<TData | Uint8Array, TAddress>[],
accounts: Account<ReadonlyUint8Array | TData, TAddress>[],
): asserts accounts is Account<TData, TAddress>[];
export function assertAccountsDecoded<TData extends object, TAddress extends string = string>(
accounts: MaybeAccount<TData | Uint8Array, TAddress>[],
accounts: MaybeAccount<ReadonlyUint8Array | TData, TAddress>[],
): asserts accounts is MaybeAccount<TData, TAddress>[];
export function assertAccountsDecoded<TData extends object, TAddress extends string = string>(
accounts: (Account<TData | Uint8Array, TAddress> | MaybeAccount<TData | Uint8Array, TAddress>)[],
accounts: (Account<ReadonlyUint8Array | TData, TAddress> | MaybeAccount<ReadonlyUint8Array | TData, TAddress>)[],
): asserts accounts is (Account<TData, TAddress> | MaybeAccount<TData, TAddress>)[] {
const encoded = accounts.filter(a => accountExists(a) && a.data instanceof Uint8Array);
if (encoded.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/codecs-core/src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type Offset = number;

type BaseEncoder<TFrom> = {
/** Encode the provided value and return the encoded bytes directly. */
readonly encode: (value: TFrom) => Uint8Array;
readonly encode: (value: TFrom) => ReadonlyUint8Array;
/**
* Writes the encoded value into the provided byte array at the given offset.
* Returns the offset of the next byte after the encoded value.
Expand Down
2 changes: 1 addition & 1 deletion packages/library-legacy-sham/src/public-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class PublicKey<TAddress extends string = string> {
return this.#address;
}
#getByteArray() {
return getAddressEncoder().encode(this.#address);
return getAddressEncoder().encode(this.#address) as Uint8Array;
}
#getBuffer() {
if (!__NODEJS__ && typeof Buffer === 'undefined') {
Expand Down
2 changes: 1 addition & 1 deletion packages/library-legacy-sham/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class Transaction {
'us know: https://github.com/solana-labs/solana-web3.js/issues/new/choose',
);
}
const byteArray = getTransactionEncoder().encode(this.#tx as CompilableTransaction);
const byteArray = getTransactionEncoder().encode(this.#tx as CompilableTransaction) as Uint8Array;
return __NODEJS__ ? Buffer.from(byteArray) : (byteArray as Buffer);
}
setSigners = createUnimplementedFunction('Transaction#setSigners');
Expand Down
6 changes: 3 additions & 3 deletions packages/rpc-api/src/__tests__/simulate-transaction-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Buffer } from 'node:buffer';

Check failure on line 1 in packages/rpc-api/src/__tests__/simulate-transaction-test.ts

View workflow job for this annotation

GitHub Actions / Build & Test on Node current

Run autofix to sort these imports!

Check failure on line 1 in packages/rpc-api/src/__tests__/simulate-transaction-test.ts

View workflow job for this annotation

GitHub Actions / Build & Test on Node current

Run autofix to sort these imports!

Check failure on line 1 in packages/rpc-api/src/__tests__/simulate-transaction-test.ts

View workflow job for this annotation

GitHub Actions / Build & Test on Node lts/*

Run autofix to sort these imports!

Check failure on line 1 in packages/rpc-api/src/__tests__/simulate-transaction-test.ts

View workflow job for this annotation

GitHub Actions / Build & Test on Node lts/*

Run autofix to sort these imports!

import type { Address } from '@solana/addresses';
import { fixEncoder } from '@solana/codecs-core';
import { ReadonlyUint8Array, fixEncoder } from '@solana/codecs-core';
import { getBase58Decoder, getBase58Encoder } from '@solana/codecs-strings';
import {
SOLANA_ERROR__JSON_RPC__INVALID_PARAMS,
Expand Down Expand Up @@ -72,9 +72,9 @@ function getMockTransactionMessageWithAdditionalAccount({
memoString,
version = 0x80, // 0 + version mask
}: {
accountAddressBytes: Uint8Array;
accountAddressBytes: ReadonlyUint8Array;
blockhash: string;
feePayerAddressBytes: Uint8Array;
feePayerAddressBytes: ReadonlyUint8Array;
memoString: string;
version?: number;
}) {
Expand Down
5 changes: 4 additions & 1 deletion packages/transactions/src/signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export async function partiallySignTransaction<TTransaction extends CompilableTr
const wireMessageBytes = getCompiledMessageEncoder().encode(compiledMessage);
const publicKeySignaturePairs = await Promise.all(
keyPairs.map(keyPair =>
Promise.all([getAddressFromPublicKey(keyPair.publicKey), signBytes(keyPair.privateKey, wireMessageBytes)]),
Promise.all([
getAddressFromPublicKey(keyPair.publicKey),
signBytes(keyPair.privateKey, wireMessageBytes as Uint8Array),
]),
),
);
for (const [signerPublicKey, signature] of publicKeySignaturePairs) {
Expand Down

0 comments on commit 615f470

Please sign in to comment.