0

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

 2 years ago
source link: https://mirror.xyz/ericet.eth/UUs4hXb9TopJ4vj0EeKU03d3JQ7SwriIjAuQR8Zkpp0
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

December 28th, 2021

很多项目为了让用户省点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市场交易,等等


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK