Skip to content

Commit

Permalink
Merge branch 'signer-from-keypair' into fix-multisig-4
Browse files Browse the repository at this point in the history
  • Loading branch information
mattlockyer committed Nov 14, 2020
2 parents 20f0073 + e958b50 commit e9f469b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
10 changes: 9 additions & 1 deletion lib/signer.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions lib/signer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion src/signer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sha256 from 'js-sha256';
import { Signature, KeyPair, PublicKey } from './utils/key_pair';
import { KeyStore } from './key_stores';
import { KeyStore, InMemoryKeyStore } from './key_stores';

/**
* General signing interface, can be used for in memory signing, RPC singing, external wallet, HSM, etc.
Expand Down Expand Up @@ -38,6 +38,19 @@ export class InMemorySigner extends Signer {
super();
this.keyStore = keyStore;
}

/**
* Creates a tempSigner (intended to be one time use) with account, network and keyPair provided
* @param networkId The targeted network. (ex. default, betanet, etc…)
* @param accountId The NEAR account to assign a public key to
* @param keyPair The keyPair to use for signing
* @returns {Promise<PublicKey>}
*/
static async fromKeyPair(networkId: string, accountId: string, keyPair: KeyPair): Promise<Signer> {
const keyStore = new InMemoryKeyStore()
await keyStore.setKey(networkId, accountId, keyPair);
return new InMemorySigner(keyStore);
}

/**
* Creates a public key for the account given
Expand Down
5 changes: 1 addition & 4 deletions test/account_multisig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const {
KeyPair,
transactions: { functionCall },
InMemorySigner,
keyStores: { InMemoryKeyStore },
multisig: { Account2FA, MULTISIG_GAS, MULTISIG_DEPOSIT },
utils: { format: { parseNearAmount } }
} = nearApi;
Expand All @@ -33,9 +32,7 @@ const getAccount2FA = async (account, keyMapping = ({ public_key: publicKey }) =
const { requestId } = account2fa.getRequest();
// set confirmKey as signer
const originalSigner = nearjs.connection.signer;
const tempKeyStore = new InMemoryKeyStore();
await tempKeyStore.setKey(nearjs.connection.networkId, accountId, account2fa.confirmKey);
nearjs.connection.signer = new InMemorySigner(tempKeyStore);
nearjs.connection.signer = await InMemorySigner.fromKeyPair(nearjs.connection.networkId, accountId, account2fa.confirmKey);
// 2nd confirmation signing with confirmKey from Account instance
await account.signAndSendTransaction(accountId, [
functionCall('confirm', { request_id: requestId }, MULTISIG_GAS, MULTISIG_DEPOSIT)
Expand Down

0 comments on commit e9f469b

Please sign in to comment.