Go-Ethereum

從 web3 創建新帳戶顯示錯誤

  • February 19, 2019

在使用 web3 從 Angular 創建新帳戶時

  1. 個人.newAccount 函式()未定義
import { 
Injectable } from 
'@angular/core';

import { 
User } from 
'./login/user';

const Web3=require('web3');

declare let
require: any;

declare let
window: any;



let tokenAbi =
require('./tokenContract.json');



@Injectable()

export class
ContractService {



private contAbi=require('./tokenContract.json');

private contAddr='0xadd2b6f750e0a05b25840ac9037365ce79f6a475';

web3:any;

private contract:any;

constructor() {



if (typeof
this.web3 !==
'undefined') {

this.web3 =
new Web3(this.web3.currentProvider);

} else {

this.web3 =
new Web3(new
Web3.providers.HttpProvider("http://localhost:8545"));

}

console.log(this.web3._provider);

this.web3.eth.getAccounts((err,
accs) => {

if (err !=
null) {

alert('There was an error fetching your accounts.');

return;

}



if (accs.length ===
0) {

alert('Couldn\'t get any accounts! Make sure your Ethereum client is configured correctly.');

return;

}

console.log(accs[0]);

this.web3.eth.defaultAccount =
accs[0];


});

this.contract=new
this.web3.eth.Contract(this.contAbi,this.contAddr);

// console.log(this.contract.methods.greet().call({from:this.web3.eth.defaultAccount,gas:0x27100},function(error,result){

// console.log(error);

// console.log(result);

// }));


}






public register(user:User){

user.address=this.web3.personal.newAccount(user.password);


}



}

您的 web3 通過 RPC 與節點通信。如果是geth節點,則personal出於安全原因,預設情況下不會公開/啟用 api。

謹慎行事。

geth --rpcapi eth,web3,personal除了其他參數之外,您還可以啟用它。

希望能幫助到你。

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