web3 test init samples
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
51
auth.html
Normal file
51
auth.html
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Wallet Authentication</title>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/web3@1.3.5/dist/web3.min.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<button id="login">Login with MetaMask</button>
|
||||||
|
<script>
|
||||||
|
document.getElementById('login').onclick = async () => {
|
||||||
|
if (typeof window.ethereum !== 'undefined') {
|
||||||
|
const web3 = new Web3(window.ethereum);
|
||||||
|
try {
|
||||||
|
// Request account access
|
||||||
|
await window.ethereum.request({ method: 'eth_requestAccounts' });
|
||||||
|
|
||||||
|
// Get the user's account
|
||||||
|
const accounts = await web3.eth.getAccounts();
|
||||||
|
const account = accounts[0];
|
||||||
|
|
||||||
|
// Sign a message
|
||||||
|
const message = "Sign this message to authenticate";
|
||||||
|
const signature = await web3.eth.personal.sign(message, account);
|
||||||
|
|
||||||
|
// Send the signature to the server for verification
|
||||||
|
fetch('/verify_signature.php', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ account, signature, message })
|
||||||
|
}).then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
if (data.verified) {
|
||||||
|
alert('Authentication successful!');
|
||||||
|
} else {
|
||||||
|
alert('Authentication failed!');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('User denied account access:', error);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
alert('Please install MetaMask!');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
45
verify.php
Normal file
45
verify.php
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once 'path/to/web3.php/src/Web3/Providers/HttpProvider.php';
|
||||||
|
require_once 'path/to/web3.php/src/Web3/RequestManagers/RequestManager.php';
|
||||||
|
require_once 'path/to/web3.php/src/Web3/Web3.php';
|
||||||
|
require_once 'path/to/web3.php/src/Web3/Eth.php';
|
||||||
|
require_once 'path/to/web3.php/src/Web3/Utils.php';
|
||||||
|
|
||||||
|
// https://github.com/sc0Vu/web3.php
|
||||||
|
// https://github.com/kornrunner/php-keccak
|
||||||
|
// https://github.com/web3p/ethereum-util
|
||||||
|
|
||||||
|
// Include the necessary files
|
||||||
|
require_once 'path/to/Keccak.php'; // Adjust the path
|
||||||
|
require_once 'path/to/Util.php'; // Adjust the path
|
||||||
|
|
||||||
|
use kornrunner\Keccak;
|
||||||
|
use Web3p\EthereumUtil\Util;
|
||||||
|
|
||||||
|
function verifySignature($address, $signature, $message) {
|
||||||
|
$util = new Util();
|
||||||
|
|
||||||
|
// Hash the message
|
||||||
|
$msgHash = Keccak::hash("\x19Ethereum Signed Message:\n" . strlen($message) . $message, 256);
|
||||||
|
|
||||||
|
// Recover the public key from the signature
|
||||||
|
$pubKey = $util->recoverPublicKey($msgHash, $signature);
|
||||||
|
|
||||||
|
// Convert the public key to an Ethereum address
|
||||||
|
$derivedAddress = $util->publicKeyToAddress($pubKey);
|
||||||
|
|
||||||
|
// Compare the derived address with the given address
|
||||||
|
return strtolower($address) === strtolower($derivedAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch and decode the JSON POST request
|
||||||
|
$data = json_decode(file_get_contents('php://input'), true);
|
||||||
|
$account = $data['account'];
|
||||||
|
$signature = $data['signature'];
|
||||||
|
$message = $data['message'];
|
||||||
|
|
||||||
|
$verified = verifySignature($account, $signature, $message);
|
||||||
|
|
||||||
|
echo json_encode(['verified' => $verified]);
|
||||||
|
|
Reference in New Issue
Block a user