From 23b3ce36b48a8b43dc2bdf5890594df050d50796 Mon Sep 17 00:00:00 2001 From: Benjamin Smith Date: Fri, 15 Nov 2024 14:55:12 +0100 Subject: [PATCH] Root Public Key as SetupConfig (#148) --- src/index.ts | 4 +++- src/mpcContract.ts | 12 ++++++++---- src/utils/kdf.ts | 4 ++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 97b7138..b890ff7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,6 +27,7 @@ type KeyPairString = `ed25519:${string}` | `secp256k1:${string}`; * @property {NearConfig} [network] - (Optional) The NEAR network configuration. * @property {string} [privateKey] - (Optional) The private key for the account. * @property {string} [derivationPath] - (Optional) The derivation path for the Ethereum account. Defaults to "ethereum,1". + * @property {string} [rootPublicKey] - (Optional) The root public key for the account. If not available it will be fetched from the MPC contract. */ export interface SetupConfig { accountId: string; @@ -34,6 +35,7 @@ export interface SetupConfig { network?: NearConfig; privateKey?: string; derivationPath?: string; + rootPublicKey?: string; } /** @@ -79,7 +81,7 @@ export async function setupAdapter(args: SetupConfig): Promise { throw error; } return NearEthAdapter.fromConfig({ - mpcContract: new MpcContract(account, mpcContractId), + mpcContract: new MpcContract(account, mpcContractId, args.rootPublicKey), derivationPath: derivationPath, }); } diff --git a/src/mpcContract.ts b/src/mpcContract.ts index b264c54..f8de388 100644 --- a/src/mpcContract.ts +++ b/src/mpcContract.ts @@ -44,11 +44,13 @@ interface MpcContractInterface extends Contract { * located in: https://github.com/near/mpc-recovery */ export class MpcContract implements IMpcContract { + rootPublicKey: string | undefined; contract: MpcContractInterface; connectedAccount: Account; - constructor(account: Account, contractId: string) { + constructor(account: Account, contractId: string, rootPublicKey?: string) { this.connectedAccount = account; + this.rootPublicKey = rootPublicKey; this.contract = new Contract(account.connection, contractId, { changeMethods: ["sign"], @@ -66,10 +68,12 @@ export class MpcContract implements IMpcContract { } deriveEthAddress = async (derivationPath: string): Promise
=> { - const rootPublicKey = await this.contract.public_key(); + if (!this.rootPublicKey) { + this.rootPublicKey = await this.contract.public_key(); + } - const publicKey = await deriveChildPublicKey( - najPublicKeyStrToUncompressedHexPoint(rootPublicKey), + const publicKey = deriveChildPublicKey( + najPublicKeyStrToUncompressedHexPoint(this.rootPublicKey), this.connectedAccount.accountId, derivationPath ); diff --git a/src/utils/kdf.ts b/src/utils/kdf.ts index e2b6529..e4d43eb 100644 --- a/src/utils/kdf.ts +++ b/src/utils/kdf.ts @@ -10,11 +10,11 @@ export function najPublicKeyStrToUncompressedHexPoint( return "04" + Buffer.from(decodedKey).toString("hex"); } -export async function deriveChildPublicKey( +export function deriveChildPublicKey( parentUncompressedPublicKeyHex: string, signerId: string, path: string = "" -): Promise { +): string { const ec = new EC("secp256k1"); const scalarHex = sha3_256( `near-mpc-recovery v0.1.0 epsilon derivation:${signerId},${path}`