desnake / web3signature
Laravel 包,用于从来自 web3 客户端的已签名消息中提取地址。
dev-master
2022-10-23 14:47 UTC
Requires
- kornrunner/keccak: ^1.1
- laravel/framework: ^9.19
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-23 19:00:46 UTC
README
用于从 web3 已签名消息中提取地址的 Laravel 包
用法
Composer
composer require desnake/web3signature
视图
//prepare message to sign
const msgtext = {{$msg}}; //Message from backend database sent with view
const hashedMessage = Web3.utils.fromUtf8(msgtext);
console.log(hashedMessage);
//get user to sign the message
const signature = await window.web3.eth.sign(hashedMessage, accounts[0]);
//Make request to controller and verify signature in backend via API
//!!CAUTION!!
//Not much secure way to do this but only for demo purposes.
const URL = "api.myproject.com/api/v1/";
const request = URL + msgtext + "/" + signature;
window.location.href(request);
控制器
1: 创建一个 API 路由,以便将签名传递给控制器函数 2: 在控制器中导入 trait Web3ForContracts
use DeSnake\Web3signature\Web3ForContracts;
and Include trait into the controller class.
`` use Web3ForContracts;``
3: 从控制器中调用 personal_ecRecover 函数
Note: $msg is plaintext string that is signed and signature is the signed message
$msg = "Hello World"; //Message from backend database.
$address = $this->personal_ecRecover($msg, $signature);
注意
包处于开发模式。
完整示例在 ./Example 文件夹中可用。
特别感谢 https://github.com/wmh/php-ecrecover && https://github.com/Wekisen/php-ecrecover && https://github.com/simplito/elliptic-php