Skip to content
Open
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
42 changes: 42 additions & 0 deletions authentication/proxying-signed-requests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,45 @@ How should you decide what to do? Here are some considerations:
* A backend proxy can be useful if you need to inspect and persist activity results. For example: if your users are creating wallets, you might want to persist the addresses. If your users are signing transactions, you might want to broadcast on their behalf.
* Another reason why a backend server could be beneficial is monitoring, feature toggles, and validation: with a proxy you're able to control which requests are proxied and which aren't. You can also perform additional validation before signed requests are forwarded to Turnkey.
* POSTing signed requests directly from your app frontend to Turnkey saves you the burden of running a proxy server, and takes you out of the loop so that your end-users interact directly with Turnkey. This is a "hands-off" approach that can work well if you want to give your end-users maximum flexibility and ownership over their sub-organization.

Here's a practical example of how you could use [@turnkey/react-wallet-kit](https://docs.turnkey.com/sdks/react/advanced-api-requests#the-http-client) to stamp a `signRawPayload` request on the client side, and then send that signed request to Turnkey — typically through your backend before reaching Turnkey’s API:

```ts
import { useTurnkey } from '@turnkey/react-wallet-kit';
import axios from 'axios';

const { httpClient } = useTurnkey();

const signed = await httpClient.stampSignRawPayload({
organizationId: session.organizationId,
signWith: <wallet_account_address>,
payload: 'hello',
encoding: 'PAYLOAD_ENCODING_TEXT_UTF8',
hashFunction: 'HASH_FUNCTION_SHA256',
});

if (!signed) throw new Error('No signed request (missing stamper?)');

const { body, stamp, url } = signed;

const xStamp =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the reason for having this? Looking at the definition of stampSignRequest, the stamp is never a string and is instead:

export type TStamp = {
  stampHeaderName: string;
  stampHeaderValue: string;
};

so I think its safe to remove this check and just do:

const xStamp = stamp?.stampHeaderValue

typeof stamp === 'string' ? stamp : stamp?.stampHeaderValue;

if (!xStamp) throw new Error('Missing X-Stamp header value');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm to me we should:

const signed = await httpClient.stampSignRawPayload({
    organizationId: session.organizationId,
    signWith: <wallet_account_address>,
    payload: 'hello',
    encoding: 'PAYLOAD_ENCODING_TEXT_UTF8',
    hashFunction: 'HASH_FUNCTION_SHA256',
});

if (!signed) throw new Error('Missing signed request');

const xStamp = stamp.stampHeaderValue

// continue on here


// Pass body as-is
const { data } = await axios.post(url, body, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Stamp': xStamp,
},
});

// Extract r, s, v -> signature
const { r, s, v } = data?.activity?.result?.signRawPayloadResult ?? {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for this part IMO, we send off the stamped request and we are done from the client-side

const sig = r && s && v ? (`0x${r}${s}${v}` as `0x${string}`) : undefined;
setSignature(sig ?? '(no signature returned)');

console.log('Signature:', sig);
```