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 consent from users

Now that you've received the consent token from your backend, you can ask the user to provide consent by using thegetConsent 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 function takes the consent token as a parameter and a callback object.

sdk.getConsent(
    // This tokens comes from backend call.
    "consentToken",
    Callback(
        onSuccess = { response: JsonResponse ->
            Log.d("onSuccess", response.toString())
        },
        onFailure = { error: JsonResponse ->
            Log.d("onFailure", error.toString())
        },
    ),
)
sdk.getConsent(
  // This tokens comes from backend call.
  consentToken: "consentToken",
  callback: Callback(
    onSuccess: { (result: JsonResponse) in
      print("onSuccess")
      print(result.description)
    },
    onFailure: { (error: JsonResponse) in
      print("onFailure")
      print(error.description)
    }
  )
)
await sdk.getConsent(
  // This tokens comes from backend call.
  'consentToken',
);
await sdk.getConsent(
  // This tokens comes from backend call.
  'consentToken',
);

On successful consent from the user, the callback onSuccess or the Promise resolve function is called with the transaction data as a JsonResponse object. This is what the data looks like:

{
  "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 perform 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

Callback onFailure or the Promise reject function is called when the user cancels the operation or if there's an error. The error object contains a status field which is a string indicating the status of the operation. Here's what the error object looks like:

{
  "status": "USER_CONSENT_DENIED"
}

Here's a table of all possible error status returned by the SDK

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

© Copyright 2024, Passbird Research Inc.