Skip to content

Commit

Permalink
Merge pull request #34 from near/add-evm-function-call
Browse files Browse the repository at this point in the history
Add evm function call
  • Loading branch information
Pessina authored Sep 12, 2024
2 parents ac49e3b + a315292 commit 7fe6844
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 99 deletions.
34 changes: 28 additions & 6 deletions package-lock.json

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

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "near-fastauth-wallet",
"version": "2.0.1",
"version": "2.0.2-beta.10",
"license": "MIT",
"scripts": {
"build": "nx build --buildLibsFromSource --skip-nx-cache",
"deploy": "cd dist/near-fastauth-wallet && npm publish",
"deploy": "npm run build && cd dist/near-fastauth-wallet && npm publish",
"deploy:beta": "npm run build && cd dist/near-fastauth-wallet && npm publish --tag beta",
"test": "nx test",
"watch": "nodemon"
},
Expand All @@ -17,7 +18,7 @@
"antd": "^5.12.8",
"class-variance-authority": "^0.7.0",
"js-sha256": "^0.11.0",
"multichain-tools": "^1.0.8",
"multichain-tools": "2.0.14-beta.10",
"near-api-js": "^2.1.4",
"tslib": "^2.3.0",
"usehooks-ts": "^2.9.2"
Expand Down Expand Up @@ -55,4 +56,4 @@
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts"
}
}
48 changes: 24 additions & 24 deletions src/lib/fastAuthWalletConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type {
Near,
WalletConnection,
} from 'near-api-js';
import * as nearAPI from 'near-api-js';
import { KeyPair, utils } from 'near-api-js';
import { ConnectedWalletAccount } from 'near-api-js';
import { deserialize, serialize } from 'near-api-js/lib/utils/serialize';
Expand All @@ -18,6 +17,12 @@ import { loadIframeDialog } from '../ui/reactApp';
import { SignedMessage, SignMessageParams } from '@near-wallet-selector/core';
import { PublicKey } from 'near-api-js/lib/utils';
import { sha256 } from 'js-sha256';
import {
BitcoinRequest,
BTCNetworkIds,
EVMRequest,
KeyDerivationPath,
} from 'multichain-tools';

const LOGIN_PATH = '/login/';
const CREATE_ACCOUNT_PATH = '/create-account/';
Expand Down Expand Up @@ -58,29 +63,24 @@ interface RequestSignTransactionsOptions {
meta?: string;
}

interface BaseSendMultichainMessage {
chain: number;
domain?: string;
to: string;
value: bigint;
meta?: { [k: string]: any };
from: string;
}

type EvmSendMultichainMessage = BaseSendMultichainMessage & {
chainId: bigint;
maxFeePerGas?: bigint;
maxPriorityFeePerGas?: bigint;
gasLimit?: number;
};

type BTCSendMultichainMessage = BaseSendMultichainMessage & {
network: 'mainnet' | 'testnet';
};

type SendMultichainMessage =
| BTCSendMultichainMessage
| EvmSendMultichainMessage;
// This type should be kept in sync with fast-auth-signer receiving message: https://github.com/near/fast-auth-signer/blob/8d2726f5fe007d7ae011dec245692ac7499aff0c/packages/near-fast-auth-signer/src/components/SignMultichain/types.ts#L1
export type SendMultichainMessage =
| (Omit<
EVMRequest,
'nearAuthentication' | 'chainConfig' | 'derivationPath'
> & {
chainConfig?: Partial<EVMRequest['chainConfig']>;
derivationPath: Omit<KeyDerivationPath, 'chain'> & { chain: 60 };
})
| (Omit<
BitcoinRequest,
'nearAuthentication' | 'chainConfig' | 'derivationPath'
> & {
chainConfig: {
network: BTCNetworkIds;
} & Partial<Omit<BitcoinRequest['chainConfig'], 'network'>>;
derivationPath: Omit<KeyDerivationPath, 'chain'> & { chain: 0 };
});

