Skip to content

Commit

Permalink
feat: optional networks (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoch authored Oct 4, 2024
1 parent f3fc09d commit e8847b4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 10 deletions.
6 changes: 6 additions & 0 deletions apps/idos-example-dapp/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ const devBackendPlugin = () => ({
export default defineConfig({
build: {
target: "esnext",
rollupOptions: {
manualChunks: {
ethers: ["ethers"],
"near-api-js": ["near-api-js"],
},
},
},
plugins: [
nodePolyfills({
Expand Down
10 changes: 10 additions & 0 deletions packages/idos-sdk-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
"ethers": "^6.12",
"near-api-js": "^3.0"
},
"peerDependenciesMeta": {
"ethers": {
"optional": true
},
"near-api-js": {
"optional": true
}
},
"devDependencies": {
"@dotenvx/dotenvx": "^1.6.4",
"@idos-network/idos-sdk-types": "workspace:*",
Expand All @@ -56,10 +64,12 @@
"@stablelib/base64": "^1.0.1",
"@stablelib/binary": "^1.0.1",
"@stablelib/bytes": "^1.0.1",
"@stablelib/hex": "^2.0.0",
"@stablelib/sha256": "^1.0.1",
"@stablelib/utf8": "^1.0.1",
"borsh": "^1.0.0",
"es-toolkit": "^1.23.0",
"bs58": "^6.0.0",
"jsonld": "^8.3.1",
"jsonld-document-loader": "^2.0.0",
"tweetnacl": "^1.0.3"
Expand Down
14 changes: 11 additions & 3 deletions packages/idos-sdk-js/src/lib/grants/evm.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Contract, type Signer, type TransactionResponse, ZeroAddress } from "ethers";
import type { Contract, Signer, TransactionResponse, ethers as ethersT } from "ethers";
import Grant from "./grant";
import type { GrantChild } from "./grant-child";

const ZERO_ADDRESS = ZeroAddress;
// Inlined from Ethers to make it easier to make the package optional.
const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
const ZERO_DATA_ID = "0";
const ZERO_TIMELOCK = 0;

Expand Down Expand Up @@ -374,9 +375,16 @@ export class EvmGrants implements GrantChild {
signer,
options,
}: { signer: Signer; options: EvmGrantsOptions }): Promise<EvmGrants> {
let ethers: typeof ethersT;
try {
ethers = await import("ethers");
} catch (cause) {
throw new Error("Can't load ethers", { cause });
}

return new EvmGrants(
signer,
new Contract(
new ethers.Contract(
options.contractAddress ?? EvmGrants.#defaultContractAddress,
EvmGrants.#abi,
signer,
Expand Down
20 changes: 16 additions & 4 deletions packages/idos-sdk-js/src/lib/grants/near.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { SignMessageParams, SignedMessage, Wallet } from "@near-wallet-selector/core";
import * as Base64Codec from "@stablelib/base64";
import { Contract, connect, keyStores, type providers } from "near-api-js";
import type { Contract, connect, keyStores, providers } from "near-api-js";
import { Nonce } from "../nonce";
import Grant from "./grant";
import type { GrantChild } from "./grant-child";
Expand Down Expand Up @@ -57,15 +57,27 @@ export class NearGrants implements GrantChild {
options: NearGrantsOptions;
publicKey: string;
}): Promise<NearGrants> {
const keylessNearConnection = await connect({
let near_api: {
Contract: typeof Contract;
connect: typeof connect;
keyStores: typeof keyStores;
};
try {
const { Contract, connect, keyStores } = await import("near-api-js");
near_api = { Contract, connect, keyStores };
} catch (e) {
throw new Error("Can't load near-api-js");
}

const keylessNearConnection = await near_api.connect({
networkId: options.network ?? NearGrants.defaultNetwork,
keyStore: new keyStores.BrowserLocalStorageKeyStore(),
keyStore: new near_api.keyStores.BrowserLocalStorageKeyStore(),
nodeUrl: options.rpcUrl ?? NearGrants.defaultRpcUrl,
});

return new NearGrants(
signer,
new Contract(
new near_api.Contract(
await keylessNearConnection.account(accountId),
options.contractId ?? NearGrants.defaultContractId,
{
Expand Down
14 changes: 11 additions & 3 deletions packages/idos-sdk-js/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import type { AccessKeyList } from "@near-js/types";
import { decodeBase58, toBeHex } from "ethers";
import { connect } from "near-api-js";
import { encode as encodeHex } from "@stablelib/hex";
import bs58 from "bs58";
import type { connect as connectT } from "near-api-js";

export async function getNearFullAccessPublicKeys(
namedAddress: string,
): Promise<string[] | undefined> {
let connect: typeof connectT;
try {
connect = (await import("near-api-js")).connect;
} catch (e) {
throw new Error("Can't load near-api-js");
}

const connectionConfig = {
networkId: import.meta.env.VITE_IDOS_NEAR_DEFAULT_NETWORK,
nodeUrl: import.meta.env.VITE_IDOS_NEAR_DEFAULT_RPC_URL,
Expand All @@ -28,7 +36,7 @@ export async function getNearFullAccessPublicKeys(

export function implicitAddressFromPublicKey(publicKey: string) {
const key_without_prefix = publicKey.replace(/^ed25519:/, "");
const implicitAddress = toBeHex(decodeBase58(key_without_prefix));
const implicitAddress = encodeHex(bs58.decode(key_without_prefix));
return implicitAddress;
}

Expand Down
23 changes: 23 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e8847b4

Please sign in to comment.