scatter配合硬件錢包實現EOS離線簽名

發現https://eostoolkit.io/home年久失修,無奈本身寫個scatter調用,配合cleos能夠實現任意命令調用。html

一、scatter導入硬件錢包帳號node

二、新建index.html,完整源碼以下npm

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>scatter離線簽名</title>
    <script src="https://cdn.scattercdn.com/file/scatter-cdn/js/latest/scatterjs-core.min.js"></script>
    <script src="https://cdn.scattercdn.com/file/scatter-cdn/js/latest/scatterjs-plugin-eosjs.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/eosjs@16.0.9/lib/eos.min.js"></script>
</head>
<body>
    正在調用scatter..
<script>

// Don't forget to tell ScatterJS which plugins you are using.
ScatterJS.plugins( new ScatterEOS() );

// Networks are used to reference certain blockchains.
// They let you get accounts and help you build signature providers.
const network = {
    blockchain:'eos',
    protocol:'https',
    host:'nodes.get-scatter.com',
    port:443,
    chainId:'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906'
}

// First we need to connect to the user's Scatter.
ScatterJS.scatter.connect('My-App').then(connected => {

    // If the user does not have Scatter or it is Locked or Closed this will return false;
    if(!connected) return false;

    const scatter = ScatterJS.scatter;

    // Now we need to get an identity from the user.
    // We're also going to require an account that is connected to the network we're using.
    const requiredFields = { accounts:[network] };
    scatter.getIdentity(requiredFields).then(() => {

        // Always use the accounts you got back from Scatter. Never hardcode them even if you are prompting
        // the user for their account name beforehand. They could still give you a different account.
        const account = scatter.identity.accounts.find(x => x.blockchain === 'eos');

        // You can pass in any additional options you want into the eosjs reference.
        const eosOptions = { expireInSeconds:60 };

        // Get a proxy reference to eosjs which you can use to sign transactions with a user's Scatter.
        const eos = scatter.eos(network, Eos, eosOptions);

        // ----------------------------
        // Now that we have an identity,
        // an EOSIO account, and a reference
        // to an eosjs object we can send a transaction.
        // ----------------------------


        // Never assume the account's permission/authority. Always take it from the returned account.
        const transactionOptions = { authorization:[`${account.name}@${account.authority}`] };

        const result = eos.transaction({
            actions: [{
                  "account": "eosio",
                  "name": "linkauth",
                  "authorization": [{
                      "actor": "aaaaaaaaaaa",
                      "permission": "active"
                    }
                  ],
                  "data": "500f75193bc969c40000000000ea305580d3355c5de94c44000000e02ae94c44"
                }
            ]
        }).catch(error => {
            console.error(error);
        });
        console.log('Success =>', JSON.stringify(result));

    }).catch(error => {
        // The user rejected this request, or doesn't have the appropriate requirements.
        console.error(error);
    });
});
</script>

</body>
</html>

三、使用cleos命令配合參數-d -s生成未簽名交易,拷貝交易裏的actions替換掉代碼裏的actions瀏覽器

四、用瀏覽器打開index.html,這時候就會喚出scatter登陸,而後受權,最後在硬件錢包點確認app

相關文章
相關標籤/搜索