From 324c6ad3e7c6d9b1b815d7d644b3808931c97e6f Mon Sep 17 00:00:00 2001 From: Till <31419678+tifrel@users.noreply.github.com> Date: Mon, 16 Oct 2023 13:38:11 +0300 Subject: [PATCH] RPC: Get access keys for account (#410) --- packages/rpc/README.md | 6 +++- packages/rpc/src/index.ts | 1 + packages/rpc/src/methods/keys.test.ts | 44 +++++++++++++++++++++++++++ packages/rpc/src/methods/keys.ts | 36 ++++++++++++++++++++++ 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 packages/rpc/src/methods/keys.test.ts create mode 100644 packages/rpc/src/methods/keys.ts diff --git a/packages/rpc/README.md b/packages/rpc/README.md index ad0ba9532..5672179e5 100644 --- a/packages/rpc/README.md +++ b/packages/rpc/README.md @@ -29,10 +29,14 @@ For a transaction hash, determine the status of a transaction on the configured Calls a token contract in order to determine the percentage amounts paid out to royalty accounts. +### `getAccessKeys(accountId: string): Promise` + +Gets all access keys (public key and permissions object) for a given account. + ## Configuration Before calling these methods the near network should be configured using the [config SDK method](https://docs.mintbase.io/dev/mintbase-sdk-ref/sdk/config) ## Future -We will be adding more contract view methods here as needs arise. \ No newline at end of file +We will be adding more contract view methods here as needs arise. diff --git a/packages/rpc/src/index.ts b/packages/rpc/src/index.ts index 49ce3ff50..2ea57c563 100644 --- a/packages/rpc/src/index.ts +++ b/packages/rpc/src/index.ts @@ -4,3 +4,4 @@ export * from './methods/balance'; export * from './methods/payouts'; export * from './methods/account'; export * from './methods/social'; +export * from './methods/keys'; diff --git a/packages/rpc/src/methods/keys.test.ts b/packages/rpc/src/methods/keys.test.ts new file mode 100644 index 000000000..bae0c71d2 --- /dev/null +++ b/packages/rpc/src/methods/keys.test.ts @@ -0,0 +1,44 @@ +import fetch from 'isomorphic-unfetch'; +import { AccessKey, getAccessKeys } from './keys'; + +jest.mock('isomorphic-unfetch'); + +describe('keys', () => { + const getRes = async (): Promise => await getAccessKeys('benipsen.near'); + + it('should return access keys for accounts', async () => { + (fetch as jest.Mock).mockResolvedValueOnce({ + json: jest.fn().mockResolvedValueOnce({ + result: { + keys: [ + { public_key: 'pubkey', permission: 'FullAccess' }, + ], + }, + }), + }); + const res = await getRes(); + expect(res.length).toBe(1); + expect(res[0].public_key).toBe('pubkey'); + expect(res[0].permission).toBe('FullAccess'); + }); + + it('should throw on returned error', async () => { + (fetch as jest.Mock).mockResolvedValueOnce({ + json: jest.fn().mockResolvedValueOnce({ + result: { error: 'some error' }, + }), + }); + + await expect(getRes).rejects.toThrow('some error'); + }); + + it('should throw on malformed response', async () => { + (fetch as jest.Mock).mockResolvedValueOnce({ + json: jest.fn().mockResolvedValueOnce({ + result: { foo: 'bar' }, + }), + }); + + await expect(getRes).rejects.toThrow('Malformed response'); + }); +}); diff --git a/packages/rpc/src/methods/keys.ts b/packages/rpc/src/methods/keys.ts new file mode 100644 index 000000000..8c6123c9f --- /dev/null +++ b/packages/rpc/src/methods/keys.ts @@ -0,0 +1,36 @@ +import { requestFromNearRpc } from '../util'; + +export type AccessKey = { + public_key: string; + permission: AccessKeyPermissions; +}; + +export type AccessKeyPermissions = 'FullAccess' | { + 'FunctionCall': { + allowance: string; + receiver_id: string; + method_names: string[]; + }; +} + +export const getAccessKeys = async (accountId: string): Promise => { + const res = await requestFromNearRpc({ + jsonrpc: '2.0', + id: 'dontcare', + method: 'query', + params: { + request_type: 'view_access_key_list', + finality: 'final', + account_id: accountId, + }, + }); + + const accessKeys = res?.result?.keys; + if (res?.error) { + throw res.error; + } + if (!accessKeys) { + throw new Error(`Malformed response: ${JSON.stringify(res)}`); + } + return accessKeys; +};