Skip to content

Commit

Permalink
streams sign verification fix πŸ§‘β€πŸ”§
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnVersus committed Jul 26, 2023
1 parent 82ba205 commit 114d12f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/02-streams-api/evm/response-body.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The body contains the data you are interested in. Logs is in array containing ra

## How to verify the signature for the received webhook request

In JavaScript or python, you can use this function, for other programming languages you can adapt this code. The secret is the web3api key for your account.
In JavaScript or python, you can use this function, for other programming languages you can adapt this code. The secret key is the streams secret which you can find in [setting](https://admin.moralis.io/settings) page.

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
Expand Down Expand Up @@ -37,7 +37,7 @@ def verify_Signature(req, secret):
raise TypeError("Signature not provided")

data = req.data+secret.encode()
signature = Web3.sha3(data).hex()
signature = Web3.keccak(text=data.decode()).hex()

if provided_signature != signature:
raise ValueError("Invalid Signature")
Expand Down
29 changes: 27 additions & 2 deletions docs/02-streams-api/evm/webhook-security.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ The signature is sent in the request headers in `headers["x-signature"]` field,

## How to verify the signature for the received webhook request

In JavaScript you can use this function, for other programming languages you can adapt this code. The secret is the web3api key for your account.
In JavaScript or python, you can use this function, for other programming languages you can adapt this code. The secret key is the streams secret which you can find in [setting](https://admin.moralis.io/settings) page.

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

<Tabs groupId="programming-language">
<TabItem value="javascript" label="index.js (JavaScript)" default>

```javascript
const verifySignature = (req, secret) => {
Expand All @@ -26,4 +32,23 @@ const verifySignature = (req, secret) => {
if(generatedSignature !== providedSignature) throw new Error("Invalid Signature")

}
```
```

</TabItem>
<TabItem value="python" label="index.py (Python)">

```python Python
def verify_Signature(req, secret):
provided_signature = req.headers.get("x-signature")
if not provided_signature:
raise TypeError("Signature not provided")

data = req.data+secret.encode()
signature = Web3.keccak(text=data.decode()).hex()

if provided_signature != signature:
raise ValueError("Invalid Signature")
```

</TabItem>
</Tabs>

1 comment on commit 114d12f

@vercel
Copy link

@vercel vercel bot commented on 114d12f Jul 26, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.