Skip to content

Commit

Permalink
RPC: Get access keys for account (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
tifrel committed Oct 16, 2023
1 parent da7ccd4 commit 324c6ad
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<AccessKey>`

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.
We will be adding more contract view methods here as needs arise.
1 change: 1 addition & 0 deletions packages/rpc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './methods/balance';
export * from './methods/payouts';
export * from './methods/account';
export * from './methods/social';
export * from './methods/keys';
44 changes: 44 additions & 0 deletions packages/rpc/src/methods/keys.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import fetch from 'isomorphic-unfetch';
import { AccessKey, getAccessKeys } from './keys';

jest.mock('isomorphic-unfetch');

describe('keys', () => {
const getRes = async (): Promise<AccessKey[]> => 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');
});
});
36 changes: 36 additions & 0 deletions packages/rpc/src/methods/keys.ts
Original file line number Diff line number Diff line change
@@ -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<AccessKey[]> => {
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;
};

0 comments on commit 324c6ad

Please sign in to comment.