-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The function queries electrum for a details of the specific transaction and converts it in a format expected by the bitcoin.Chain interface. The simpliest solution is to use `GetTransaction` function to query electrum as it returns the transaction details in verbose format. Unfortunatelly it's not compatible with Esplora/Electrs implementation of ElectrumX server. (see: Blockstream/electrs#36) As a workaround to cover integration with Esplora/Electrs we use `GetRawTransaction` and deserialize the transaction.
- Loading branch information
Showing
2 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package electrum | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/btcsuite/btcd/wire" | ||
|
||
"github.com/keep-network/keep-core/pkg/bitcoin" | ||
) | ||
|
||
// decodeTransaction deserializes a transaction from the hexadecimal serialized | ||
// string to a btcd message format. | ||
func decodeTransaction(rawTx string) (*wire.MsgTx, error) { | ||
headerBytes, err := hex.DecodeString(rawTx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to decode a hex string: [%w]", err) | ||
} | ||
|
||
buf := bytes.NewBuffer(headerBytes) | ||
|
||
var t wire.MsgTx | ||
if err := t.Deserialize(buf); err != nil { | ||
return nil, fmt.Errorf("failed to deserialize a transaction: [%w]", err) | ||
} | ||
|
||
return &t, nil | ||
} | ||
|
||
// convertRawTransaction transforms a transaction provided in the hexadecimal serialized | ||
// string to the format expected by the bitcoin.Chain interface. | ||
func convertRawTransaction(rawTx string) (*bitcoin.Transaction, error) { | ||
t, err := decodeTransaction(rawTx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to decode a transaction: [%w]", err) | ||
} | ||
|
||
result := &bitcoin.Transaction{ | ||
Version: int32(t.Version), | ||
Locktime: t.LockTime, | ||
} | ||
|
||
for _, vin := range t.TxIn { | ||
input := &bitcoin.TransactionInput{ | ||
Outpoint: &bitcoin.TransactionOutpoint{ | ||
TransactionHash: bitcoin.Hash(vin.PreviousOutPoint.Hash), | ||
OutputIndex: vin.PreviousOutPoint.Index, | ||
}, | ||
SignatureScript: vin.SignatureScript, | ||
Sequence: vin.Sequence, | ||
} | ||
|
||
result.Inputs = append(result.Inputs, input) | ||
} | ||
|
||
for _, vout := range t.TxOut { | ||
output := &bitcoin.TransactionOutput{ | ||
Value: vout.Value, | ||
PublicKeyScript: vout.PkScript, | ||
} | ||
|
||
result.Outputs = append(result.Outputs, output) | ||
} | ||
|
||
return result, nil | ||
} |