Skip to content

Commit

Permalink
feat: add fast auth iframe wallet module
Browse files Browse the repository at this point in the history
  • Loading branch information
esaminu committed Jul 28, 2023
1 parent c50c6d6 commit 889537e
Show file tree
Hide file tree
Showing 4 changed files with 515 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/components/vm/VmInitializer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import { recordWalletConnect, reset as resetSegment } from '@/utils/analytics';
import { networkId, signInContractId } from '@/utils/config';
import { KEYPOM_OPTIONS } from '@/utils/keypom-options';

import { setupFastAuthWallet } from '../../lib/selector/fast-auth-wallet';

export default function VmInitializer() {
const [signedIn, setSignedIn] = useState(false);
const [signedAccountId, setSignedAccountId] = useState(null);
Expand Down Expand Up @@ -68,14 +70,15 @@ export default function VmInitializer() {
}),
setupNightly(),
setupWelldoneWallet(),
setupFastAuth({
networkId,
signInContractId,
relayerUrl:
networkId === 'testnet'
? 'http://34.70.226.83:3030/relay'
: 'https://near-relayer-mainnet.api.pagoda.co/relay',
}) as any, // TODO: Refactor setupFastAuth() to TS
// setupFastAuth({
// networkId,
// signInContractId,
// relayerUrl:
// networkId === 'testnet'
// ? 'http://34.70.226.83:3030/relay'
// : 'https://near-relayer-mainnet.api.pagoda.co/relay',
// }) as any, // TODO: Refactor setupFastAuth() to TS
setupFastAuthWallet(),
setupKeypom({
trialAccountSpecs: {
url:
Expand Down
2 changes: 2 additions & 0 deletions src/lib/selector/fast-auth-icon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-disable import/no-anonymous-default-export */
export default `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAQAAADTdEb+AAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QA/4ePzL8AAAAJcEhZcwAACxMAAAsTAQCanBgAAAAHdElNRQfmCBYRIzjChBcFAAAabUlEQVR42u2deWBVxfX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ILuQrB2W1m15Wp46uM7i5aCGXa7dvgDmd6Q7Na33LdXYCasaL5CbsPJvMsq8lQ8SpTnfWGxZVLi/Zfy1XhU58nsCjfAEf0jG8BbqCc3jF2KueIj6nc6JvT+rSja9szG4d4lNup5Z2e4b8TGWu5HX2JiiqAr6im6mDHerSlVkWqvZuYQY3hKJyIVVpx0RW2Xzm2sEsusYnqqR4/giAKrSgA+05h4ZUL/cAzSIOsIWlzGcBazms3YYhUWnERVzB+ZxOLSrH/OtictnKMj7jU1ZTEJ+L+IVVQmUa0oJWnEUT6pFOVSCfHHawnlV8zxq2uXJnbsjx1KUpLWlJU06mNmnHPAsXk08OO9nAKpazmq3WNjb/PwtMam6w6l9KAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDIyLTA4LTIyVDE3OjM1OjQ4KzAwOjAwUOMNpQAAACV0RVh0ZGF0ZTptb2RpZnkAMjAyMi0wOC0yMlQxNzozNTo0OCswMDowMCG+tRkAAAAASUVORK5CYII=`
239 changes: 239 additions & 0 deletions src/lib/selector/fast-auth-wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
import type {
Account,
BrowserWallet,
Network,
Optional,
Transaction,
WalletBehaviourFactory,
WalletModuleFactory,
} from "@near-wallet-selector/core";
import { createAction } from "@near-wallet-selector/wallet-utils";
import * as nearAPI from "near-api-js";

import icon from "./fast-auth-icon";
import { FastAuthWalletConnection } from "./fastAuthWalletConnection";

export interface FastAuthWalletParams {
walletUrl?: string;
iconUrl?: string;
deprecated?: boolean;
successUrl?: string;
failureUrl?: string;
}

interface FastAuthWalletState {
wallet: nearAPI.WalletConnection;
keyStore: nearAPI.keyStores.BrowserLocalStorageKeyStore;
}

interface FastAuthWalletExtraOptions {
walletUrl: string;
}

const resolveWalletUrl = (network: Network, walletUrl?: string) => {
if (walletUrl) {
return walletUrl;
}

switch (network.networkId) {
case "mainnet":
return "http://localhost:3000";
case "testnet":
return "http://localhost:3000";
default:
throw new Error("Invalid wallet url");
}
};

const setupWalletState = async (
params: FastAuthWalletExtraOptions,
network: Network
): Promise<FastAuthWalletState> => {
const keyStore = new nearAPI.keyStores.BrowserLocalStorageKeyStore();

const near = await nearAPI.connect({
keyStore,
walletUrl: params.walletUrl,
...network,
headers: {},
});

const wallet = new FastAuthWalletConnection(near, "near_app");

return {
wallet,
keyStore,
};
};

const FastAuthWallet: WalletBehaviourFactory<
BrowserWallet,
{ params: FastAuthWalletExtraOptions }
> = async ({ metadata, options, store, params, logger }) => {
const _state = await setupWalletState(params, options.network);
const getAccounts = async (): Promise<Array<Account>> => {
const accountId = _state.wallet.getAccountId();
const account = _state.wallet.account();

if (!accountId || !account) {
return [];
}

const publicKey = await account.connection.signer.getPublicKey(
account.accountId,
options.network.networkId
);
return [
{
accountId,
publicKey: publicKey ? publicKey.toString() : "",
},
];
};

const transformTransactions = async (
transactions: Array<Optional<Transaction, "signerId">>
) => {
const account = _state.wallet.account();
const { networkId, signer, provider } = account.connection;

const localKey = await signer.getPublicKey(account.accountId, networkId);

return Promise.all(
transactions.map(async (transaction, index) => {
const actions = transaction.actions.map((action) =>
createAction(action)
);
const accessKey = await account.accessKeyForTransaction(
transaction.receiverId,
actions,
localKey
);

if (!accessKey) {
throw new Error(
`Failed to find matching key for transaction sent to ${transaction.receiverId}`
);
}

const block = await provider.block({ finality: "final" });

return nearAPI.transactions.createTransaction(
account.accountId,
nearAPI.utils.PublicKey.from(accessKey.public_key),
transaction.receiverId,
accessKey.access_key.nonce + index + 1,
actions,
nearAPI.utils.serialize.base_decode(block.header.hash)
);
})
);
};

return {
async signIn({ contractId, methodNames, successUrl, failureUrl }) {
const existingAccounts = await getAccounts();

if (existingAccounts.length) {
return existingAccounts;
}

await _state.wallet.requestSignIn({
contractId,
methodNames,
successUrl,
failureUrl,
});

return getAccounts();
},

async signOut() {
if (_state.wallet.isSignedIn()) {
_state.wallet.signOut();
}
},

async getAccounts() {
return getAccounts();
},

async verifyOwner() {
throw new Error(`Method not supported by ${metadata.name}`);
},

async signAndSendTransaction({
signerId,
receiverId,
actions,
callbackUrl,
}) {
logger.log("signAndSendTransaction", {
signerId,
receiverId,
actions,
callbackUrl,
});

const { contract } = store.getState();

if (!_state.wallet.isSignedIn() || !contract) {
throw new Error("Wallet not signed in");
}

const account = _state.wallet.account();

return account["signAndSendTransaction"]({
receiverId: receiverId || contract.contractId,
actions: actions.map((action) => createAction(action)),
walletCallbackUrl: callbackUrl,
});
},

async signAndSendTransactions({ transactions, callbackUrl }) {
logger.log("signAndSendTransactions", { transactions, callbackUrl });

if (!_state.wallet.isSignedIn()) {
throw new Error("Wallet not signed in");
}

return _state.wallet.requestSignTransactions({
transactions: await transformTransactions(transactions),
callbackUrl,
});
},
};
};

export function setupFastAuthWallet({
walletUrl,
iconUrl = icon,
deprecated = false,
successUrl = "",
failureUrl = "",
}: FastAuthWalletParams = {}): WalletModuleFactory<BrowserWallet> {
return async (moduleOptions) => {
return {
id: "fast-auth-wallet",
type: "browser",
metadata: {
name: "FastAuthWallet",
description: null,
iconUrl,
deprecated,
available: true,
successUrl,
failureUrl,
walletUrl: resolveWalletUrl(moduleOptions.options.network, walletUrl),
},
init: (options) => {
return FastAuthWallet({
...options,
params: {
walletUrl: resolveWalletUrl(options.options.network, walletUrl),
},
});
},
};
};
}
Loading

0 comments on commit 889537e

Please sign in to comment.