Skip to content

Commit

Permalink
refactor(experimental): add getIdentity API method
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Aug 4, 2023
1 parent f55f3e7 commit e3d9540
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
30 changes: 30 additions & 0 deletions packages/rpc-core/src/rpc-methods/__tests__/get-identity-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { base58 } from '@metaplex-foundation/umi-serializers';
import { createHttpTransport, createJsonRpc } from '@solana/rpc-transport';
import type { Rpc } from '@solana/rpc-transport/dist/types/json-rpc-types';
import fetchMock from 'jest-fetch-mock-fork';

import validatorIdentityBytes from '../../../../../test-ledger/validator-keypair.json';
import { createSolanaRpcApi, SolanaRpcMethods } from '../index';

describe('getIdentity', () => {
let rpc: Rpc<SolanaRpcMethods>;
beforeEach(() => {
fetchMock.resetMocks();
fetchMock.dontMock();
rpc = createJsonRpc<SolanaRpcMethods>({
api: createSolanaRpcApi(),
transport: createHttpTransport({ url: 'http://127.0.0.1:8899' }),
});
});

it('returns the identity of the currently running local validator', async () => {
expect.assertions(1);
const secretKey = new Uint8Array(validatorIdentityBytes);
const publicKey = secretKey.slice(32, 64);
const expectedAddress = base58.deserialize(publicKey)[0];
const identityPromise = rpc.getIdentity().send();
await expect(identityPromise).resolves.toMatchObject({
identity: expectedAddress,
});
});
});
15 changes: 15 additions & 0 deletions packages/rpc-core/src/rpc-methods/getIdentity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Base58EncodedAddress } from '@solana/addresses';

type GetIdentityApiResponse = Readonly<{
identity: Base58EncodedAddress;
}>;

export interface GetIdentityApi {
/**
* Returns the identity pubkey for the current node
*/
getIdentity(
// FIXME: https://github.com/solana-labs/solana-web3.js/issues/1389
NO_CONFIG?: Record<string, never>
): GetIdentityApiResponse;
}
2 changes: 2 additions & 0 deletions packages/rpc-core/src/rpc-methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { GetEpochScheduleApi } from './getEpochSchedule';
import { GetFirstAvailableBlockApi } from './getFirstAvailableBlock';
import { GetHealthApi } from './getHealth';
import { GetHighestSnapshotSlotApi } from './getHighestSnapshotSlot';
import { GetIdentityApi } from './getIdentity';
import { GetInflationRewardApi } from './getInflationReward';
import { GetLatestBlockhashApi } from './getLatestBlockhash';
import { GetMaxRetransmitSlotApi } from './getMaxRetransmitSlot';
Expand Down Expand Up @@ -51,6 +52,7 @@ export type SolanaRpcMethods = GetAccountInfoApi &
GetFirstAvailableBlockApi &
GetHealthApi &
GetHighestSnapshotSlotApi &
GetIdentityApi &
GetInflationRewardApi &
GetLatestBlockhashApi &
GetMaxRetransmitSlotApi &
Expand Down
3 changes: 2 additions & 1 deletion packages/rpc-core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"lib": ["DOM", "ES2017", "ES2020.BigInt", "ES2022.Error"]
"lib": ["DOM", "ES2017", "ES2020.BigInt", "ES2022.Error"],
"resolveJsonModule": true
},
"display": "@solana/rpc-core",
"extends": "tsconfig/base.json",
Expand Down

0 comments on commit e3d9540

Please sign in to comment.