Skip to content

Commit

Permalink
Extract viewAccessKey method
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrichina committed Mar 2, 2024
1 parent ce42296 commit d8181c6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
34 changes: 6 additions & 28 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ const cors = require('@koa/cors');
const resolveBlockHeightUtil = require('./resolve-block-height');
const isJSON = require('./utils/is-json');
const { runContract, getWasmModule } = require('./run-contract');
const { viewAccessKey } = require('./utils/view-access-key');
const storage = require('./storage');
const { accountKey, accessKeyKey } = require('./storage-keys');
const { accountKey } = require('./storage-keys');
const { deserialize } = require('borsh');
const bs58 = require('bs58');
const { BORSH_SCHEMA, Account, AccessKey, } = require('./data-model');
const { BORSH_SCHEMA, Account } = require('./data-model');

const parseQueryArgs = async (ctx, next) => {
// TODO: Refactor/merge with web4?
Expand Down Expand Up @@ -147,35 +148,12 @@ router.get('/account/:accountId/contract/methods', resolveBlockHeight, async ctx
router.get('/account/:accountId/key/:publicKey', resolveBlockHeight, async ctx => {
const { accountId, publicKey } = ctx.params;

// TODO: Refactor with JSON-RPC version and other similar methods?
const storageKey = accessKeyKey(accountId, publicKey);
const data = await storage.getLatestData(storageKey, ctx.blockHeight);
if (!data) {
const accessKey = await viewAccessKey({ accountId, publicKey, blockHeight: ctx.blockHeight });
if (!accessKey) {
ctx.throw(404);
}

const { nonce, permission: { functionCall, fullAccess } } = deserialize(BORSH_SCHEMA, AccessKey, data);
let permission;
if (functionCall) {
const { allowance, receiverId, methodNames } = functionCall;
permission = {
type: 'FunctionCall',
method_names: methodNames,
receiver_id: receiverId,
allowance: allowance.toString(10)
}
} else if (fullAccess) {
permission = {
type: 'FullAccess'
}
} else {
ctx.throw(500, 'unexpected permission type');
}
ctx.body = {
public_key: publicKey,
nonce: nonce.toString(),
...permission
};
ctx.body = accessKey;
});

const MAX_BLOCK_LAG_TIME_MS = 60_000;
Expand Down
41 changes: 41 additions & 0 deletions utils/view-access-key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { FastNEARError } = require('../error');
const storage = require('../storage');
const { accessKeyKey } = require('../storage-keys');
const { BORSH_SCHEMA, AccessKey, } = require('../data-model');
const { deserialize } = require('borsh');

async function viewAccessKey({ accountId, publicKey, blockHeight }) {
const storageKey = accessKeyKey(accountId, publicKey);
const data = await storage.getLatestData(storageKey, blockHeight);
if (!data) {
return null;
}

const { nonce, permission: { functionCall, fullAccess } } = deserialize(BORSH_SCHEMA, AccessKey, data);
let permission;
if (functionCall) {
const { allowance, receiverId, methodNames } = functionCall;
permission = {
type: 'FunctionCall',
method_names: methodNames,
receiver_id: receiverId,
allowance: allowance.toString(10)
}
} else if (fullAccess) {
permission = {
type: 'FullAccess'
}
} else {
throw FastNEARError('unexpectedPermissionType', 'unexpected permission type');
}

return {
public_key: publicKey,
nonce: nonce.toString(),
...permission
};
}

module.exports = {
viewAccessKey
};

0 comments on commit d8181c6

Please sign in to comment.