-
Notifications
You must be signed in to change notification settings - Fork 11
Stamp and separately send a stamped request #465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
t-vila
wants to merge
1
commit into
main
Choose a base branch
from
traian/stamp-to-proxy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = | ||
| typeof stamp === 'string' ? stamp : stamp?.stampHeaderValue; | ||
|
|
||
| if (!xStamp) throw new Error('Missing X-Stamp header value'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm to me we should: |
||
|
|
||
| // 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 ?? {}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:so I think its safe to remove this check and just do: