Skip to content

Commit

Permalink
[SDK] Expose sdk.wallet.signTypedData() (#468)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquim-verges committed Dec 16, 2022
1 parent 73883f5 commit f7a74a3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-starfishes-type.md
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/sdk": patch
---

Expose `sdk.wallet.signTypedData()`
5 changes: 1 addition & 4 deletions packages/sdk/src/evm/common/sign.ts
@@ -1,7 +1,6 @@
// couldn't find this in barbones ethers export, but "type" should mean it does not increase bundle size either way
import type { TypedDataField } from "@ethersproject/abstract-signer";
import { ethers, Signer, providers } from "ethers";
import { _TypedDataEncoder } from "ethers/lib/utils";

/**
* @internal
Expand Down Expand Up @@ -76,9 +75,7 @@ export async function signTypedDataInternal(
try {
await provider.send("eth_signTypedData_v4", [
signerAddress,
JSON.stringify(
_TypedDataEncoder.getPayload(domain, types, message),
),
JSON.stringify(payload),
]);
} catch (finalErr: any) {
throw finalErr;
Expand Down
39 changes: 38 additions & 1 deletion packages/sdk/src/evm/core/wallet/UserWallet.ts
Expand Up @@ -3,6 +3,7 @@ import {
isNativeToken,
normalizePriceValue,
} from "../../common/currency";
import { EIP712Domain, signTypedDataInternal } from "../../common/sign";
import { NATIVE_TOKEN_ADDRESS } from "../../constants";
import { SDKOptions } from "../../schema";
import { Amount, CurrencyValue } from "../../types";
Expand All @@ -11,7 +12,7 @@ import { RPCConnectionHandler } from "../classes/rpc-connection-handler";
import { NetworkOrSignerOrProvider, TransactionResult } from "../types";
import type { IERC20 } from "@thirdweb-dev/contracts-js";
import ERC20Abi from "@thirdweb-dev/contracts-js/dist/abis/IERC20.json";
import { ethers, BigNumber, providers, Signer } from "ethers";
import { ethers, BigNumber, providers, Signer, TypedDataField } from "ethers";
import EventEmitter from "eventemitter3";
import invariant from "tiny-invariant";

Expand Down Expand Up @@ -183,6 +184,42 @@ export class UserWallet {
return await signer.signMessage(message);
}

/**
* Sign a typed data structure (EIP712) with the connected wallet private key
* @param domain - the domain as EIP712 standard
* @param types - the strcuture and data types as defined by the EIP712 standard
* @param message - the data to sign
* @returns the payload and its associated signature
*
* @example
* ```javascript
* // This is the message to be signed
* // Now we can sign the message with the connected wallet
* const { payload, signature } = await sdk.wallet.signTypedData(
* {
name: "MyEIP721Domain",
version: "1",
chainId: 1,
verifyingContract: "0x...",
},
{ MyStruct: [ { name: "to", type: "address" }, { name: "quantity", type: "uint256" } ] },
{ to: "0x...", quantity: 1 },
* );
* ```
*/
public async signTypedData(
domain: EIP712Domain,
types: Record<string, Array<TypedDataField>>,
message: Record<string, any>,
): Promise<{ payload: any; signature: string }> {
return await signTypedDataInternal(
this.requireWallet(),
domain,
types,
message,
);
}

/**
* Recover the signing address from a signed message
* @param message - the original message that was signed
Expand Down
21 changes: 21 additions & 0 deletions packages/sdk/test/evm/wallet.test.ts
Expand Up @@ -56,4 +56,25 @@ describe("Wallet", async () => {
const address = sdk.wallet.recoverAddress(message, signature);
expect(address).to.eq(adminWallet.address);
});

it("should sign typed data and recover address", async () => {
sdk.updateSignerOrProvider(adminWallet);
const { payload, signature } = await sdk.wallet.signTypedData(
{
name: "MyEIP721Domain",
version: "1",
chainId: 1,
verifyingContract: adminWallet.address,
},
{
MyStruct: [
{ name: "to", type: "address" },
{ name: "quantity", type: "uint256" },
],
},
{ to: adminWallet.address, quantity: 1 },
);
expect(payload !== undefined).to.eq(true);
expect(signature !== undefined).to.eq(true);
});
});

0 comments on commit f7a74a3

Please sign in to comment.