Handle ethereum txid and block hash with the standard 0x prefix
This commit is contained in:
parent
a75376706e
commit
9727a3dc8c
@ -40,7 +40,7 @@ type rpcBlock struct {
|
||||
}
|
||||
|
||||
func ethHashToHash(h ethcommon.Hash) string {
|
||||
return h.Hex()[2:]
|
||||
return h.Hex()
|
||||
}
|
||||
|
||||
func ethNumber(n string) (int64, error) {
|
||||
@ -88,7 +88,7 @@ func ethTxToTx(tx *rpcTransaction, blocktime int64, confirmations uint32) (*bcha
|
||||
Vout: []bchain.Vout{
|
||||
{
|
||||
N: 0, // there is always up to one To address
|
||||
// Value - cannot set, it does not fit precisely to float64
|
||||
// Value - cannot be set, it does not fit precisely to float64
|
||||
ScriptPubKey: bchain.ScriptPubKey{
|
||||
// Hex
|
||||
Addresses: ta,
|
||||
@ -110,18 +110,18 @@ func (p *EthereumParser) GetAddrIDFromVout(output *bchain.Vout) ([]byte, error)
|
||||
return p.GetAddrIDFromAddress(output.ScriptPubKey.Addresses[0])
|
||||
}
|
||||
|
||||
func has0xPrefix(s string) bool {
|
||||
return len(s) >= 2 && s[0] == '0' && (s[1]|32) == 'x'
|
||||
}
|
||||
|
||||
// GetAddrIDFromAddress returns internal address representation of given address
|
||||
func (p *EthereumParser) GetAddrIDFromAddress(address string) ([]byte, error) {
|
||||
// github.com/ethereum/go-ethereum/common.HexToAddress does not handle address errors, using own decoding
|
||||
if len(address) > 1 {
|
||||
if address[0:2] == "0x" || address[0:2] == "0X" {
|
||||
address = address[2:]
|
||||
}
|
||||
} else {
|
||||
if len(address) == 0 {
|
||||
return nil, bchain.ErrAddressMissing
|
||||
}
|
||||
return nil, errors.Errorf("Invalid address '%v'", address)
|
||||
if has0xPrefix(address) {
|
||||
address = address[2:]
|
||||
}
|
||||
if len(address) == 0 {
|
||||
return nil, bchain.ErrAddressMissing
|
||||
}
|
||||
if len(address)&1 == 1 {
|
||||
address = "0" + address
|
||||
@ -263,22 +263,28 @@ func (p *EthereumParser) PackedTxidLen() int {
|
||||
|
||||
// PackTxid packs txid to byte array
|
||||
func (p *EthereumParser) PackTxid(txid string) ([]byte, error) {
|
||||
if has0xPrefix(txid) {
|
||||
txid = txid[2:]
|
||||
}
|
||||
return hex.DecodeString(txid)
|
||||
}
|
||||
|
||||
// UnpackTxid unpacks byte array to txid
|
||||
func (p *EthereumParser) UnpackTxid(buf []byte) (string, error) {
|
||||
return hex.EncodeToString(buf), nil
|
||||
return hexutil.Encode(buf), nil
|
||||
}
|
||||
|
||||
// PackBlockHash packs block hash to byte array
|
||||
func (p *EthereumParser) PackBlockHash(hash string) ([]byte, error) {
|
||||
if has0xPrefix(hash) {
|
||||
hash = hash[2:]
|
||||
}
|
||||
return hex.DecodeString(hash)
|
||||
}
|
||||
|
||||
// UnpackBlockHash unpacks byte array to block hash
|
||||
func (p *EthereumParser) UnpackBlockHash(buf []byte) (string, error) {
|
||||
return hex.EncodeToString(buf), nil
|
||||
return hexutil.Encode(buf), nil
|
||||
}
|
||||
|
||||
// IsUTXOChain returns true if the block chain is UTXO type, otherwise false
|
||||
|
||||
@ -75,22 +75,22 @@ func TestEthereumParser_PackTx(t *testing.T) {
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "1",
|
||||
name: "with 0x prefix",
|
||||
args: args{
|
||||
tx: &bchain.Tx{
|
||||
Blocktime: 1521515026,
|
||||
Hex: "7b226e6f6e6365223a2230783239666165222c226761735072696365223a223078313261303566323030222c22676173223a2230786462626130222c22746f223a22307836383262373930336131313039386366373730633761656634616130326138356233663336303161222c2276616c7565223a22307830222c22696e707574223a223078663032356361616630303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030323235222c2268617368223a22307865366231363864366262336438656437386530336462663832386236626664316662363133663665313239636261363234393634393834353533373234633564222c22626c6f636b4e756d626572223a223078326263616630222c2266726f6d223a22307864616363396336313735346130633436313666633533323364633934366538396562323732333032222c227472616e73616374696f6e496e646578223a22307831222c2276223a2230783162222c2272223a22307831626434306133313132326330333931386466366431363664373430613661336132326630386132353933346365623136383863363239373736363163383063222c2273223a22307836303766626331356331663739393561343235386635613962636363363362303430333632643139393164356566653133363163353632323265346361383966227d",
|
||||
Time: 1521515026,
|
||||
Txid: "e6b168d6bb3d8ed78e03dbf828b6bfd1fb613f6e129cba624964984553724c5d",
|
||||
Txid: "0xe6b168d6bb3d8ed78e03dbf828b6bfd1fb613f6e129cba624964984553724c5d",
|
||||
Vin: []bchain.Vin{
|
||||
{
|
||||
Addresses: []string{"dacc9c61754a0c4616fc5323dc946e89eb272302"},
|
||||
Addresses: []string{"0xdacc9c61754a0c4616fc5323dc946e89eb272302"},
|
||||
},
|
||||
},
|
||||
Vout: []bchain.Vout{
|
||||
{
|
||||
ScriptPubKey: bchain.ScriptPubKey{
|
||||
Addresses: []string{"682b7903a11098cf770c7aef4aa02a85b3f3601a"},
|
||||
Addresses: []string{"0x682b7903a11098cf770c7aef4aa02a85b3f3601a"},
|
||||
},
|
||||
},
|
||||
},
|
||||
@ -101,7 +101,7 @@ func TestEthereumParser_PackTx(t *testing.T) {
|
||||
want: "08aebf0a1205012a05f20018a0f7362a24f025caaf00000000000000000000000000000000000000000000000000000000000002253220e6b168d6bb3d8ed78e03dbf828b6bfd1fb613f6e129cba624964984553724c5d38f095af014092f4c1d5054a14682b7903a11098cf770c7aef4aa02a85b3f3601a5214dacc9c61754a0c4616fc5323dc946e89eb272302580162011b6a201bd40a31122c03918df6d166d740a6a3a22f08a25934ceb1688c62977661c80c7220607fbc15c1f7995a4258f5a9bccc63b040362d1991d5efe1361c56222e4ca89f",
|
||||
},
|
||||
{
|
||||
name: "2",
|
||||
name: "without 0x prefix",
|
||||
args: args{
|
||||
tx: &bchain.Tx{
|
||||
Blocktime: 1521533434,
|
||||
@ -162,7 +162,7 @@ func TestEthereumParser_UnpackTx(t *testing.T) {
|
||||
Blocktime: 1521515026,
|
||||
Hex: "7b226e6f6e6365223a2230783239666165222c226761735072696365223a223078313261303566323030222c22676173223a2230786462626130222c22746f223a22307836383262373930336131313039386366373730633761656634616130326138356233663336303161222c2276616c7565223a22307830222c22696e707574223a223078663032356361616630303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030323235222c2268617368223a22307865366231363864366262336438656437386530336462663832386236626664316662363133663665313239636261363234393634393834353533373234633564222c22626c6f636b4e756d626572223a223078326263616630222c2266726f6d223a22307864616363396336313735346130633436313666633533323364633934366538396562323732333032222c227472616e73616374696f6e496e646578223a22307831222c2276223a2230783162222c2272223a22307831626434306133313132326330333931386466366431363664373430613661336132326630386132353933346365623136383863363239373736363163383063222c2273223a22307836303766626331356331663739393561343235386635613962636363363362303430333632643139393164356566653133363163353632323265346361383966227d",
|
||||
Time: 1521515026,
|
||||
Txid: "e6b168d6bb3d8ed78e03dbf828b6bfd1fb613f6e129cba624964984553724c5d",
|
||||
Txid: "0xe6b168d6bb3d8ed78e03dbf828b6bfd1fb613f6e129cba624964984553724c5d",
|
||||
Vin: []bchain.Vin{
|
||||
{
|
||||
Addresses: []string{"0xdacc9c61754a0c4616fc5323dc946e89eb272302"},
|
||||
@ -185,7 +185,7 @@ func TestEthereumParser_UnpackTx(t *testing.T) {
|
||||
Blocktime: 1521533434,
|
||||
Hex: "7b226e6f6e6365223a22307862323663222c226761735072696365223a223078343330653233343030222c22676173223a22307835323038222c22746f223a22307835353565653131666264646330653439613962616233353861383934316164393566666462343866222c2276616c7565223a22307831626330313539643533306536303030222c22696e707574223a223078222c2268617368223a22307863643634373135313535326235313332623261656637633962653030646336663733616663353930316464653135376161623133313333356261616138353362222c22626c6f636b4e756d626572223a223078326263663038222c2266726f6d223a22307833653361336436396463363662613130373337663533316564303838393534613965633839643937222c227472616e73616374696f6e496e646578223a22307861222c2276223a2230783239222c2272223a22307866373136316331373064343335373361643963386437303163646166373134666632613534386135363262306463363339323330643137383839666364343035222c2273223a22307833633439373766633930333835613237656661303033326531376234396664353735623238323663623536653364316563663231353234663261393466393135227d",
|
||||
Time: 1521533434,
|
||||
Txid: "cd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b",
|
||||
Txid: "0xcd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b",
|
||||
Vin: []bchain.Vin{
|
||||
{
|
||||
Addresses: []string{"0x3e3a3d69dc66ba10737f531ed088954a9ec89d97"},
|
||||
|
||||
@ -77,7 +77,7 @@ func TestEthRPC_GetBestBlockHash(t *testing.T) {
|
||||
fields: fields{
|
||||
b: setupEthRPC(),
|
||||
},
|
||||
want: 64,
|
||||
want: 66,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
@ -149,7 +149,7 @@ func TestEthRPC_GetBlockHash(t *testing.T) {
|
||||
args: args{
|
||||
height: 1000000,
|
||||
},
|
||||
want: "6e6b2e771a3026a1981227ab4a4c8d018edb568494f17df46bcddfa427df686e",
|
||||
want: "0x6e6b2e771a3026a1981227ab4a4c8d018edb568494f17df46bcddfa427df686e",
|
||||
},
|
||||
{
|
||||
name: "2870000",
|
||||
@ -159,7 +159,7 @@ func TestEthRPC_GetBlockHash(t *testing.T) {
|
||||
args: args{
|
||||
height: 2870000,
|
||||
},
|
||||
want: "eccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895",
|
||||
want: "0xeccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895",
|
||||
},
|
||||
{
|
||||
name: "ErrBlockNotFound",
|
||||
@ -214,7 +214,7 @@ func TestEthRPC_GetBlockHeader(t *testing.T) {
|
||||
hash: "eccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895",
|
||||
},
|
||||
want: &bchain.BlockHeader{
|
||||
Hash: "eccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895",
|
||||
Hash: "0xeccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895",
|
||||
Height: 2870000,
|
||||
Confirmations: int(uint32(bh.Number.Uint64()) - 2870000 + 1),
|
||||
},
|
||||
@ -274,7 +274,7 @@ func TestEthRPC_GetBlock(t *testing.T) {
|
||||
},
|
||||
want: &bchain.Block{
|
||||
BlockHeader: bchain.BlockHeader{
|
||||
Hash: "eccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895",
|
||||
Hash: "0xeccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895",
|
||||
Height: 2870000,
|
||||
Confirmations: int(uint32(bh.Number.Uint64()) - 2870000 + 1),
|
||||
},
|
||||
@ -291,7 +291,7 @@ func TestEthRPC_GetBlock(t *testing.T) {
|
||||
},
|
||||
want: &bchain.Block{
|
||||
BlockHeader: bchain.BlockHeader{
|
||||
Hash: "eccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895",
|
||||
Hash: "0xeccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895",
|
||||
Height: 2870000,
|
||||
Confirmations: int(uint32(bh.Number.Uint64()) - 2870000 + 1),
|
||||
},
|
||||
@ -370,7 +370,7 @@ func TestEthRPC_GetTransaction(t *testing.T) {
|
||||
Confirmations: uint32(bh.Number.Uint64()) - 2870000 + 1,
|
||||
Hex: "7b226e6f6e6365223a2230783239666165222c226761735072696365223a223078313261303566323030222c22676173223a2230786462626130222c22746f223a22307836383262373930336131313039386366373730633761656634616130326138356233663336303161222c2276616c7565223a22307830222c22696e707574223a223078663032356361616630303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030323235222c2268617368223a22307865366231363864366262336438656437386530336462663832386236626664316662363133663665313239636261363234393634393834353533373234633564222c22626c6f636b4e756d626572223a223078326263616630222c2266726f6d223a22307864616363396336313735346130633436313666633533323364633934366538396562323732333032222c227472616e73616374696f6e496e646578223a22307831222c2276223a2230783162222c2272223a22307831626434306133313132326330333931386466366431363664373430613661336132326630386132353933346365623136383863363239373736363163383063222c2273223a22307836303766626331356331663739393561343235386635613962636363363362303430333632643139393164356566653133363163353632323265346361383966227d",
|
||||
Time: 1521515026,
|
||||
Txid: "e6b168d6bb3d8ed78e03dbf828b6bfd1fb613f6e129cba624964984553724c5d",
|
||||
Txid: "0xe6b168d6bb3d8ed78e03dbf828b6bfd1fb613f6e129cba624964984553724c5d",
|
||||
Vin: []bchain.Vin{
|
||||
{
|
||||
Addresses: []string{"0xdacc9c61754a0c4616fc5323dc946e89eb272302"},
|
||||
@ -398,7 +398,7 @@ func TestEthRPC_GetTransaction(t *testing.T) {
|
||||
Confirmations: uint32(bh.Number.Uint64()) - 2871048 + 1,
|
||||
Hex: "7b226e6f6e6365223a22307862323663222c226761735072696365223a223078343330653233343030222c22676173223a22307835323038222c22746f223a22307835353565653131666264646330653439613962616233353861383934316164393566666462343866222c2276616c7565223a22307831626330313539643533306536303030222c22696e707574223a223078222c2268617368223a22307863643634373135313535326235313332623261656637633962653030646336663733616663353930316464653135376161623133313333356261616138353362222c22626c6f636b4e756d626572223a223078326263663038222c2266726f6d223a22307833653361336436396463363662613130373337663533316564303838393534613965633839643937222c227472616e73616374696f6e496e646578223a22307861222c2276223a2230783239222c2272223a22307866373136316331373064343335373361643963386437303163646166373134666632613534386135363262306463363339323330643137383839666364343035222c2273223a22307833633439373766633930333835613237656661303033326531376234396664353735623238323663623536653364316563663231353234663261393466393135227d",
|
||||
Time: 1521533434,
|
||||
Txid: "cd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b",
|
||||
Txid: "0xcd647151552b5132b2aef7c9be00dc6f73afc5901dde157aab131335baaa853b",
|
||||
Vin: []bchain.Vin{
|
||||
{
|
||||
Addresses: []string{"0x3e3a3d69dc66ba10737f531ed088954a9ec89d97"},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user