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

Commit

Permalink
feat: add bodyHash to keypair auth docs
Browse files Browse the repository at this point in the history
  • Loading branch information
arcoraven committed Jun 7, 2024
1 parent 2ee0581 commit 0e72b53
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/app/engine/features/keypair-authentication/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,42 @@ await fetch(`${engineBaseUrl}/backend-wallet/get-all`, {
});
```

## Restrict the payload body (Advanced)

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.

Example: This access token is restricted to transfer 0.1 MATIC on Polygon to 0xE68FFAE106cc68A0e36Ba9Fd86f27337E3a71da6.

```typescript
import { createHash } from "crypto";
import jsonwebtoken from "jsonwebtoken";

// Prepare the request payload body.
const body = JSON.stringify({
to: "0xE68FFAE106cc68A0e36Ba9Fd86f27337E3a71da6",
currencyAddress: "0x0000000000000000000000000000000000000000",
amount: "0.1",
});

// Add a hash of `body` to the signed payload.
const payload = {
iss: publicKey,
bodyHash: createHash("sha256").update(body).digest("hex"),
};
const accessToken = jsonwebtoken.sign(payload, privateKey, {
algorithm: "ES256",
});

// Call Engine with `body`.
await fetch(`${engineBaseUrl}/backend-wallet/137/transfer`, {
headers: {
"Content-Type": "application/json",
authorization: `Bearer ${accessToken}`,
},
body,
});
```

## FAQ

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

0 comments on commit 0e72b53

Please sign in to comment.