mesh-bitcoin icon indicating copy to clipboard operation
mesh-bitcoin copied to clipboard

enable parsing output address in field of string type

Open alejoacosta74 opened this issue 1 year ago • 0 comments

Fixes # .

Motivation

After Bitcoin core 23.0, bitcoind no longer returns the addresses field in RPC methods gettxout, getrawtransaction, decoderawtransaction, decodescript, gettransaction verbose=true (see bitcoin release notes for more detail). Instead, a address field is returned, which should be decoded in a Address string field within the ScriptPubKey struct in bitcoin's client types.

Solution

This PR contains a fix that supports both the deprecated Addresses []string field and the new Address string field, that will enable Rosetta-bitcoin implementation to be compatible with bitcoin core v23 while maintaining backward compatibility with previous bitcoin releases Proposed code changes

type ScriptPubKey struct {
	ASM          string   `json:"asm"`
	Hex          string   `json:"hex"`
	RequiredSigs int64    `json:"reqSigs,omitempty"`
	Type         string   `json:"type"`
	Addresses    []string `json:"addresses,omitempty"`
        Address string  `json:"address,omitempty"` //! <---new field added 
}
func (b *Client) parseOutputAccount(
	scriptPubKey *ScriptPubKey,
) *types.AccountIdentifier {
	if scriptPubKey.Address == "" { // <--- new logic to handle both cases with "Addresses []string" and "Address string"
		if len(scriptPubKey.Addresses) != 1 {
			return &types.AccountIdentifier{Address: scriptPubKey.Hex}
		}
		return &types.AccountIdentifier{Address: scriptPubKey.Addresses[0]}
	}
	return &types.AccountIdentifier{Address: scriptPubKey.Address}
}

Unit tests for the bitcoin client have been slightly modified to take the new scenario into consideration.

  • A new client fixture json file has been added (get_block_responses_2_bis.json) which is a copy of the original test data (i.e. get_block_responses_2.json) but with the data fields modified as described above.
  • In line with the new json file added, a var with the corresponding new block (block100000_bis) has been added to the test file bitcoin/client_test.go to update unit tests accordigly

Open questions

N/A

alejoacosta74 avatar Mar 09 '23 20:03 alejoacosta74