Web3js

如何在沒有 MetaMask 的情況下使用 web3 登錄?

  • November 9, 2018

是否可以在沒有 MetaMask 的情況下在 web3 中為使用者簽名?我正在嘗試執行我創建的智能合約,我正在使用 Infura 連接到 Kovan 測試網路,但是 web3.eth.accounts

$$ 0 $$未定義。如何在不使用 MetaMask 的情況下登錄使用者?這甚至可能嗎?如果沒有,我應該如何解決這個問題?謝謝,麻煩您了!

在這種情況下,您應該使用 web3-provider-engine。使用自定義私鑰和 Infura API URL(甚至是 TestRPC)執行的範常式式碼片段如下:

//Provider Engine sub-modules

const ProviderEngine = require('web3-provider-engine')
const CacheSubprovider = require('web3-provider-engine/subproviders/cache.js')
const FixtureSubprovider = require('web3-provider-engine/subproviders/fixture.js')
const FilterSubprovider = require('web3-provider-engine/subproviders/filters.js')
const VmSubprovider = require('web3-provider-engine/subproviders/vm.js')
const NonceSubprovider = require('web3-provider-engine/subproviders/nonce-tracker.js')
const RpcSubprovider = require('web3-provider-engine/subproviders/rpc.js')

//EthereumJS Wallet Sub-Provider

const WalletSubprovider = require('ethereumjs-wallet/provider-engine')
const walletFactory = require('ethereumjs-wallet')

//Web3 Module

const Web3 = require('web3')

//Wallet Initialization

var privateKey = "3a1076bf45ab87712ad64ccb3b10217737f7faacbf2872e88fdd9a537d8fe266"
var privateKeyBuffer = new Buffer(privateKey, "hex")
var myWallet = walletFactory.fromPrivateKey(privateKeyBuffer)

//Engine initialization & sub-provider attachment

var engine = new ProviderEngine();

engine.addProvider(new FixtureSubprovider({
 web3_clientVersion: 'ProviderEngine/v0.0.0/javascript',
 net_listening: true,
 eth_hashrate: '0x00',
 eth_mining: false,
 eth_syncing: true,
}))

// cache layer
engine.addProvider(new CacheSubprovider())

// filters
engine.addProvider(new FilterSubprovider())

// pending nonce
engine.addProvider(new NonceSubprovider())

// vm
engine.addProvider(new VmSubprovider())

// Here the URL can be your localhost for TestRPC or the Infura URL
engine.addProvider(new RpcSubprovider({
 rpcUrl: 'https://mainnet.infura.io/YOUR_ACCESS_TOKEN',
}))

// Wallet Attachment
engine.addProvider(new WalletSubprovider(myWallet))

// network connectivity error
engine.on('error', function(err){
 // report connectivity errors
 console.error(err.stack)
})

// start polling for blocks
engine.start()

//Actual Initialization of the web3 module

var web3 = new Web3(engine)

通過在每個透視 web3 函式呼叫的設置對像中將您的公鑰指定為“來自”值,您將能夠使用您提供的私鑰簽署任何您想要的交易。

編輯:我個人鄙視 web3 庫,因此,我決定修改程式碼片段以使用 ethers.js 庫,它具有更易於閱讀的 API:

//Provider Engine sub-modules

const ethers = require('ethers');
const Wallet = ethers.Wallet;
const Contract = ethers.Contract;
const utils = ethers.utils;
const providers = ethers.providers; 

//Note the "0x" appended at the start
let privateKey = "0x3a1076bf45ab87712ad64ccb3b10217737f7faacbf2872e88fdd9a537d8fe266";

let network = "http://192.168.1.1:8545";

//let network = "kovan";
//let network = "ropsten";
//let network = "rinkeby";
//let network = "homestead";

//let infuraAPIKey = "9LPi9fDukz8phfLXdy5K";

let provider = new providersJsonRpcProvider(network, 'homestead');
//let provider = new providers.InfuraProvider(network, infuraAPIKey);

let serverWallet = new Wallet(privateKey, provider);

基本上就是這樣。您現在可以使用它發送交易或構造 Contract 對像以與它的合約進行互動。有關更多資訊,請參閱 ethers.js 文件。

也許這有助於https://github.com/MetaMask/provider-engine>和一些資訊在這裡<http://truffleframework.com/tutorials/using-infura-custom-provider

引用自:https://ethereum.stackexchange.com/questions/29526