Skip to content

Commit

Permalink
feat: add multi_contract_keystore tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gtsonevv committed Aug 20, 2024
1 parent 99114f7 commit 9c7db11
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/clean-cougars-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@near-js/keystores": minor
"@near-js/keystores-browser": minor
---

Add multi_contract_keystore
13 changes: 12 additions & 1 deletion packages/keystores-browser/test/browser_keystore.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { BrowserLocalStorageKeyStore } = require('../lib');
const { BrowserLocalStorageKeyStore, MultiContractBrowserLocalStorageKeyStore } = require('../lib');

describe('Browser keystore', () => {
let ctx = {};
Expand All @@ -9,3 +9,14 @@ describe('Browser keystore', () => {

require('./keystore_common').shouldStoreAndRetrieveKeys(ctx);
});


describe('Browser multi keystore', () => {
let ctx = {};

beforeAll(async () => {
ctx.keyStore = new MultiContractBrowserLocalStorageKeyStore(require('localstorage-memory'));
});

require('./multi_contract_browser_keystore_common').shouldStoreAndRetrieveKeys(ctx);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { KeyPairEd25519 } = require('@near-js/crypto');

const NETWORK_ID = 'networkid';
const ACCOUNT_ID = 'accountid';
const CONTRACT_ID = 'contractid';
const KEYPAIR = new KeyPairEd25519('2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw');

module.exports.shouldStoreAndRetrieveKeys = ctx => {
beforeEach(async () => {
await ctx.keyStore.clear();
await ctx.keyStore.setKey(NETWORK_ID, ACCOUNT_ID, KEYPAIR, CONTRACT_ID);
});

test('Get all keys with empty network returns empty list', async () => {
const emptyList = await ctx.keyStore.getAccounts('emptynetwork');
expect(emptyList).toEqual([]);
});

test('Get all keys with single key in keystore', async () => {
const accountIds = await ctx.keyStore.getAccounts(NETWORK_ID);
expect(accountIds).toEqual([ACCOUNT_ID]);
});

test('Get not-existing account', async () => {
expect(await ctx.keyStore.getKey('somenetwork', 'someaccount', 'somecontract')).toBeNull();
});

test('Get account id from a network with single key', async () => {
const key = await ctx.keyStore.getKey(NETWORK_ID, ACCOUNT_ID, CONTRACT_ID);
expect(key).toEqual(KEYPAIR);
});

test('Get networks', async () => {
const networks = await ctx.keyStore.getNetworks();
expect(networks).toEqual([NETWORK_ID]);
});

test('Get accounts', async () => {
const accounts = await ctx.keyStore.getAccounts(NETWORK_ID);
expect(accounts).toEqual([ACCOUNT_ID]);
});

test('Get contracts', async () => {
const contracts = await ctx.keyStore.getContracts(NETWORK_ID, ACCOUNT_ID);
expect(contracts).toEqual([CONTRACT_ID]);
});

test('Add two contracts to account and retrieve them', async () => {
const networkId = "network"
const accountId = "account"
const contract1 = "contract1"
const contract2 = "contract2"
const key1Expected = KeyPairEd25519.fromRandom();
const key2Expected = KeyPairEd25519.fromRandom();
await ctx.keyStore.setKey(networkId, accountId, key1Expected, contract1);
await ctx.keyStore.setKey(networkId, accountId, key2Expected, contract2);
const key1 = await ctx.keyStore.getKey(networkId, accountId, contract1);
const key2 = await ctx.keyStore.getKey(networkId, accountId, contract2);
expect(key1).toEqual(key1Expected);
expect(key2).toEqual(key2Expected);
const contractIds = await ctx.keyStore.getContracts(networkId, accountId);
expect(contractIds).toEqual([contract1, contract2]);
});
};

0 comments on commit 9c7db11

Please sign in to comment.