Skip to content
Open
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions docs/webhooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Webhook Signature Verification

Stellar Pay signs webhook payloads using HMAC-SHA256 and includes the result in the `x-stellarpay-signature` header.

## How it works

1. Payload is converted to a string
2. HMAC-SHA256 is generated using the merchant secret
3. Signature is sent in `x-stellarpay-signature` header

## Verification Example

```ts
import { verifyWebhookSignature } from '@stellar-pay/sdk-js';

const isValid = verifyWebhookSignature(rawBody, merchantSecret, signatureHeader);

if (!isValid) {
throw new Error('Invalid webhook signature');
}
```
1 change: 1 addition & 0 deletions packages/sdk-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"lint": "eslint \"src/**/*.ts\""
},
"devDependencies": {
"@types/node": "^22.10.0",
"tsup": "^8.0.0",
"typescript": "^5.0.0"
}
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-js/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './webhook-signature';
27 changes: 27 additions & 0 deletions packages/sdk-js/src/webhook-signature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createHmac, timingSafeEqual } from 'node:crypto';

/**
* Generate HMAC-SHA256 signature for webhook payload
*/
export function signWebhookPayload(payload: string, secret: string): string {
return createHmac('sha256', secret).update(payload, 'utf8').digest('hex');
}

/**
* Verify webhook signature securely
*/
export function verifyWebhookSignature(
payload: string,
secret: string,
signature: string,
): boolean {
if (!signature) return false;

const expected = signWebhookPayload(payload, secret);

if (expected.length !== signature.length) {
return false;
}

return timingSafeEqual(Buffer.from(expected, 'utf8'), Buffer.from(signature, 'utf8'));
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.