如何使用PHP验证以太坊签名的消息?
使用Web3.js中的web3.personal.sign
函数对消息进行签名,然后将签名发送到服务器。如何使用PHP进行验证?
(在Packagist上)是否有一些预先构建的软件包?我应该从头开始吗?是否可以在没有与RPC节点或链(链外)任何连接的情况下执行此操作?
我已经在以太坊StackExchange上找到some question about this,但它非常复杂且有点陈旧,所以我想知道是否有更新更好的解决方案。
我还找到了some package on GitHub,但是我不知道它是否可以与web3.personal.sign
一起使用。
我找到了一些链接:
答案 0 :(得分:1)
可以使用软件包php-ecrecover来验证签名消息。
您可以使用此软件包获取原始邮件地址,然后验证其是否与预期地址相同。
JS标志:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import android.util.Log;
public class TCPClient {
private String serverMessage;
/**
* Specify the Server Ip Address here. Whereas our Socket Server is started.
* */
public String serverIp = "192.168.178.20"; // your computer IP address
public static final int SERVERPORT = 1025;
private OnMessageReceived mMessageListener = null;
private boolean mRun = false;
private PrintWriter out = null;
private BufferedReader in = null;
/**
* Constructor of the class. OnMessagedReceived listens for the messages
received from server
*/
public TCPClient(final OnMessageReceived listener, String
ipAddressOfServerDevice)
{
mMessageListener = listener;
serverIp = ipAddressOfServerDevice;
}
/**
* Sends the message entered by client to the server
* @param message text entered by client
*/
public void sendMessage(String message){
if (out != null && !out.checkError()) {
System.out.println("message: "+ message);
out.println(message);
out.flush();
}
}
public void stopClient(){
mRun = false;
}
public void run() {
mRun = true;
try {
//here you must put your computer's IP address.
InetAddress serverAddr = InetAddress.getByName(serverIp);
Log.e("TCP SI Client", "SI: Connecting...");
//create a socket to make the connection with the server
Socket socket = new Socket(serverAddr, SERVERPORT);
try {
//send the message to the server
out = new PrintWriter(new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream())), true);
Log.e("TCP SI Client", "SI: Sent.");
Log.e("TCP SI Client", "SI: Done.");
//receive the message which the server sends back
in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
//in this while the client listens for the messages sent by the
server
while (mRun) {
serverMessage = in.readLine();
if (serverMessage != null && mMessageListener != null) {
//call the method messageReceived from MyActivity class
mMessageListener.messageReceived(serverMessage);
Log.e("RESPONSE FROM SERVER", "S: Received Message: '" +
serverMessage + "'");
}
serverMessage = null;
}
}
catch (Exception e)
{
Log.e("TCP SI Error", "SI: Error", e);
e.printStackTrace();
}
finally
{
//the socket must be closed. It is not possible to reconnect to
this socket
// after it is closed, which means a new socket instance has to
be created.
socket.close();
}
} catch (Exception e) {
Log.e("TCP SI Error", "SI: Error", e);
}
}
//Declare the interface. The method messageReceived(String message) will
must be implemented in the MyActivity
//class at on asynckTask doInBackground
public interface OnMessageReceived {
public void messageReceived(String message);
}
}
First Activity:
public class WelcomePage extends Activity {
private Button send;
private EditText ipPrompt;
public static String serverIp;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_page);
send = (Button)findViewById(R.id.enterIP);
ipPrompt = (EditText) findViewById(R.id.ipPrompt);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
serverIp = ipPrompt.getText().toString();
if ( !serverIp.matches("[" + .0123456789 + "]+") ||
serverIp.contains(" ") || serverIp.isEmpty() || serverIp.equals(null) ||
serverIp == null ) {
ipPrompt.setText("");
Toast.makeText(WelcomePage.this, "Invalid IP Address!",
Toast.LENGTH_LONG).show();
}
else {
Intent i = new Intent( view.getContext(), MyActivity.class);
startActivity(i);
}
}
});
}
public static String getServerIp() {
return serverIp;
}
}
PHP验证:
@SuppressLint("NewApi")
public class MyActivity extends Activity
{
private ListView mList;
private ArrayList<String> arrayList;
private MyCustomAdapter mAdapter;
private TCPClient mTcpClient = null;
private connectTask conctTask = null;
private String ipAddressOfServerDevice;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ipAddressOfServerDevice = WelcomePage.getServerIp();
arrayList = new ArrayList<String>();
final EditText editText = (EditText) findViewById(R.id.editText);
Button send = (Button)findViewById(R.id.send_button);
//relate the listView from java to the one created in xml
mList = (ListView)findViewById(R.id.list);
mAdapter = new MyCustomAdapter(this, arrayList);
mList.setAdapter(mAdapter);
mTcpClient = null;
// connect to the server
conctTask = new connectTask();
conctTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message = editText.getText().toString();
//add the text in the arrayList
arrayList.add("Android Client: " + message);
//sends the message to the server
if (mTcpClient != null)
{
mTcpClient.sendMessage(message);
}
//refresh the list
mAdapter.notifyDataSetChanged();
editText.setText("");
}
});
}
/*receive the message from server with asyncTask*/
public class connectTask extends AsyncTask<String,String,TCPClient> {
@Override
protected TCPClient doInBackground(String... message)
{
//create a TCPClient object and
mTcpClient = new TCPClient(new TCPClient.OnMessageReceived()
{
@Override
//here the messageReceived method is implemented
public void messageReceived(String message)
{
try
{
//this method calls the onProgressUpdate
publishProgress(message);
if(message!=null)
{
System.out.println("Return Message from Socket:::::
>>>>> "+message);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
},ipAddressOfServerDevice);
mTcpClient.run();
if(mTcpClient!=null)
{
mTcpClient.sendMessage("Initial Message when connected with
Socket Server");
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
//in the arrayList we add the messaged received from server
arrayList.add(values[0]);
// notify the adapter that the data set has changed. This means that
new message received
// from server was added to the list
mAdapter.notifyDataSetChanged();
}
}
@Override
protected void onDestroy()
{
try
{
System.out.println("onDestroy.");
mTcpClient.sendMessage("bye");
mTcpClient.stopClient();
conctTask.cancel(true);
conctTask = null;
}
catch (Exception e)
{
e.printStackTrace();
}
super.onDestroy();
}
}