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

Wallet Connect: Beta Feature Field #93

Merged
merged 2 commits into from
Aug 9, 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
69 changes: 61 additions & 8 deletions src/wallet-connect/handlers.ts → src/beta.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import {
Hex,
Signature,
TransactionSerializable,
fromHex,
hashMessage,
hashTypedData,
keccak256,
serializeSignature,
serializeTransaction,
} from "viem";
import { populateTx, toPayload } from "../utils/transaction";
import { RecoveryData } from "../types";
import { addSignature, populateTx, toPayload } from "./utils/transaction";
import { NearEthTxData, RecoveryData } from "./types";
import { NearEthAdapter } from "./chains/ethereum";
import { Web3WalletTypes } from "@walletconnect/web3wallet";

// Interface for Ethereum transaction parameters
export interface EthTransactionParams {
Expand All @@ -25,10 +29,10 @@ export type PersonalSignParams = [Hex, Hex];
/// Interface for eth_sign parameters (address and message)
export type EthSignParams = [Hex, Hex];

// Interface for complex structured parameters like EIP-712
/// Interface for complex structured parameters like EIP-712
export type TypedDataParams = [Hex, string];

type SessionRequestParams =
export type SessionRequestParams =
| EthTransactionParams[]
| PersonalSignParams
| EthSignParams
Expand Down Expand Up @@ -104,10 +108,7 @@ export async function wcRouter(
type: "eth_signTypedData",
data: {
address: sender,
types: typedData.types,
primaryType: typedData.primaryType,
message: typedData.message,
domain: typedData.domain,
...typedData,
},
},
};
Expand All @@ -119,3 +120,55 @@ export async function wcRouter(
function stripEip155Prefix(eip155Address: string): string {
return eip155Address.split(":").pop() ?? "";
}

/**
* Features currently underdevelopment that will be migrated into the adapter class once refined.
* These features are accessible through the adapter class as `adapter.beta.methodName(...)`
*/
export class Beta {
adapter: NearEthAdapter;

constructor(adapter: NearEthAdapter) {
this.adapter = adapter;
}

async handleSessionRequest(
request: Partial<Web3WalletTypes.SessionRequest>
): Promise<NearEthTxData> {
const {
chainId,
request: { method, params },
} = request.params!;
console.log(`Session Request of type ${method} for chainId ${chainId}`);
const { evmMessage, payload, recoveryData } = await wcRouter(
method,
chainId,
params
);
console.log("Parsed Request:", payload, recoveryData);
return {
nearPayload: this.adapter.mpcContract.encodeSignatureRequestTx({
path: this.adapter.derivationPath,
payload,
key_version: 0,
}),
evmMessage,
recoveryData,
};
}

async respondSessionRequest(
recoveryData: RecoveryData,
signature: Signature
): Promise<Hex> {
if (recoveryData.type === "eth_sendTransaction") {
const signedTx = addSignature({
transaction: recoveryData.data as Hex,
signature,
});
// Returns relayed transaction hash (without waiting for confirmation).
return this.adapter.relaySignedTransaction(signedTx, false);
}
return serializeSignature(signature);
}
}
57 changes: 6 additions & 51 deletions src/chains/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,28 @@ import {
TypedDataDefinition,
parseTransaction,
keccak256,
Signature,
} from "viem";
import {
BaseTx,
AdapterParams,
FunctionCallTransaction,
TxPayload,
TransactionWithSignature,
NearEthTxData,
SignArgs,
RecoveryData,
} from "../types";
import { MpcContract } from "../mpcContract";
import {
MpcContract,
Network,
buildTxPayload,
addSignature,
populateTx,
toPayload,
} from "../utils/transaction";
import { Network } from "../network";
import { Web3WalletTypes } from "@walletconnect/web3wallet";
import { wcRouter } from "../wallet-connect/handlers";
} from "..";
import { Beta } from "../beta";

export class NearEthAdapter {
readonly mpcContract: MpcContract;
readonly address: Address;
readonly derivationPath: string;
readonly beta: Beta;

private constructor(config: {
mpcContract: MpcContract;
Expand All @@ -47,6 +42,7 @@ export class NearEthAdapter {
this.mpcContract = config.mpcContract;
this.derivationPath = config.derivationPath;
this.address = config.sender;
this.beta = new Beta(this);
}

/**
Expand Down Expand Up @@ -244,45 +240,4 @@ export class NearEthAdapter {
});
return serializeSignature(signature);
}

/// Mintbase Wallet
async handleSessionRequest(
request: Partial<Web3WalletTypes.SessionRequest>
): Promise<NearEthTxData> {
const {
chainId,
request: { method, params },
} = request.params!;
console.log(`Session Request of type ${method} for chainId ${chainId}`);
const { evmMessage, payload, recoveryData } = await wcRouter(
method,
chainId,
params
);
console.log("Parsed Request:", payload, recoveryData);
return {
nearPayload: this.mpcContract.encodeSignatureRequestTx({
path: this.derivationPath,
payload,
key_version: 0,
}),
evmMessage,
recoveryData,
};
}

async wcRespond(
recoveryData: RecoveryData,
signature: Signature
): Promise<Hex> {
if (recoveryData.type === "eth_sendTransaction") {
const signedTx = addSignature({
transaction: recoveryData.data as Hex,
signature,
});
// Returns relayed transaction hash (without waiting for confirmation).
return this.relaySignedTransaction(signedTx, false);
}
return serializeSignature(signature);
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export * from "./types";
export * from "./utils/signature";
export * from "./network";
export * from "./utils/transaction";
/// Beta features
export * from "./beta";

/**
* Configuration for setting up the adapter.
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/ethereum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ describe("ethereum", () => {
expect(request.actions.length).toEqual(1);

expect(() =>
adapter.handleSessionRequest({
adapter.beta.handleSessionRequest({
params: {
chainId: "11155111",
request: { method: "poop", params: [] },
},
})
).rejects.toThrow("Unhandled session_request method: poop");

const ethSign = await adapter.handleSessionRequest({
const ethSign = await adapter.beta.handleSessionRequest({
params: {
chainId: "11155111",
request: {
Expand Down
Loading