export class FastAuthWalletConnection {
/** @hidden */
Expand Down
145 changes: 80 additions & 65 deletions src/lib/near-fastauth-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ import * as nearAPI from 'near-api-js';

import BN from 'bn.js';
import {
NearNetworkIds,
ChainSignatureContracts,
BTCNetworkIds,
fetchDerivedBTCAddress,
type FetchEVMAddressRequest,
type BitcoinPublicKeyAndAddressRequest,
fetchDerivedEVMAddress,
fetchDerivedBTCAddressAndPublicKey,
} from 'multichain-tools';

import icon from './fast-auth-icon';
import { FastAuthWalletConnection } from './fastAuthWalletConnection';
import {
FastAuthWalletConnection,
type SendMultichainMessage,
} from './fastAuthWalletConnection';

export interface FastAuthWalletParams {
walletUrl?: string;
Expand All @@ -46,21 +48,53 @@ interface FastAuthWalletState {
near: any;
}

interface DerivedAddressParamEVM {
type: 'EVM';
signerId: string;
path: string;
networkId: NearNetworkIds;
contract: ChainSignatureContracts;
// Should be maintained until these methods are added through NEP to the wallet interface
interface FastAuthSignInParams {
email?: string;
accountId?: string;
isRecovery?: boolean;
}

interface DerivedAddressParamBTC {
type: 'BTC';
signerId: string;
path: string;
networkId: NearNetworkIds;
btcNetworkId: BTCNetworkIds;
contract: ChainSignatureContracts;
type ExtendedSignIn = (
params: Parameters<BrowserWallet['signIn']>[0] & FastAuthSignInParams
) => ReturnType<BrowserWallet['signIn']>;
interface FastAuthBrowserWallet extends BrowserWallet {
signIn: ExtendedSignIn;

signAndSendDelegateAction(params: {
receiverId: string;
actions: Action[];
}): Promise<void>;

signAndSendDelegateActions(params: {
transactions: Optional<Transaction, 'signerId'>[];
callbackUrl?: string;
}): Promise<void>;

setRelayerUrl(params: { relayerUrl: string }): void;

resetRelayerUrl(): void;

verifySignMessage(
message: SignMessageParams,
signedMessage: SignedMessage
): Promise<boolean>;

getDerivedAddress(
args:
| (Omit<FetchEVMAddressRequest, 'path'> & {
path: Omit<FetchEVMAddressRequest['path'], 'chain'> & {
chain: 60;
};
})
| (Omit<BitcoinPublicKeyAndAddressRequest, 'path'> & {
path: Omit<BitcoinPublicKeyAndAddressRequest['path'], 'chain'> & {
chain: 0;
};
})
): Promise<string>;

signMultiChainTransaction(data: SendMultichainMessage): Promise<void>;
}

const resolveWalletUrl = (network: Network, walletUrl?: string) => {
Expand Down Expand Up @@ -100,8 +134,8 @@ const setupWalletState = async (
};
};

const FastAuthWallet: WalletBehaviourFactory<
BrowserWallet,
export const FastAuthWallet: WalletBehaviourFactory<
FastAuthBrowserWallet,
{ params: FastAuthWalletExtraOptions }
> = async ({ metadata, options, store, params, logger }) => {
const _state = await setupWalletState(params, options.network);
Expand Down Expand Up @@ -297,7 +331,7 @@ const FastAuthWallet: WalletBehaviourFactory<
email,
accountId,
isRecovery,
}: any) {
}) {
const existingAccounts = await getAccounts();

if (existingAccounts.length) {
Expand Down Expand Up @@ -345,70 +379,32 @@ const FastAuthWallet: WalletBehaviourFactory<
});
},

async signAndSendDelegateAction({
receiverId,
actions,
}: {
receiverId: string;
actions: Action[];
}): Promise<void> {
async signAndSendDelegateAction({ receiverId, actions }) {
await _signAndSendWithRelayer({
transactions: [{ receiverId, actions }],
});
},

async signAndSendDelegateActions({
transactions,
callbackUrl,
}: {
transactions: Optional<Transaction, 'signerId'>[];
callbackUrl?: string;
}): Promise<void> {
async signAndSendDelegateActions({ transactions, callbackUrl }) {
await _signAndSendWithRelayer({
transactions,
callbackUrl,
});
},

async getDerivedAddress(
args: DerivedAddressParamEVM | DerivedAddressParamBTC
) {
if (args.type === 'EVM') {
return await fetchDerivedEVMAddress(
args.signerId,
args.path,
args.networkId,
args.contract
);
} else if (args.type === 'BTC') {
const address = await fetchDerivedBTCAddress(
args.signerId,
args.path,
args.networkId,
args.btcNetworkId,
args.contract
);
return address;
} else {
throw new Error('Unsupported chain type');
}
},
async signMultiChainTransaction(data) {
await _state.wallet.requestSignMultiChain(data);
},
setRelayerUrl({ relayerUrl: relayerUrlArg }) {
relayerUrl = relayerUrlArg;
},

resetRelayerUrl() {
relayerUrl = params.relayerUrl;
},

async signMessage(data) {
return _state.wallet.requestSignMessage(data);
},
async verifySignMessage(
message: SignMessageParams,
signedMessage: SignedMessage
): Promise<boolean> {

async verifySignMessage(message, signedMessage) {
const accessKeys = await _state.wallet.account().getAccessKeys();
const isFullAccessKey = accessKeys.some(
(key) =>
Expand All @@ -422,6 +418,25 @@ const FastAuthWallet: WalletBehaviourFactory<

return _state.wallet.verifySignMessage(message, signedMessage);
},

async getDerivedAddress(args) {
if (args.path.chain === 60) {
return await fetchDerivedEVMAddress(args);
} else if (args.path.chain === 0) {
return (
await fetchDerivedBTCAddressAndPublicKey(
// TypeScript can't infer this from the if clause above, so we need to provide a type assertion
args as BitcoinPublicKeyAndAddressRequest
)
).address;
} else {
throw new Error('Unsupported chain type');
}
},

async signMultiChainTransaction(data) {
await _state.wallet.requestSignMultiChain(data);
},
};
};

Expand Down

0 comments on commit 7fe6844

Please sign in to comment.