Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Root Public Key as SetupConfig #148

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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