Skip to content

電子署名処理の調整 #28

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

Merged
merged 2 commits into from
Dec 3, 2021
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
60 changes: 30 additions & 30 deletions package-lock.json

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

78 changes: 68 additions & 10 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const signerMethods = [
"eth_requestAccounts",
"eth_accounts",
"eth_chainId",
"personal_sign",
"eth_sign",
"eth_signTypedData",
"eth_signTypedData_v4",
Expand Down Expand Up @@ -131,13 +132,33 @@ export class UnWalletProvider implements Eip1193Provider {
resolve(this.accounts?.chainId as any);
return;

case "personal_sign":
try {
if (!this.isConnected()) {
await this.connect();
}
const params = this.parsePersonalSignParams(args.params);
const sig = await this.ethSign({
account: params[1],
message: params[0],
});
resolve(sig as any);
} catch (e) {
reject(e);
}
return;

case "eth_sign":
try {
if (!this.isConnected()) {
await this.connect();
}
const params = this.parseEthSignParams(args.params);
resolve((await this.ethSign(params[1])) as any);
const sig = await this.ethSign({
account: params[0],
message: params[1],
});
resolve(sig as any);
} catch (e) {
reject(e);
}
Expand All @@ -149,7 +170,11 @@ export class UnWalletProvider implements Eip1193Provider {
await this.connect();
}
const params = this.parseEthSignTypedDataParams(args.params);
resolve((await this.ethSignTypedData(params[1])) as any);
const sig = await this.ethSignTypedData({
account: params[0],
data: params[1],
});
resolve(sig as any);
} catch (e) {
reject(e);
}
Expand All @@ -161,7 +186,11 @@ export class UnWalletProvider implements Eip1193Provider {
await this.connect();
}
const params = this.parseEthSignTypedDataV4Params(args.params);
resolve((await this.ethSignTypedDataV4(params[1])) as any);
const sig = await this.ethSignTypedDataV4({
account: params[0],
data: params[1],
});
resolve(sig as any);
} catch (e) {
reject(e);
}
Expand All @@ -180,7 +209,8 @@ export class UnWalletProvider implements Eip1193Provider {
await this.connect();
}
const params = this.parseEthSendTransactionParams(args.params);
resolve((await this.ethSendTransaction(params[0])) as any);
const txHash = await this.ethSendTransaction(params[0]);
resolve(txHash as any);
} catch (e) {
reject(e);
}
Expand Down Expand Up @@ -266,32 +296,41 @@ export class UnWalletProvider implements Eip1193Provider {
});
}

private ethSign(message: string): Promise<string> {
private ethSign(args: { account: string; message: string }): Promise<string> {
return new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
this.openSignerWindow("/x/eth/sign", {
message: message,
account: args.account,
message: args.message,
});
});
}

private ethSignTypedData(data: Eip712TypedData): Promise<string> {
private ethSignTypedData(args: {
account: string;
data: Eip712TypedData;
}): Promise<string> {
return new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
this.openSignerWindow("/x/eth/signTypedData", {
data: JSON.stringify(data),
account: args.account,
data: JSON.stringify(args.data),
});
});
}

private ethSignTypedDataV4(data: string): Promise<string> {
private ethSignTypedDataV4(args: {
account: string;
data: string;
}): Promise<string> {
return new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
this.openSignerWindow("/x/eth/signTypedData", {
data: data,
account: args.account,
data: args.data,
});
});
}
Expand Down Expand Up @@ -374,6 +413,25 @@ export class UnWalletProvider implements Eip1193Provider {
this.eventEmitter.removeListener(eventType, listener);
}

private parsePersonalSignParams(
params?: object | readonly unknown[]
): [string, string] {
if (params === undefined) {
throw new Error("params undefined");
}
if (!Array.isArray(params) || params.length !== 2) {
throw new Error("invalid params");
}
if (!ethers.utils.isHexString(params[0])) {
throw new Error("invalid message");
}
if (!ethers.utils.isAddress(params[1])) {
throw new Error("invalid account");
}

return [params[0], params[1]];
}

private parseEthSignParams(
params?: object | readonly unknown[]
): [string, string] {
Expand Down