本文档介绍了如何使用OpenZeppelin Defender的通知通道功能,通过邮件、Webhook以及第三方服务(如Slack、Telegram、Discord、Datadog、PagerDuty、Opsgenie)接收来自Defender模块的事件通知。同时,还介绍了如何设置Webhook Secrets以增强安全性,并提供了使用Defender SDK和Python进行签名验证的示例。
使用通知通道来获取关于不同 Defender 模块事件的通知,例如监控触发器、工作流或 Relayer 交易生命周期事件。
Email: 接收来自 Defender 可信地址的邮件:noreply@defender.openzeppelin.com
Webhooks: 配置你自己的 endpoints 来接收签名通知。
进入 Settings → Notification Channels 部分,选择并配置你喜欢的通道。
通知通道可以链接到任何 Defender 模块,以获取关于事件的通知。
此外,还可以自定义通知模板,查看方法。
作为额外的安全措施,Defender 实施了一种基于哈希的消息验证码(HMAC),通过在发送到 webhook endpoints 的通知中添加 Defender-Signature
和 Defender-Timestamp
请求 headers。因此,接收通知的 endpoint 可以验证请求的真实性。
每个 webhook 通知都有一个关联的 secret key,可以在 Settings → Notification Channnels → Webhook details 下访问。
Defender-Signature
使用 SHA256 algorithm 和 webhook secret
对 payload 和时间戳进行签名生成。
只有账户中的管理员用户才有权查看 webhook 密钥。 |
签名的真实性可以使用 Defender SDK 中的 verifySignature
实用函数进行验证。
function webhookHandler(req, res) {
const signature = req.headers['Defender-Signature'];
const timestamp = req.headers['Defender-Timestamp'];
const defender = new Defender({
apiKey: process.env.API_KEY,
apiSecret: process.env.API_SECRET,
});
const result = client.notificationChannel.verifySignature({
body: req.body,
signature,
timestamp,
secret: process.env.WEBHOOK_SECRET,
validityInMs: 1000 * 60 * 10, // 10 mins
});
if (!result.valid) throw new Error(result.error);
// your handler code
}
该签名是使用带有 SHA256
algorithm 的 HMAC 生成的,因此可以使用正确的 Webhook Secret
在任何编程语言中进行验证。
此代码示例已在 Python 3.12 中测试。 对于不同的版本,代码可能略有不同。 |
from datetime import datetime, timedelta, UTC
import hmac
import hashlib
import json
def verify_signature(body_object: dict, timestamp: str, signature: str, secret: str) -> bool:
# Parse the timestamp
try:
timestamp_dt = datetime.fromisoformat(timestamp)
except ValueError:
return False # Invalid timestamp format
# Get the current time and calculate the time difference
current_time = datetime.now(UTC)
time_difference = current_time - timestamp_dt
# Check if the time difference is within the allowed range (10 minutes)
if time_difference > timedelta(minutes=10):
return False
# Merge timestamp with body_object
payload_to_verify = {**body_object, 'timestamp': timestamp}
payload_to_verify_str = json.dumps(payload_to_verify, separators=(',', ':'))
# Create a new HMAC object using the secret and the SHA256 hash algorithm
hmac_obj = hmac.new(secret.encode(), payload_to_verify_str.encode(), hashlib.sha256)
# Generate signature
generated_signature = hmac_obj.hexdigest()
# Compare the generated signature with the provided signature
return hmac.compare_digest(generated_signature, signature)
- 原文链接: docs.openzeppelin.com/de...
- 登链社区 AI 助手,为大家转译优秀英文文章,如有翻译不通的地方,还请包涵~
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!