Skip to content

Commit

Permalink
Root Public Key as SetupConfig (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith authored Nov 15, 2024
1 parent 6e8f6c0 commit 23b3ce3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ 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;
mpcContractId: string;
network?: NearConfig;
privateKey?: string;
derivationPath?: string;
rootPublicKey?: string;
}

/**
Expand Down Expand Up @@ -79,7 +81,7 @@ export async function setupAdapter(args: SetupConfig): Promise<NearEthAdapter> {
throw error;
}
return NearEthAdapter.fromConfig({
mpcContract: new MpcContract(account, mpcContractId),
mpcContract: new MpcContract(account, mpcContractId, args.rootPublicKey),
derivationPath: derivationPath,
});
}
12 changes: 8 additions & 4 deletions src/mpcContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -66,10 +68,12 @@ export class MpcContract implements IMpcContract {
}

deriveEthAddress = async (derivationPath: string): Promise<Address> => {
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
);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/kdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
): string {
const ec = new EC("secp256k1");
const scalarHex = sha3_256(
`near-mpc-recovery v0.1.0 epsilon derivation:${signerId},${path}`
Expand Down

0 comments on commit 23b3ce3

Please sign in to comment.