Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions web3.js/src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ const GetTokenAccountsByOwner = jsonRpcResultAndContext(
executable: 'boolean',
owner: 'string',
lamports: 'number',
data: 'string',
data: ['string', struct.literal('base64')],
rentEpoch: 'number?',
}),
}),
Expand Down Expand Up @@ -795,7 +795,7 @@ const ParsedAccountInfoResult = struct.object({
owner: 'string',
lamports: 'number',
data: struct.union([
'string',
['string', struct.literal('base64')],
struct.pick({
program: 'string',
parsed: 'any',
Expand Down Expand Up @@ -1613,7 +1613,7 @@ export class Connection {
_args.push({programId: filter.programId.toBase58()});
}

const args = this._buildArgs(_args, commitment, 'binary64');
const args = this._buildArgs(_args, commitment, 'base64');
const unsafeRes = await this._rpcRequest('getTokenAccountsByOwner', args);
const res = GetTokenAccountsByOwner(unsafeRes);
if (res.error) {
Expand All @@ -1631,15 +1631,18 @@ export class Connection {

return {
context,
value: value.map(result => ({
pubkey: new PublicKey(result.pubkey),
account: {
executable: result.account.executable,
owner: new PublicKey(result.account.owner),
lamports: result.account.lamports,
data: Buffer.from(result.account.data, 'base64'),
},
})),
value: value.map(result => {
assert(result.account.data[1] === 'base64');
return {
pubkey: new PublicKey(result.pubkey),
account: {
executable: result.account.executable,
owner: new PublicKey(result.account.owner),
lamports: result.account.lamports,
data: Buffer.from(result.account.data[0], 'base64'),
},
};
}),
};
}

Expand Down Expand Up @@ -1752,7 +1755,7 @@ export class Connection {
const args = this._buildArgs(
[publicKey.toBase58()],
commitment,
'binary64',
'base64',
);
const unsafeRes = await this._rpcRequest('getAccountInfo', args);
const res = GetAccountInfoAndContextRpcResult(unsafeRes);
Expand All @@ -1769,11 +1772,12 @@ export class Connection {
let value = null;
if (res.result.value) {
const {executable, owner, lamports, data} = res.result.value;
assert(data[1] === 'base64');
value = {
executable,
owner: new PublicKey(owner),
lamports,
data: Buffer.from(data, 'base64'),
data: Buffer.from(data[0], 'base64'),
};
}

Expand Down Expand Up @@ -1817,7 +1821,8 @@ export class Connection {

let data = resultData;
if (!data.program) {
data = Buffer.from(data, 'base64');
assert(data[1] === 'base64');
data = Buffer.from(data[0], 'base64');
}

value = {
Expand Down Expand Up @@ -1864,7 +1869,7 @@ export class Connection {
const args = this._buildArgs(
[programId.toBase58()],
commitment,
'binary64',
'base64',
);
const unsafeRes = await this._rpcRequest('getProgramAccounts', args);
const res = GetProgramAccountsRpcResult(unsafeRes);
Expand All @@ -1881,13 +1886,14 @@ export class Connection {
assert(typeof result !== 'undefined');

return result.map(result => {
assert(result.account.data[1] === 'base64');
return {
pubkey: new PublicKey(result.pubkey),
account: {
executable: result.account.executable,
owner: new PublicKey(result.account.owner),
lamports: result.account.lamports,
data: Buffer.from(result.account.data, 'base64'),
data: Buffer.from(result.account.data[0], 'base64'),
},
};
});
Expand Down Expand Up @@ -1931,7 +1937,8 @@ export class Connection {

let data = resultData;
if (!data.program) {
data = Buffer.from(data, 'base64');
assert(data[1] === 'base64');
data = Buffer.from(data[0], 'base64');
}

return {
Expand Down Expand Up @@ -2776,7 +2783,7 @@ export class Connection {
this._subscribe(
sub,
'accountSubscribe',
this._buildArgs([sub.publicKey], sub.commitment, 'binary64'),
this._buildArgs([sub.publicKey], sub.commitment, 'base64'),
);
}

Expand All @@ -2785,7 +2792,7 @@ export class Connection {
this._subscribe(
sub,
'programSubscribe',
this._buildArgs([sub.programId], sub.commitment, 'binary64'),
this._buildArgs([sub.programId], sub.commitment, 'base64'),
);
}

Expand Down Expand Up @@ -2899,14 +2906,15 @@ export class Connection {
const {result} = res;
const {value, context} = result;

assert(value.account.data[1] === 'base64');
sub.callback(
{
accountId: value.pubkey,
accountInfo: {
executable: value.account.executable,
owner: new PublicKey(value.account.owner),
lamports: value.account.lamports,
data: Buffer.from(value.account.data, 'base64'),
data: Buffer.from(value.account.data[0], 'base64'),
},
},
context,
Expand Down Expand Up @@ -3016,7 +3024,7 @@ export class Connection {
_buildArgs(
args: Array<any>,
override: ?Commitment,
encoding?: 'jsonParsed' | 'binary64',
encoding?: 'jsonParsed' | 'base64',
): Array<any> {
const commitment = override || this._commitment;
if (commitment || encoding) {
Expand Down
14 changes: 7 additions & 7 deletions web3.js/test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ test('get account info - not found', async () => {
url,
{
method: 'getAccountInfo',
params: [account.publicKey.toBase58(), {encoding: 'binary64'}],
params: [account.publicKey.toBase58(), {encoding: 'base64'}],
},
{
error: null,
Expand Down Expand Up @@ -255,15 +255,15 @@ test('get program accounts', async () => {
method: 'getProgramAccounts',
params: [
programId.publicKey.toBase58(),
{commitment: 'recent', encoding: 'binary64'},
{commitment: 'recent', encoding: 'base64'},
],
},
{
error: null,
result: [
{
account: {
data: '',
data: ['', 'base64'],
executable: false,
lamports: LAMPORTS_PER_SOL - feeCalculator.lamportsPerSignature,
owner: programId.publicKey.toBase58(),
Expand All @@ -273,7 +273,7 @@ test('get program accounts', async () => {
},
{
account: {
data: '',
data: ['', 'base64'],
executable: false,
lamports:
0.5 * LAMPORTS_PER_SOL - feeCalculator.lamportsPerSignature,
Expand Down Expand Up @@ -1681,7 +1681,7 @@ test('request airdrop', async () => {
method: 'getAccountInfo',
params: [
account.publicKey.toBase58(),
{commitment: 'recent', encoding: 'binary64'},
{commitment: 'recent', encoding: 'base64'},
],
},
{
Expand All @@ -1693,7 +1693,7 @@ test('request airdrop', async () => {
value: {
owner: '11111111111111111111111111111111',
lamports: minimumAmount + 42,
data: '',
data: ['', 'base64'],
executable: false,
},
},
Expand Down Expand Up @@ -1727,7 +1727,7 @@ test('request airdrop', async () => {
value: {
owner: '11111111111111111111111111111111',
lamports: minimumAmount + 42,
data: '',
data: ['', 'base64'],
executable: false,
},
},
Expand Down
14 changes: 10 additions & 4 deletions web3.js/test/nonce.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ if (!mockRpcEnabled) {
jest.setTimeout(30000);
}

const expectedData = (authorizedPubkey: PublicKey): string => {
const expectedData = (authorizedPubkey: PublicKey): [string, string] => {
const expectedData = Buffer.alloc(NONCE_ACCOUNT_LENGTH);
expectedData.writeInt32LE(0, 0); // Version, 4 bytes
expectedData.writeInt32LE(1, 4); // State, 4 bytes
authorizedPubkey.toBuffer().copy(expectedData, 8); // authorizedPubkey, 32 bytes
const mockNonce = new Account();
mockNonce.publicKey.toBuffer().copy(expectedData, 40); // Hash, 32 bytes
expectedData.writeUInt16LE(5000, 72); // feeCalculator, 8 bytes
return expectedData.toString('base64');
return [expectedData.toString('base64'), 'base64'];
};

test('create and query nonce account', async () => {
Expand Down Expand Up @@ -119,7 +119,10 @@ test('create and query nonce account', async () => {
url,
{
method: 'getAccountInfo',
params: [nonceAccount.publicKey.toBase58(), {commitment: 'recent'}],
params: [
nonceAccount.publicKey.toBase58(),
{encoding: 'base64', commitment: 'recent'},
],
},
{
error: null,
Expand Down Expand Up @@ -243,7 +246,10 @@ test('create and query nonce account with seed', async () => {
url,
{
method: 'getAccountInfo',
params: [noncePubkey.toBase58(), {commitment: 'recent'}],
params: [
noncePubkey.toBase58(),
{encoding: 'base64', commitment: 'recent'},
],
},
{
error: null,
Expand Down