很多项目为了让用户省点GAS,经常只需用户提交签名就能完成交易

比如你在Decentraland的市场挂单交易, 只需签名一下,就能挂单到市场

image.png

逻辑是,用你的私钥给一个信息加密生成一个签名,然后把这个信息提交到项目的API上

理解这个逻辑,就比较容易自动化这个过程。这次拿今天比较热门的一个NFT项目(Prime Ape Planet)白名单抽奖做个例子

逻辑是,新建一个钱包,然后给参与白名单抽奖的信息弄个签名,然后把签名提交到API上

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var Web3 = require('web3');
const fs = require('fs');
const axios = require('axios');
var web3 = new Web3('https://eth-mainnet.alchemyapi.io/v2/drsYFmqbDqcTNhWsrceiuHE4x_Zmzlb7');

//需要签名的信息
const msg = "I consent to be included in the Prime Ape Planet raffle.\n\nNo gas fee will be needed to perform this action."

setInterval(function(){
start();
},15*1000);

async function start() {
//创建新钱包
let wallet = web3.eth.accounts.create();
//给信息签名
const { signature } = await web3.eth.accounts.sign(msg, wallet.privateKey);
//提交签名到API
axios.get(`https://primeapeplanet.com/api/${signature}`)
.then(res=>{
console.log(res.data);
//记录钱包地址和私钥到keys.txt文件
writeOutput(`${wallet.address}:${wallet.privateKey}\n`);
}).catch(err=>{
console.log(err)
})

}

function writeOutput(data) {
fs.appendFile('keys.txt', data, function (err) {
if (err) throw err;
});
}

效果:
image.png

github:https://github.com/ericet/kexuejia/blob/master/primeApeWhitelist.js

自动化签名的应用场景还有很多,比如zapper的每日打卡,某项目的签名就领NFT,decentraland市场交易,等等