使用場景大多數用在DAPP中呼叫中心化資料或者操作某些中心化功能的時候通過DAPP呼叫MetaMask錢包對資料進行簽名傳遞給后臺,后臺驗證簽名資料是否是否當前用戶錢包地址簽名的資料實作鑒權,
一、DAPP端用Web3簽名資料
注:不同的web3版本簽名代碼有點差異
1、0.26版本簽名 web3.personal.sign
//引數1:要簽名的資料
//引數2:簽名的錢包地址
web3.personal.sign(web3.fromUtf8("Hello Dapp"), "0x40141cF4756A72DF8D8f81c1E0c2ad403C127b9E",(err, res) => {
console.log("簽名后的資料:",res)
//0x53ea88d24f4ef8cdcc4bcc843912510b065cd6014c453ff61316c4cd75162f0a38f83a2103da028fb8e5181292ba194b0c8aa21a9ddacdf6783ebfa608889d121c
})
//web3.eth.sign此簽名方法MetaMask會提示未來版本會被移除
2、1.0版本簽名 web3.eth.personal.sign
3、喚醒MetaMask錢包簽名資料
簽名后資料為:0x53ea88d24f4ef8cdcc4bcc843912510b065cd6014c453ff61316c4cd75162f0a38f83a2103da028fb8e5181292ba194b0c8aa21a9ddacdf6783ebfa608889d121c

二、Java端使用Web3J校驗資料
1、正常校驗 true

2、改變下錢包地址在次校驗 false

三、后端原始碼-來源于github上的原始碼
1、web3j包
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>3.6.0</version>
</dependency>
2、校驗原始碼
package zh.block.manage.common.utils;
import org.web3j.crypto.*;
import org.web3j.utils.Numeric;
import org.web3j.crypto.Sign.SignatureData;
import java.math.BigInteger;
import java.util.Arrays;
/**
* 以太坊簽名訊息校驗工具
*/
public class MetaMaskUtil {
/**
* 以太坊自定義的簽名訊息都以以下字符開頭
* 參考 eth_sign in https://github.com/ethereum/wiki/wiki/JSON-RPC
*/
public static final String PERSONAL_MESSAGE_PREFIX = "\u0019Ethereum Signed Message:\n";
public static void main(String[] args) {
//簽名后的資料
String signature="0x53ea88d24f4ef8cdcc4bcc843912510b065cd6014c453ff61316c4cd75162f0a38f83a2103da028fb8e5181292ba194b0c8aa21a9ddacdf6783ebfa608889d121c";
//簽名原文
String message="Hello Dapp";
//簽名的錢包地址
String address="0xc290436b3da897115493a1547B52783c50f0Bef3";
Boolean result = validate(signature,message,address);
System.out.println(result);
}
/**
* 對簽名訊息,原始訊息,賬號地址三項資訊進行認證,判斷簽名是否有效
* @param signature
* @param message
* @param address
* @return
*/
public static boolean validate(String signature, String message, String address) {
//參考 eth_sign in https://github.com/ethereum/wiki/wiki/JSON-RPC
// eth_sign
// The sign method calculates an Ethereum specific signature with:
// sign(keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))).
//
// By adding a prefix to the message makes the calculated signature recognisable as an Ethereum specific signature.
// This prevents misuse where a malicious DApp can sign arbitrary data (e.g. transaction) and use the signature to
// impersonate the victim.
String prefix = PERSONAL_MESSAGE_PREFIX + message.length();
byte[] msgHash = Hash.sha3((prefix + message).getBytes());
byte[] signatureBytes = Numeric.hexStringToByteArray(signature);
byte v = signatureBytes[64];
if (v < 27) {
v += 27;
}
SignatureData sd = new SignatureData(
v,
Arrays.copyOfRange(signatureBytes, 0, 32),
Arrays.copyOfRange(signatureBytes, 32, 64));
String addressRecovered = null;
boolean match = false;
// Iterate for each possible key to recover
for (int i = 0; i < 4; i++) {
BigInteger publicKey = Sign.recoverFromSignature(
(byte) i,
new ECDSASignature(new BigInteger(1, sd.getR()), new BigInteger(1, sd.getS())),
msgHash);
if (publicKey != null) {
addressRecovered = "0x" + Keys.getAddress(publicKey);
if (addressRecovered.equals(address)) {
match = true;
break;
}
}
}
return match;
}
}
多鏈錢包充提系統


學如逆水行舟,不進則退,心似平原跑馬,易放難收,【區塊鏈】【系統/網路/運維】【云計算/大資料】【資料庫】【移動開發】【后端開發】【游戲開發】【UI設計】【微服務】【爬蟲】【Java】【Go】【C++】【PHP】【Python】【Android/IOS】【HTML/CSS】【JavaScript】【Node】【VUE】【ReactNaive】,,,
歡迎各位大神萌新一起專研分享各行各業技術!
Chain區塊鏈開發社區:593674370

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/297359.html
標籤:區塊鏈
