首先進(jìn)入Java后臺(tái)生成簽名示例代碼如下:
package cn.gdchent.springbootsell;
import cn.gdchent.springbootsell.utils.SolidityUtil;
import org.apache.catalina.connector.Connector;
import org.bouncycastle.util.encoders.Hex;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.Hash;
import org.web3j.crypto.Sign;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Numeric;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ServletComponentScan //Servlet 注解的掃描
@SpringBootApplication(scanBasePackages = {"cn.gdchent.springbootsell"})
//@MapperScan(basePackages={"cn.gdchent.springbootsell.generator"}) //如果你自己使用自定義配置數(shù)據(jù)源配置掃描mybatis生成的mapper路徑 就不需要這個(gè)注解
@ImportResource(locations = {"classpath:beans.xml"}) //自己定義xml文件 然后來(lái)讀取
public class SpringbootsellApplication {
//私鑰地址對(duì)應(yīng)賬號(hào)是9698
private static final String PRIVATE_KEY ="fec4de8b60d3f9ef0003fc4143d2675eb6c3dbced35633e8aa7854abbe8e0459";
public static void main(String[] args) {
SpringApplication.run(SpringbootsellApplication.class, args);
getSign();
}
public static String getSign(){
// 連接到以太坊網(wǎng)絡(luò)
Web3j web3j = Web3j.build(new HttpService("https://ropsten.infura.io/v3/YOUR_INFURA_PROJECT_ID"));
// 設(shè)置私鑰
Credentials credentials = Credentials.create(PRIVATE_KEY);
List<BigInteger> list = Arrays.asList(
BigInteger.valueOf(36),
BigInteger.valueOf(69),
BigInteger.valueOf(555)
);
String address = "0x21e35e6e68E465C96e0186a8FeEE356aD9A79Ee3";
String dataString = "這里是一個(gè)中文字符串";
//String dataString = "0xabcdef123456";
// 設(shè)置參數(shù)值
BigInteger num = BigInteger.valueOf(123);
// 構(gòu)建要簽名的數(shù)據(jù)
byte[] encodedNum = Numeric.toBytesPadded(num, 32);
//將interger類型的數(shù)組轉(zhuǎn)換為字節(jié)數(shù)組
List<Byte> byteList = new ArrayList<>();
for (BigInteger value : list) {
byte[] paddedBytes = Numeric.toBytesPadded(value, 32);
for (byte b : paddedBytes) {
byteList.add(b);
}
}
byte[] encodedList = new byte[byteList.size()];
for (int i = 0; i < byteList.size(); i++) {
encodedList[i] = byteList.get(i);
}
byte[] encodedAddress = Numeric.hexStringToByteArray(address);
byte[] encodedDataString = dataString.getBytes(StandardCharsets.UTF_8);
byte[] message = SolidityUtil.concatenate(encodedNum, encodedList, encodedAddress, encodedDataString);
// 對(duì)合并后的字節(jié)碼進(jìn)行keccak256哈希
byte[] hash = Hash.sha3(message);
// 使用私鑰簽名消息
Sign.SignatureData signatureData = Sign.signPrefixedMessage(hash, credentials.getEcKeyPair());
byte[] signature = SolidityUtil.concatenate(signatureData.getR(), signatureData.getS(), signatureData.getV());
String signResult = Hex.toHexString(signature);
// 打印簽名結(jié)果
System.out.println("Signature: 0x" +signResult);
return signResult;
}
/**
* 處理url地址帶數(shù)組作參數(shù)錯(cuò)誤情況
* @return
*/
@Bean
public TomcatServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
@Override
public void customize(Connector connector) {
connector.setProperty("relaxedPathChars", "\"<>[\\]^`{|}");
connector.setProperty("relaxedQueryChars", "\"<>[\\]^`{|}");
}
});
return factory;
}
}
運(yùn)行生成的Java簽名輸出如下圖所示:0x9fc0a4d018440dd8067914b31aa9cec8f95101d65991c686e5c98051ae314e0c57729ea5c261a225cbc9dab6656e1dc8e424e46564c28d64c308ce7b25bcd9f91c
web3j生成簽名.png
接下來(lái)進(jìn)入到solidity的驗(yàn)簽示例代碼如下:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract VerfityJava {
address public signAccount = 0x1D99d05a9616973Ba67751e7DD3600aBB29d9698;
address public javaAccount;
bytes32 public byteSig;
bytes32 public byteSig2;
event Check(address account);
function genMsg(
uint256 num,
uint256[] memory list,
address _address,
string memory dataString
) public returns (bytes32) {
//這里寫成鏈上存儲(chǔ)是為調(diào)試
byteSig = keccak256(abi.encodePacked(num, list, _address, dataString));
//第二次得到簽名hash
byteSig2 = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", byteSig));
return byteSig;
}
function check(
uint256 num,
uint256[] memory list,
address _address,
string memory dataString, //如果是中文這里是一個(gè)字符串
bytes memory sign
) external returns (bool) {
bytes32 messageHash = genMsg(num, list, _address, dataString);
javaAccount = recoverSigner(messageHash, sign);
emit Check(javaAccount);
return javaAccount == signAccount;
}
function splitSignature(bytes memory sign)
internal
pure
returns (
uint8,
bytes32,
bytes32
)
{
require(sign.length == 65, "invalid signature length");
bytes32 r;
bytes32 s;
uint8 v;
assembly {
// first 32 bytes, after the length prefix
r := mload(add(sign, 32))
// second 32 bytes
s := mload(add(sign, 64))
// final byte (first byte of the next 32 bytes)
v := byte(0, mload(add(sign, 96)))
}
return (v, r, s);
}
function recoverSigner(bytes32 message, bytes memory sign)
internal
pure
returns (address)
{
uint8 v;
bytes32 r;
bytes32 s;
(v, r, s) = splitSignature(sign);
return ecrecover(message, v, r, s);
}
}
驗(yàn)簽結(jié)果如下
合約驗(yàn)簽成功.png