Skip to content

Commit

Permalink
Fix parsing witness script to Payment with FromScript() (#188)
Browse files Browse the repository at this point in the history
* Fix parsing script into payment

* Fix gh action
  • Loading branch information
altafan authored Jan 28, 2022
1 parent 36be5ff commit 130e909
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ jobs:
- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Install Nigiri
run: |
mkdir ~/.nigiri; cd ~/.nigiri
curl https://travis.nigiri.network | bash; cd
docker-compose -f ~/.nigiri/docker-compose.yml up -d
- name: Run Nigiri
uses: vulpemventures/nigiri-github-action@v1

- name: Get dependencies
run: go get -v -t -d ./...
Expand Down
35 changes: 16 additions & 19 deletions payment/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ func FromPayment(payment *Payment) (*Payment, error) {

// FromScript creates parses a script into a Payment struct
func FromScript(
script []byte,
outputScript []byte,
net *network.Network,
blindingKey *btcec.PublicKey,
) (*Payment, error) {
if script == nil || len(script) == 0 {
if len(outputScript) == 0 {
return nil, errors.New("payment's script can't be empty or nil")
}

Expand All @@ -142,35 +142,32 @@ func FromScript(
tmpNet = net
}

scriptHash := make([]byte, 0)
witnessScriptHash := make([]byte, 0)
_script := make([]byte, 0)
witnessScript := make([]byte, 0)
switch address.GetScriptType(script) {
var script, scriptHash, witnessScript, witnessScriptHash []byte
switch address.GetScriptType(outputScript) {
case address.P2WpkhScript:
scriptHash = append(scriptHash, script[2:]...)
_script = buildScript(scriptHash, "p2pkh")
witnessScriptHash = append(scriptHash, script[2:]...)
witnessScript = script
scriptHash = outputScript[2:]
script = buildScript(scriptHash, "p2pkh")
witnessScriptHash = scriptHash
witnessScript = outputScript
case address.P2WshScript:
witnessScriptHash = append(scriptHash, script[2:]...)
witnessScript = script
witnessScriptHash = outputScript[2:]
witnessScript = outputScript
case address.P2ShScript:
scriptHash = append(scriptHash, script[2:len(script)-1]...)
_script = script
scriptHash = outputScript[2 : len(outputScript)-1]
script = outputScript
case address.P2PkhScript:
scriptHash = append(scriptHash, script[3:len(script)-2]...)
_script = script
scriptHash = outputScript[3 : len(outputScript)-2]
script = outputScript
// multisig, here we do not calculate the hashes because this payment
// must be wrapped into another one
default:
_script = script
script = outputScript
}

return &Payment{
Hash: scriptHash,
WitnessHash: witnessScriptHash,
Script: _script,
Script: script,
WitnessScript: witnessScript,
Network: tmpNet,
BlindingKey: blindingKey,
Expand Down

0 comments on commit 130e909

Please sign in to comment.