Calling signTypedData
Once you have built an ethereum typed data, you can ask the user to sign it by using signTypedData
function in MetaKeep SDK. The function expects a non-empty reason
which is shown to the user at the time of typed data signing.
This API is compliant with the latest EIP-712 spec and is a drop in replacement for Metamask's signTypedData_v4 API.
await sdk.signTypedData(
// Typed data
{
types: {
EIP712Domain: [{
name: "name",
type: "string"
},
{
name: "version",
type: "string"
},
{
name: "chainId",
type: "uint256"
},
{
name: "verifyingContract",
type: "address"
},
],
MetaTransaction: [{
name: "nonce",
type: "uint256",
},
{
name: "from",
type: "address",
},
{
name: "functionSignature",
type: "bytes",
},
],
},
primaryType: "MetaTransaction",
domain: {
name: "Ether Mail",
version: "1",
chainId: 1,
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
},
message: {
nonce: 200,
from: "0x97706Df14A769E28EC897dAc5Ba7bCfa5AA9C444",
functionSignature: "0xd1a1beb40000",
},
},
// signing reason
"reason",
);
Here's a reference implementation of this code: https://jsfiddle.net/passbird/j8egh6fr/
Response
signTypedData
returns a promise which succeeds when the sign operation is successful. The response looks like this-
{
status: "SUCCESS",
// Signature of the signed typed data. The signature encodes the r, s and v parameters
// from appendix F of the yellow paper in big-endian format.
// Bytes 0…32 contain the r parameter, bytes 32…64 the s parameter and the last byte the v parameter.
signature: "0x1a84678f385553358386051464a252fa25d019335deb20262eb231d4ec1467300e6f1944e872430b7e04af6dd68ece90e2a9995a7abd827b50588602523b525600",
r: "0x1a84678f385553358386051464a252fa25d019335deb20262eb231d4ec146730",
s: "0x0e6f1944e872430b7e04af6dd68ece90e2a9995a7abd827b50588602523b5256",
v: "0x0"
}
Error status
signTypedData
returns a promise which throws an error when the sign operation is unsuccessful. The error object looks like so-
{
status: "USER_CONSENT_DENIED"
}
Here's a table of all possible error status returned by the SDK
Status | Description |
---|---|
USER_REQUEST_DENIED | User has denied the sign operation. In this case, you should ask the user if they want to try again. |
INVALID_REQUEST | Invalid data passed to sign operation. Please get in touch with us if you are unable to fix this. |
APP_ID_REQUIRED | No app id provided when initializing the SDK. You can find the app id in MetaKeep Developer Console. |
APP_NOT_FOUND | Provided app id is invalid. You can find the correct app id in MetaKeep Developer Console. |
SOMETHING_WENT_WRONG | Unknown error happened. Please get in touch with us if you continue seeing this error. |
© Copyright 2024, Passbird Research Inc.