Skip to content

Commit

Permalink
Remove dotenv Constructors (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith authored Jul 1, 2024
1 parent d837525 commit 91833e8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 53 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,28 @@ import dotenv from "dotenv";
import {
MultichainContract,
NearEthAdapter,
nearAccountFromEnv,
nearAccountFromKeyPair,
} from "near-ca";
import { KeyPair } from "near-api-js";

dotenv.config();
const { NEAR_ACCOUNT_ID, NEAR_ACCOUNT_PRIVATE_KEY } = process.env;

const account = await nearAccountFromEnv();
const account = await nearAccountFromKeyPair({
accountId: NEAR_ACCOUNT_ID!,
keyPair: KeyPair.fromString(NEAR_ACCOUNT_PRIVATE_KEY!),
network: {
networkId: "testnet",
nodeUrl: "https://rpc.testnet.near.org",
},
});

const adapter = await NearEthAdapter.fromConfig({
mpcContract: new MultichainContract(
account,
process.env.NEAR_MULTICHAIN_CONTRACT!
),
derivationPath: "ethereum,1",
// derivationPath: "ethereum,1",
});

await adapter.signAndSendTransaction({
Expand Down
19 changes: 16 additions & 3 deletions examples/setup.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import dotenv from "dotenv";
import { MultichainContract, NearEthAdapter, nearAccountFromEnv } from "../src";
import {
MultichainContract,
NearEthAdapter,
nearAccountFromKeyPair,
} from "../src";
import { KeyPair } from "near-api-js";

// This is Sepolia, but can be replaced with nearly any EVM network.
export const SEPOLIA_CHAIN_ID = 11_155_111;
const TESTNET_CONFIG = {
networkId: "testnet",
nodeUrl: "https://rpc.testnet.near.org",
};

export async function setupNearEthAdapter(): Promise<NearEthAdapter> {
dotenv.config();
const account = await nearAccountFromEnv();
const account = await nearAccountFromKeyPair({
keyPair: KeyPair.fromString(process.env.NEAR_ACCOUNT_PRIVATE_KEY!),
accountId: process.env.NEAR_ACCOUNT_ID!,
network: TESTNET_CONFIG,
});
return NearEthAdapter.fromConfig({
mpcContract: new MultichainContract(
account,
process.env.NEAR_MULTICHAIN_CONTRACT!
),
derivationPath: "ethereum,1",
// derivationPath: "ethereum,1",
});
}

Expand Down
53 changes: 16 additions & 37 deletions src/chains/near.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,6 @@ export interface NearConfig {
nodeUrl: string;
}

const TESTNET_CONFIG = {
networkId: "testnet",
nodeUrl: "https://rpc.testnet.near.org",
};

/**
* Loads Near Account from process.env.
* Defaults to TESTNET_CONFIG if no network configuration is provided
* @param network {NearConfig} network settings
* @returns {Account}
*/
export const nearAccountFromEnv = async (
network?: NearConfig
): Promise<Account> => {
const keyPair = KeyPair.fromString(process.env.NEAR_ACCOUNT_PRIVATE_KEY!);
return nearAccountFromKeyPair({
keyPair,
accountId: process.env.NEAR_ACCOUNT_ID!,
network: network || TESTNET_CONFIG,
});
};

/**
* Loads Near Account from provided keyPair and accountId
* Defaults to TESTNET_CONFIG
Expand All @@ -41,30 +19,31 @@ export const nearAccountFromEnv = async (
export const nearAccountFromKeyPair = async (config: {
keyPair: KeyPair;
accountId: string;
network?: NearConfig;
network: NearConfig;
}): Promise<Account> => {
const keyStore = new keyStores.InMemoryKeyStore();
await keyStore.setKey("testnet", config.accountId, config.keyPair);
const near = await connect({
...(config.network || TESTNET_CONFIG),
keyStore,
});
const account = await near.account(config.accountId);
return account;
return createNearAccount(config.accountId, config.network, config.keyPair);
};

/** Minimally sufficient Account instance to construct the signing contract instance.
* Can't be used to change methods.
*/
export const nearAccountFromAccountId = async (
accountId: string,
network?: NearConfig
network: NearConfig
): Promise<Account> => {
return createNearAccount(accountId, network);
};

const createNearAccount = async (
accountId: string,
network: NearConfig,
keyPair?: KeyPair
): Promise<Account> => {
const keyStore = new keyStores.InMemoryKeyStore();
const near = await connect({
...(network || TESTNET_CONFIG),
keyStore,
});
if (keyPair) {
await keyStore.setKey(network.networkId, accountId, keyPair);
}
const near = await connect({ ...network, keyStore });
const account = await near.account(accountId);
return account;
};
};
15 changes: 5 additions & 10 deletions src/mpcContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {
najPublicKeyStrToUncompressedHexPoint,
uncompressedHexPointToEvmAddress,
} from "./utils/kdf";
import { NO_DEPOSIT, nearAccountFromEnv, TGAS } from "./chains/near";
import { NO_DEPOSIT, TGAS } from "./chains/near";
import {
MPCSignature,
NearContractFunctionPayload,
SignArgs,
} from "./types/types";

const DEFAULT_MPC_CONTRACT = "v2.multichain-mpc.testnet";

/// Near Contract Type for change methods
export interface ChangeMethodArgs<T> {
/// Change method function agruments.
Expand All @@ -38,23 +40,16 @@ export class MultichainContract {
contract: MultichainContractInterface;
connectedAccount: Account;

constructor(account: Account, contractId: string) {
constructor(account: Account, contractId: string = DEFAULT_MPC_CONTRACT) {
this.connectedAccount = account;

this.contract = new Contract(account, contractId, {
changeMethods: ["sign"],
viewMethods: ["public_key"],
useLocalViewExecution: false,
}) as MultichainContractInterface;
}

static async fromEnv(): Promise<MultichainContract> {
const account = await nearAccountFromEnv();
return new MultichainContract(
account,
process.env.NEAR_MULTICHAIN_CONTRACT!
);
}

deriveEthAddress = async (derivationPath: string): Promise<Address> => {
const rootPublicKey = await this.contract.public_key();

Expand Down

0 comments on commit 91833e8

Please sign in to comment.