diff --git a/lib/signer.d.ts b/lib/signer.d.ts index 9322c3a10e..d9f4ae8de8 100644 --- a/lib/signer.d.ts +++ b/lib/signer.d.ts @@ -1,4 +1,4 @@ -import { Signature, PublicKey } from './utils/key_pair'; +import { Signature, KeyPair, PublicKey } from './utils/key_pair'; import { KeyStore } from './key_stores'; /** * General signing interface, can be used for in memory signing, RPC singing, external wallet, HSM, etc. @@ -28,6 +28,14 @@ export declare abstract class Signer { export declare class InMemorySigner extends Signer { readonly keyStore: KeyStore; constructor(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} + */ + static fromKeyPair(networkId: string, accountId: string, keyPair: KeyPair): Promise; /** * Creates a public key for the account given * @param accountId The NEAR account to assign a public key to diff --git a/lib/signer.js b/lib/signer.js index 50c8dde10b..778fca68a6 100644 --- a/lib/signer.js +++ b/lib/signer.js @@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.InMemorySigner = exports.Signer = void 0; const js_sha256_1 = __importDefault(require("js-sha256")); const key_pair_1 = require("./utils/key_pair"); +const key_stores_1 = require("./key_stores"); /** * General signing interface, can be used for in memory signing, RPC singing, external wallet, HSM, etc. */ @@ -20,6 +21,18 @@ 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} + */ + static async fromKeyPair(networkId, accountId, keyPair) { + const keyStore = new key_stores_1.InMemoryKeyStore(); + await keyStore.setKey(networkId, accountId, keyPair); + return new InMemorySigner(keyStore); + } /** * Creates a public key for the account given * @param accountId The NEAR account to assign a public key to diff --git a/src/signer.ts b/src/signer.ts index 1c9d481596..4f0b51ed19 100644 --- a/src/signer.ts +++ b/src/signer.ts @@ -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. @@ -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} + */ + static async fromKeyPair(networkId: string, accountId: string, keyPair: KeyPair): Promise { + const keyStore = new InMemoryKeyStore() + await keyStore.setKey(networkId, accountId, keyPair); + return new InMemorySigner(keyStore); + } /** * Creates a public key for the account given diff --git a/test/account_multisig.test.js b/test/account_multisig.test.js index 24ef08d188..464a366a85 100644 --- a/test/account_multisig.test.js +++ b/test/account_multisig.test.js @@ -12,7 +12,6 @@ const { KeyPair, transactions: { functionCall }, InMemorySigner, - keyStores: { InMemoryKeyStore }, multisig: { Account2FA, MULTISIG_GAS, MULTISIG_DEPOSIT }, utils: { format: { parseNearAmount } } } = nearApi; @@ -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)