fix: use base64 encoding by default for account data

This commit is contained in:
Justin Starry
2020-08-10 16:26:48 +08:00
committed by Justin Starry
parent 88ca04dbdb
commit 97e53f867f
3 changed files with 48 additions and 40 deletions

View File

@ -1381,7 +1381,7 @@ export class Connection {
publicKey: PublicKey, publicKey: PublicKey,
commitment: ?Commitment, commitment: ?Commitment,
): Promise<RpcResponseAndContext<number>> { ): Promise<RpcResponseAndContext<number>> {
const args = this._argsWithCommitment([publicKey.toBase58()], commitment); const args = this._buildArgs([publicKey.toBase58()], commitment);
const unsafeRes = await this._rpcRequest('getBalance', args); const unsafeRes = await this._rpcRequest('getBalance', args);
const res = GetBalanceAndContextRpcResult(unsafeRes); const res = GetBalanceAndContextRpcResult(unsafeRes);
if (res.error) { if (res.error) {
@ -1464,7 +1464,7 @@ export class Connection {
async getSupply( async getSupply(
commitment: ?Commitment, commitment: ?Commitment,
): Promise<RpcResponseAndContext<Supply>> { ): Promise<RpcResponseAndContext<Supply>> {
const args = this._argsWithCommitment([], commitment); const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getSupply', args); const unsafeRes = await this._rpcRequest('getSupply', args);
const res = GetSupplyRpcResult(unsafeRes); const res = GetSupplyRpcResult(unsafeRes);
if (res.error) { if (res.error) {
@ -1484,10 +1484,7 @@ export class Connection {
tokenMintAddress: PublicKey, tokenMintAddress: PublicKey,
commitment: ?Commitment, commitment: ?Commitment,
): Promise<RpcResponseAndContext<TokenAmount>> { ): Promise<RpcResponseAndContext<TokenAmount>> {
const args = this._argsWithCommitment( const args = this._buildArgs([tokenMintAddress.toBase58()], commitment);
[tokenMintAddress.toBase58()],
commitment,
);
const unsafeRes = await this._rpcRequest('getTokenSupply', args); const unsafeRes = await this._rpcRequest('getTokenSupply', args);
const res = GetTokenSupplyRpcResult(unsafeRes); const res = GetTokenSupplyRpcResult(unsafeRes);
if (res.error) { if (res.error) {
@ -1504,10 +1501,7 @@ export class Connection {
tokenAddress: PublicKey, tokenAddress: PublicKey,
commitment: ?Commitment, commitment: ?Commitment,
): Promise<RpcResponseAndContext<TokenAmount>> { ): Promise<RpcResponseAndContext<TokenAmount>> {
const args = this._argsWithCommitment( const args = this._buildArgs([tokenAddress.toBase58()], commitment);
[tokenAddress.toBase58()],
commitment,
);
const unsafeRes = await this._rpcRequest('getTokenAccountBalance', args); const unsafeRes = await this._rpcRequest('getTokenAccountBalance', args);
const res = GetTokenAccountBalance(unsafeRes); const res = GetTokenAccountBalance(unsafeRes);
if (res.error) { if (res.error) {
@ -1540,7 +1534,7 @@ export class Connection {
_args.push({programId: filter.programId.toBase58()}); _args.push({programId: filter.programId.toBase58()});
} }
const args = this._argsWithCommitment(_args, commitment); const args = this._buildArgs(_args, commitment, 'binary64');
const unsafeRes = await this._rpcRequest('getTokenAccountsByOwner', args); const unsafeRes = await this._rpcRequest('getTokenAccountsByOwner', args);
const res = GetTokenAccountsByOwner(unsafeRes); const res = GetTokenAccountsByOwner(unsafeRes);
if (res.error) { if (res.error) {
@ -1564,7 +1558,7 @@ export class Connection {
executable: result.account.executable, executable: result.account.executable,
owner: new PublicKey(result.account.owner), owner: new PublicKey(result.account.owner),
lamports: result.account.lamports, lamports: result.account.lamports,
data: bs58.decode(result.account.data), data: Buffer.from(result.account.data, 'base64'),
}, },
})), })),
}; };
@ -1591,7 +1585,7 @@ export class Connection {
_args.push({programId: filter.programId.toBase58()}); _args.push({programId: filter.programId.toBase58()});
} }
const args = this._argsWithCommitment(_args, commitment, 'jsonParsed'); const args = this._buildArgs(_args, commitment, 'jsonParsed');
const unsafeRes = await this._rpcRequest('getTokenAccountsByOwner', args); const unsafeRes = await this._rpcRequest('getTokenAccountsByOwner', args);
const res = GetParsedTokenAccountsByOwner(unsafeRes); const res = GetParsedTokenAccountsByOwner(unsafeRes);
if (res.error) { if (res.error) {
@ -1652,7 +1646,11 @@ export class Connection {
publicKey: PublicKey, publicKey: PublicKey,
commitment: ?Commitment, commitment: ?Commitment,
): Promise<RpcResponseAndContext<AccountInfo<Buffer> | null>> { ): Promise<RpcResponseAndContext<AccountInfo<Buffer> | null>> {
const args = this._argsWithCommitment([publicKey.toBase58()], commitment); const args = this._buildArgs(
[publicKey.toBase58()],
commitment,
'binary64',
);
const unsafeRes = await this._rpcRequest('getAccountInfo', args); const unsafeRes = await this._rpcRequest('getAccountInfo', args);
const res = GetAccountInfoAndContextRpcResult(unsafeRes); const res = GetAccountInfoAndContextRpcResult(unsafeRes);
if (res.error) { if (res.error) {
@ -1672,7 +1670,7 @@ export class Connection {
executable, executable,
owner: new PublicKey(owner), owner: new PublicKey(owner),
lamports, lamports,
data: bs58.decode(data), data: Buffer.from(data, 'base64'),
}; };
} }
@ -1693,7 +1691,7 @@ export class Connection {
): Promise< ): Promise<
RpcResponseAndContext<AccountInfo<Buffer | ParsedAccountData> | null>, RpcResponseAndContext<AccountInfo<Buffer | ParsedAccountData> | null>,
> { > {
const args = this._argsWithCommitment( const args = this._buildArgs(
[publicKey.toBase58()], [publicKey.toBase58()],
commitment, commitment,
'jsonParsed', 'jsonParsed',
@ -1760,7 +1758,11 @@ export class Connection {
programId: PublicKey, programId: PublicKey,
commitment: ?Commitment, commitment: ?Commitment,
): Promise<Array<{pubkey: PublicKey, account: AccountInfo<Buffer>}>> { ): Promise<Array<{pubkey: PublicKey, account: AccountInfo<Buffer>}>> {
const args = this._argsWithCommitment([programId.toBase58()], commitment); const args = this._buildArgs(
[programId.toBase58()],
commitment,
'binary64',
);
const unsafeRes = await this._rpcRequest('getProgramAccounts', args); const unsafeRes = await this._rpcRequest('getProgramAccounts', args);
const res = GetProgramAccountsRpcResult(unsafeRes); const res = GetProgramAccountsRpcResult(unsafeRes);
if (res.error) { if (res.error) {
@ -1782,7 +1784,7 @@ export class Connection {
executable: result.account.executable, executable: result.account.executable,
owner: new PublicKey(result.account.owner), owner: new PublicKey(result.account.owner),
lamports: result.account.lamports, lamports: result.account.lamports,
data: bs58.decode(result.account.data), data: Buffer.from(result.account.data, 'base64'),
}, },
}; };
}); });
@ -1802,7 +1804,7 @@ export class Connection {
account: AccountInfo<Buffer | ParsedAccountData>, account: AccountInfo<Buffer | ParsedAccountData>,
}>, }>,
> { > {
const args = this._argsWithCommitment( const args = this._buildArgs(
[programId.toBase58()], [programId.toBase58()],
commitment, commitment,
'jsonParsed', 'jsonParsed',
@ -1894,7 +1896,7 @@ export class Connection {
* Return the list of nodes that are currently participating in the cluster * Return the list of nodes that are currently participating in the cluster
*/ */
async getVoteAccounts(commitment: ?Commitment): Promise<VoteAccountStatus> { async getVoteAccounts(commitment: ?Commitment): Promise<VoteAccountStatus> {
const args = this._argsWithCommitment([], commitment); const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getVoteAccounts', args); const unsafeRes = await this._rpcRequest('getVoteAccounts', args);
const res = GetVoteAccounts(unsafeRes); const res = GetVoteAccounts(unsafeRes);
//const res = unsafeRes; //const res = unsafeRes;
@ -1909,7 +1911,7 @@ export class Connection {
* Fetch the current slot that the node is processing * Fetch the current slot that the node is processing
*/ */
async getSlot(commitment: ?Commitment): Promise<number> { async getSlot(commitment: ?Commitment): Promise<number> {
const args = this._argsWithCommitment([], commitment); const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getSlot', args); const unsafeRes = await this._rpcRequest('getSlot', args);
const res = GetSlot(unsafeRes); const res = GetSlot(unsafeRes);
if (res.error) { if (res.error) {
@ -1923,7 +1925,7 @@ export class Connection {
* Fetch the current slot leader of the cluster * Fetch the current slot leader of the cluster
*/ */
async getSlotLeader(commitment: ?Commitment): Promise<string> { async getSlotLeader(commitment: ?Commitment): Promise<string> {
const args = this._argsWithCommitment([], commitment); const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getSlotLeader', args); const unsafeRes = await this._rpcRequest('getSlotLeader', args);
const res = GetSlotLeader(unsafeRes); const res = GetSlotLeader(unsafeRes);
if (res.error) { if (res.error) {
@ -1972,7 +1974,7 @@ export class Connection {
* Fetch the current transaction count of the cluster * Fetch the current transaction count of the cluster
*/ */
async getTransactionCount(commitment: ?Commitment): Promise<number> { async getTransactionCount(commitment: ?Commitment): Promise<number> {
const args = this._argsWithCommitment([], commitment); const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getTransactionCount', args); const unsafeRes = await this._rpcRequest('getTransactionCount', args);
const res = GetTransactionCountRpcResult(unsafeRes); const res = GetTransactionCountRpcResult(unsafeRes);
if (res.error) { if (res.error) {
@ -1986,7 +1988,7 @@ export class Connection {
* Fetch the current total currency supply of the cluster in lamports * Fetch the current total currency supply of the cluster in lamports
*/ */
async getTotalSupply(commitment: ?Commitment): Promise<number> { async getTotalSupply(commitment: ?Commitment): Promise<number> {
const args = this._argsWithCommitment([], commitment); const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getTotalSupply', args); const unsafeRes = await this._rpcRequest('getTotalSupply', args);
const res = GetTotalSupplyRpcResult(unsafeRes); const res = GetTotalSupplyRpcResult(unsafeRes);
if (res.error) { if (res.error) {
@ -2002,7 +2004,7 @@ export class Connection {
async getInflationGovernor( async getInflationGovernor(
commitment: ?Commitment, commitment: ?Commitment,
): Promise<InflationGovernor> { ): Promise<InflationGovernor> {
const args = this._argsWithCommitment([], commitment); const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getInflationGovernor', args); const unsafeRes = await this._rpcRequest('getInflationGovernor', args);
const res = GetInflationGovernorRpcResult(unsafeRes); const res = GetInflationGovernorRpcResult(unsafeRes);
if (res.error) { if (res.error) {
@ -2016,7 +2018,7 @@ export class Connection {
* Fetch the Epoch Info parameters * Fetch the Epoch Info parameters
*/ */
async getEpochInfo(commitment: ?Commitment): Promise<EpochInfo> { async getEpochInfo(commitment: ?Commitment): Promise<EpochInfo> {
const args = this._argsWithCommitment([], commitment); const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getEpochInfo', args); const unsafeRes = await this._rpcRequest('getEpochInfo', args);
const res = GetEpochInfoRpcResult(unsafeRes); const res = GetEpochInfoRpcResult(unsafeRes);
if (res.error) { if (res.error) {
@ -2061,7 +2063,7 @@ export class Connection {
dataLength: number, dataLength: number,
commitment: ?Commitment, commitment: ?Commitment,
): Promise<number> { ): Promise<number> {
const args = this._argsWithCommitment([dataLength], commitment); const args = this._buildArgs([dataLength], commitment);
const unsafeRes = await this._rpcRequest( const unsafeRes = await this._rpcRequest(
'getMinimumBalanceForRentExemption', 'getMinimumBalanceForRentExemption',
args, args,
@ -2084,7 +2086,7 @@ export class Connection {
): Promise< ): Promise<
RpcResponseAndContext<{blockhash: Blockhash, feeCalculator: FeeCalculator}>, RpcResponseAndContext<{blockhash: Blockhash, feeCalculator: FeeCalculator}>,
> { > {
const args = this._argsWithCommitment([], commitment); const args = this._buildArgs([], commitment);
const unsafeRes = await this._rpcRequest('getRecentBlockhash', args); const unsafeRes = await this._rpcRequest('getRecentBlockhash', args);
const res = GetRecentBlockhashAndContextRpcResult(unsafeRes); const res = GetRecentBlockhashAndContextRpcResult(unsafeRes);
@ -2102,7 +2104,7 @@ export class Connection {
blockhash: Blockhash, blockhash: Blockhash,
commitment: ?Commitment, commitment: ?Commitment,
): Promise<RpcResponseAndContext<FeeCalculator | null>> { ): Promise<RpcResponseAndContext<FeeCalculator | null>> {
const args = this._argsWithCommitment([blockhash], commitment); const args = this._buildArgs([blockhash], commitment);
const unsafeRes = await this._rpcRequest( const unsafeRes = await this._rpcRequest(
'getFeeCalculatorForBlockhash', 'getFeeCalculatorForBlockhash',
args, args,
@ -2613,7 +2615,7 @@ export class Connection {
this._subscribe( this._subscribe(
sub, sub,
'accountSubscribe', 'accountSubscribe',
this._argsWithCommitment([sub.publicKey], sub.commitment), this._buildArgs([sub.publicKey], sub.commitment, 'binary64'),
); );
} }
@ -2622,7 +2624,7 @@ export class Connection {
this._subscribe( this._subscribe(
sub, sub,
'programSubscribe', 'programSubscribe',
this._argsWithCommitment([sub.programId], sub.commitment), this._buildArgs([sub.programId], sub.commitment, 'binary64'),
); );
} }
@ -2636,7 +2638,7 @@ export class Connection {
this._subscribe( this._subscribe(
sub, sub,
'signatureSubscribe', 'signatureSubscribe',
this._argsWithCommitment([sub.signature], sub.commitment), this._buildArgs([sub.signature], sub.commitment),
); );
} }
@ -2667,7 +2669,7 @@ export class Connection {
executable: value.executable, executable: value.executable,
owner: new PublicKey(value.owner), owner: new PublicKey(value.owner),
lamports: value.lamports, lamports: value.lamports,
data: bs58.decode(value.data), data: Buffer.from(value.data, 'base64'),
}, },
context, context,
); );
@ -2743,7 +2745,7 @@ export class Connection {
executable: value.account.executable, executable: value.account.executable,
owner: new PublicKey(value.account.owner), owner: new PublicKey(value.account.owner),
lamports: value.account.lamports, lamports: value.account.lamports,
data: bs58.decode(value.account.data), data: Buffer.from(value.account.data, 'base64'),
}, },
}, },
context, context,
@ -2850,10 +2852,10 @@ export class Connection {
} }
} }
_argsWithCommitment( _buildArgs(
args: Array<any>, args: Array<any>,
override: ?Commitment, override: ?Commitment,
encoding?: 'jsonParsed', encoding?: 'jsonParsed' | 'binary64',
): Array<any> { ): Array<any> {
const commitment = override || this._commitment; const commitment = override || this._commitment;
if (commitment || encoding) { if (commitment || encoding) {

View File

@ -63,7 +63,7 @@ test('get account info - not found', async () => {
url, url,
{ {
method: 'getAccountInfo', method: 'getAccountInfo',
params: [account.publicKey.toBase58()], params: [account.publicKey.toBase58(), {encoding: 'binary64'}],
}, },
{ {
error: null, error: null,
@ -253,7 +253,10 @@ test('get program accounts', async () => {
url, url,
{ {
method: 'getProgramAccounts', method: 'getProgramAccounts',
params: [programId.publicKey.toBase58(), {commitment: 'recent'}], params: [
programId.publicKey.toBase58(),
{commitment: 'recent', encoding: 'binary64'},
],
}, },
{ {
error: null, error: null,
@ -1659,7 +1662,10 @@ test('request airdrop', async () => {
url, url,
{ {
method: 'getAccountInfo', method: 'getAccountInfo',
params: [account.publicKey.toBase58(), {commitment: 'recent'}], params: [
account.publicKey.toBase58(),
{commitment: 'recent', encoding: 'binary64'},
],
}, },
{ {
error: null, error: null,

View File

@ -22,7 +22,7 @@ const expectedData = (authorizedPubkey: PublicKey): string => {
const mockNonce = new Account(); const mockNonce = new Account();
mockNonce.publicKey.toBuffer().copy(expectedData, 40); // Hash, 32 bytes mockNonce.publicKey.toBuffer().copy(expectedData, 40); // Hash, 32 bytes
expectedData.writeUInt16LE(5000, 72); // feeCalculator, 8 bytes expectedData.writeUInt16LE(5000, 72); // feeCalculator, 8 bytes
return bs58.encode(expectedData); return expectedData.toString('base64');
}; };
test('create and query nonce account', async () => { test('create and query nonce account', async () => {