ericet
ericet

Likecoin/Desmos/Evmos/Nomic/Cerberus 验证人。喜欢分享各类脚本。 网站: https://ericet.xyz

【科学家养成日记#8】生成消息签名,提交到API

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

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


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

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

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

代码:

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;
    });
}

效果:


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

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

CC BY-NC-ND 2.0 版权声明

喜欢我的文章吗?
别忘了给点支持与赞赏,让我知道创作的路上有你陪伴。

加载中…
加载中…

发布评论