miguelmota / go-ethereum-hdwallet

Ethereum HD Wallet derivations in Go (golang)

Home Page:https://github.com/miguelmota/go-ethereum-hdwallet

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for EIP155 signer

cypherhat opened this issue · comments

I love this package, but I want to use it to sign token transfers as well. Which, as I understand it means that I have to us: types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey). I can PR this package, but I am unsure how you would want this implemented. THis is less than DRY, but my thought is:

// SignTxEIP155 implements accounts.Wallet, which allows the account to sign an ERC-20 transaction.
func (w *Wallet) SignTxEIP155(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) {
	w.stateLock.RLock() // Comms have own mutex, this is for the state fields
	defer w.stateLock.RUnlock()

	// Make sure the requested account is contained within
	path, ok := w.paths[account.Address]
	if !ok {
		return nil, accounts.ErrUnknownAccount
	}

	privateKey, err := w.derivePrivateKey(path)
	if err != nil {
		return nil, err
	}

	// Sign the transaction and verify the sender to avoid hardware fault surprises
	signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
	if err != nil {
		return nil, err
	}

	msg, err := signedTx.AsMessage(types.NewEIP155Signer(chainID))
	if err != nil {
		return nil, err
	}

	sender := msg.From()
	if sender != account.Address {
		return nil, fmt.Errorf("signer mismatch: expected %s, got %s", account.Address.Hex(), sender.Hex())
	}

	return signedTx, nil
}

Thoughts?

Closed via #8