Blockchain tutorial ether Source Analysis Accounts Package introduction

Source: Internet
Author: User

Tags: code accounts ERR basic Pat NTA account [] Tool

The accounts package implements the wallet and account management of the ETH client.
Data structure of the account:
Typeaccount struct {

Address Common. Address json:"address" //Ethereum account addressderived from the key

Urlurl json:"url" //Optional resource Locator within a backend

Wallet interface refers to a software wallet or a hardware wallet that contains one or more accounts.
Type Wallet struct {
The URL () URL//URL is used to get the canonical path that this wallet can access. It will be used by the upper layer to sort from all the back-end wallets.

Status () (string, error)//used to return a text value to identify the status of the current wallet. An error is also returned to identify any errors encountered by the wallet.

The Open (passphrase string) error//open initializes access to the wallet instance. If you open a purse, you must close it.

Close () error//Close releases any resources that are occupied by the Open method.

Accounts () []account//Accounts used to get wallet found a list of accounts. For tiered wallets, this list does not include an exhaustive listing of all accounts, but only accounts that are explicitly fixed during account derivation.

Derive (path Derivationpath, pin bool) (ACCOUNT,ERROR)//derive an attempt to explicitly derive a hierarchical deterministic account on the specified derived path. If the pin is true, the derived account is added to the list of tracking accounts for the wallet.

SelfDerive(base DerivationPath,chain ethereum.ChainStateReader)    //SelfDerive设置一个基本帐户导出路径,从中钱包尝试发现非零帐户,并自动将其添加到跟踪帐户列表中。SignHash(account Account, hash []byte)([]byte, error)    // SignHash 请求钱包来给传入的hash进行签名。SignTx(account Account, tx*types.Transaction, chainID *big.Int) (*types.Transaction, error)   // SignTx 请求钱包对指定的交易进行签名。SignHashWithPassphrase(accountAccount, passphrase string, hash []byte) ([]byte, error)    //SignHashWithPassphrase请求钱包使用给定的passphrase来签名给定的hashSignTxWithPassphrase(accountAccount, passphrase string, tx *types.Transaction, chainID *big.Int)(*types.Transaction, error)    // SignHashWithPassphrase请求钱包使用给定的passphrase来签名给定的transaction

The backend Backend,backend is a wallet provider. Can contain a batch of accounts. They can sign transactions on request.
Type backend struct {

Wallets() []wallet   // Wallets获取当前能够查找到的钱包Subscribe(sink chan <-WalletEvent) event.Subscription    // 订阅创建异步订阅,以便在后端检测到钱包的到达或离开时接收通知。

Manager is an account management tool that contains everything. Can communicate with all backends to sign the transaction.
ETH account definition, defined in Accounts.keystore.key.go
The ETH account consists of three information, ID, address and public key pair.
Type Keystruct {
Address Common. Address
Privatekey ECDSA. Privatekey
The process of creating an account at ETH:
1, the user enters a password (passphrase string)
2, internally a public-private key pair (Internal.ethapi.apinewAccount method) is randomly generated by elliptic curve algorithm
3, to the public key hash get address
4, the password using the scrypt algorithm encryption, get encrypted password Derivedkey
5, use the Derivedkey to encrypt the private key using AES-CTR algorithm, get ciphertext ciphertext
6, the Derivedkey and ciphertext hash get Mac, this Mac actually play the role of signing, in the decryption time to verify the legitimacy, to prevent others tampering
7, save the account address and encryption process to write dead or randomly generated parameters into the JSON file, that is, the above file
Create the core code of the account: (Accounts.keystore.keystore_passphrase.go)
The Encryptkey method in
Funcencryptkey (Key
key,authstring,scryptn,scryptpint) ([]byte,error)
Where key is an encrypted account, including ID, public private key, address
Auth is the password entered by the user

SCRYPTN, is the n in the scrypt algorithm

P in the Scryptp,scrypt algorithm
Derivedkey, err: = Scrypt. Key (Autharray, salt, SCRYPTN, Scryptr, SCRYPTP, Scryptdklen)
The password entered for the user name uses Scrypt encryption and returns a Derivedkey

Blockchain tutorial ether Source Analysis Accounts Package introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: