-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RPC: Get access keys for account (#410)
- Loading branch information
Showing
4 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |