Skip to content
This repository was archived by the owner on Aug 5, 2024. It is now read-only.

Commit 0e72b53

Browse files
committed
feat: add bodyHash to keypair auth docs
1 parent 2ee0581 commit 0e72b53

File tree

1 file changed

+36
-0
lines changed
  • src/app/engine/features/keypair-authentication

1 file changed

+36
-0
lines changed

src/app/engine/features/keypair-authentication/page.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,42 @@ await fetch(`${engineBaseUrl}/backend-wallet/get-all`, {
107107
});
108108
```
109109

110+
## Restrict the payload body (Advanced)
111+
112+
To ensure this access token can only execute a specific payload, provide a SHA256 hash of the payload body as the `bodyHash` argument of the signed object.
113+
114+
Example: This access token is restricted to transfer 0.1 MATIC on Polygon to 0xE68FFAE106cc68A0e36Ba9Fd86f27337E3a71da6.
115+
116+
```typescript
117+
import { createHash } from "crypto";
118+
import jsonwebtoken from "jsonwebtoken";
119+
120+
// Prepare the request payload body.
121+
const body = JSON.stringify({
122+
to: "0xE68FFAE106cc68A0e36Ba9Fd86f27337E3a71da6",
123+
currencyAddress: "0x0000000000000000000000000000000000000000",
124+
amount: "0.1",
125+
});
126+
127+
// Add a hash of `body` to the signed payload.
128+
const payload = {
129+
iss: publicKey,
130+
bodyHash: createHash("sha256").update(body).digest("hex"),
131+
};
132+
const accessToken = jsonwebtoken.sign(payload, privateKey, {
133+
algorithm: "ES256",
134+
});
135+
136+
// Call Engine with `body`.
137+
await fetch(`${engineBaseUrl}/backend-wallet/137/transfer`, {
138+
headers: {
139+
"Content-Type": "application/json",
140+
authorization: `Bearer ${accessToken}`,
141+
},
142+
body,
143+
});
144+
```
145+
110146
## FAQ
111147

112148
#### How do I enable this feature on my self-hosted Engine?

0 commit comments

Comments
 (0)