From e3d9540cc010b20069f5341016c75441e857e48e Mon Sep 17 00:00:00 2001 From: Joe Date: Fri, 4 Aug 2023 11:24:59 -0600 Subject: [PATCH] refactor(experimental): add getIdentity API method --- .../__tests__/get-identity-test.ts | 30 +++++++++++++++++++ .../rpc-core/src/rpc-methods/getIdentity.ts | 15 ++++++++++ packages/rpc-core/src/rpc-methods/index.ts | 2 ++ packages/rpc-core/tsconfig.json | 3 +- 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 packages/rpc-core/src/rpc-methods/__tests__/get-identity-test.ts create mode 100644 packages/rpc-core/src/rpc-methods/getIdentity.ts diff --git a/packages/rpc-core/src/rpc-methods/__tests__/get-identity-test.ts b/packages/rpc-core/src/rpc-methods/__tests__/get-identity-test.ts new file mode 100644 index 000000000000..63a14780335e --- /dev/null +++ b/packages/rpc-core/src/rpc-methods/__tests__/get-identity-test.ts @@ -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; + beforeEach(() => { + fetchMock.resetMocks(); + fetchMock.dontMock(); + rpc = createJsonRpc({ + 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, + }); + }); +}); diff --git a/packages/rpc-core/src/rpc-methods/getIdentity.ts b/packages/rpc-core/src/rpc-methods/getIdentity.ts new file mode 100644 index 000000000000..25a7512286f8 --- /dev/null +++ b/packages/rpc-core/src/rpc-methods/getIdentity.ts @@ -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 + ): GetIdentityApiResponse; +} diff --git a/packages/rpc-core/src/rpc-methods/index.ts b/packages/rpc-core/src/rpc-methods/index.ts index f645368aa518..1ca96410df48 100644 --- a/packages/rpc-core/src/rpc-methods/index.ts +++ b/packages/rpc-core/src/rpc-methods/index.ts @@ -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'; @@ -51,6 +52,7 @@ export type SolanaRpcMethods = GetAccountInfoApi & GetFirstAvailableBlockApi & GetHealthApi & GetHighestSnapshotSlotApi & + GetIdentityApi & GetInflationRewardApi & GetLatestBlockhashApi & GetMaxRetransmitSlotApi & diff --git a/packages/rpc-core/tsconfig.json b/packages/rpc-core/tsconfig.json index f7a42ce4ebea..cc88735f00fe 100644 --- a/packages/rpc-core/tsconfig.json +++ b/packages/rpc-core/tsconfig.json @@ -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",