Get Consent

Getting the ConsentRequestToken

NFT Transfer, Coin Transfer & Invoke Lambda requests initiated by developers on behalf of the users result in a consentToken.

{
  "status": "USER_CONSENT_NEEDED",
  "consentToken": "CrgBAQIDAHgtrXLM76dJyt4f1olcBlk7ncpU7dew9F1vRFXlKBw-fJA-TfATHhYppUrIuoEMsncIpxm4af4uUh4Cikr2YkEzasNQr9433Z-V73Hw=="
}

Getting the consent from users

Now that you've received the consent token from your backend, you can ask the user to provide consent by using getConsent function in MetaKeep SDK.

🚧

Consent token validity

Note that the consent token once issued, is valid only for 10 minutes. SDK will throw an error if you try to use the consent token outside the expiration window.

getConsent returns a promise which succeeds when the consent operation is successful. You can handle this with a wrapper like so-

/* 
	 Call getConsent. It returns a promise which resolves into a transaction data when the operation succeeds.
   Otherwise throws an error.
*/
async function getConsentFromMetakeepSDK(consentToken) {
  try {
    const consentResponse = await sdk.getConsent(consentToken);
    console.log("getConsent successful");
    console.log(consentResponse);
  } catch (err) {
    console.log("Error when trying to get consent");
    console.log(err);
  }
}

Now, call the above function with the consentToken from the backend transfer function.

getConsentFromMetakeepSDK("CrgBAQIDAHgtrXLM76dJyt4f1olcBlk7ncpU7dew9F1vRFXlKBw-fJA-TfATHhYppUrIuoEMsncIpxm4af4uUh4Cikr2YkEzasNQr9433Z-V73Hw==");

Here's a reference implementation of this code: https://jsfiddle.net/passbird/5gxn4y8h/

On successful consent from user, await sdk.getConsent(consentToken) results in a successful transaction response.

{
  status: "QUEUED",
  transactionChainScanUrl: "https://mumbai.polygonscan.com/tx/0x66bb55bfe89b071c9ae526fa451ff58d3bcc688e7b0618fbac2080cc2ba1437d",
  transactionHash: "0x66bb55bfe89b071c9ae526fa451ff58d3bcc688e7b0618fbac2080cc2ba1437d",
  transactionId: "695a704c-d01b-4e02-978a-1767b31dde46"
}
{
  "status": "QUEUED",
  "transactionId": "e1e3af7e-de1c-48b7-9f3a-35bfcc13bbed",
  "transactionSignature": "2J4qDKhuwtUBYQC371xgp5CE3kQv7JqZGR3YyvGQf7BUUYWdapnj1cfUTTnvbpY3KcA5Y63bQ9eWeMvpC6UpHPJ9",
  "transactionChainScanUrl": "https://explorer.solana.com/tx/2J4qDKhuwtUBYQC371xgp5CE3kQv7JqZGR3YyvGQf7BUUYWdapnj1cfUTTnvbpY3KcA5Y63bQ9eWeMvpC6UpHPJ9?cluster=devnet"
}
{
  "status": "QUEUED",
  "transactions": [
    {
      "status": "QUEUED",
      "transactionId": "38cae336-9bc2-4b69-9141-3c85edf26f28",
      "transactionSignature": "3MRukZUmWhn6kKisa49fQXLu8sZWR5FuyoPczmmPgvaxwDZttxgrHheWADN2HH6vo8wZnCTDc2X1BmWwtzVMvsrS",
      "transactionChainScanUrl": "https://explorer.solana.com/tx/3MRukZUmWhn6kKisa49fQXLu8sZWR5FuyoPczmmPgvaxwDZttxgrHheWADN2HH6vo8wZnCTDc2X1BmWwtzVMvsrS?cluster=devnet"
    },
    {
      "status": "QUEUED",
      "transactionId": "94be5e32-744e-458f-b8f5-d6882360816c",
      "transactionSignature": "3tCRSonjNK6DyKWGb6sdc9i29j8VU6GMFndAtNF4xFh6kVDFihsoWvhSP9xNMP3KwWWk4Lug5zAtHv9LgewMjBdk",
      "transactionChainScanUrl": "https://explorer.solana.com/tx/3tCRSonjNK6DyKWGb6sdc9i29j8VU6GMFndAtNF4xFh6kVDFihsoWvhSP9xNMP3KwWWk4Lug5zAtHv9LgewMjBdk?cluster=devnet"
    },
    {
      "status": "QUEUED",
      "transactionId": "df2294c0-24af-4cc1-afa3-0f2c89ec8b85",
      "transactionSignature": "2S3RaKieYzg7abV85nL4CJNWtQpYAg4PJ4yVGpQP1u6wXq5uy4H9roiH2pF1MPPpQB2JeqoANjsFnLtM7wiUtr2h",
      "transactionChainScanUrl": "https://explorer.solana.com/tx/2S3RaKieYzg7abV85nL4CJNWtQpYAg4PJ4yVGpQP1u6wXq5uy4H9roiH2pF1MPPpQB2JeqoANjsFnLtM7wiUtr2h?cluster=devnet"
    }
  ]
}

You can now use the transactionId in the Transaction Status endpoint in the backend. MetaKeep invisibly ensures and applies several strategies to ensure the transactions are accepted and executed on the blockchain at the lowest gas possible.

Idempotency Key

MetaKeep's infrastructure allows you to safely retry getConsent with the same idempotency key provided during the backend API call. This ensures that you don't accidentally performing the same operation twice. For e.g., you can generate 2 consent tokens for Coin Transfer operation using the same idempotency key and then you can call getConsent twice using 2 different consent tokens. The coin transfer will only happen once.

Error status

getConsent returns a promise which throws an error when the consent 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

StatusDescription
USER_CONSENT_DENIEDUser has denied the consent request.
In this case, you should ask the user if they want to try again.
EXPIRED_TOKENConsent token has expired.
You should get a new consent token from your backend.
INVALID_CONSENT_TOKENConsent token is invalid.
Make sure you get the consent token from the correct backend APIs.
APP_NOT_FOUNDProvided app id is invalid.
You can find the correct app id in the MetaKeep Developer Console.
ISSUER_DOES_NOT_MATCHYou are trying to use consent token from dublin environment in prod environment.
SOMETHING_WENT_WRONGUnknown error happened.
Please get in touch with us if you continue seeing this error.

© Copyright 2024, Passbird Research Inc.