Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic in ToAsm() caused by last commit. #109

Merged
merged 11 commits into from
Mar 19, 2022
6 changes: 4 additions & 2 deletions bscript/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,12 @@ func (s *Script) ToASM() (string, error) {
}
} else {
if data && len(p) <= 4 {
b := make([]byte, 0)
b = append(b, p...)
for i := 0; i < 4-len(p); i++ {
p = append(p, 0)
b = append(b, 0)
}
asmScript.WriteString(fmt.Sprintf("%d", binary.LittleEndian.Uint32(p)))
asmScript.WriteString(fmt.Sprintf("%d", binary.LittleEndian.Uint32(b)))
} else {
asmScript.WriteString(hex.EncodeToString(p))
}
Expand Down
16 changes: 16 additions & 0 deletions bscript/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,19 @@ func TestRunScriptExample2(t *testing.T) {
t.Errorf("\nExpected %q\ngot %q", expected, asm)
}
}

func TestRunScriptExample3(t *testing.T) {
script, _ := hex.DecodeString("006a223139694733575459537362796f7333754a373333794b347a45696f69314665734e55010042666166383166326364346433663239383061623162363564616166656231656631333561626339643534386461633466366134656361623230653033656365362d300274780134")
s := bscript.Script(script)

asm, err := s.ToASM()
if err != nil {
t.Error(err)
t.FailNow()
}

expected := "0 OP_RETURN 3139694733575459537362796f7333754a373333794b347a45696f69314665734e55 0 666166383166326364346433663239383061623162363564616166656231656631333561626339643534386461633466366134656361623230653033656365362d30 30836 52"
if asm != expected {
t.Errorf("\nExpected %q\ngot %q", expected, asm)
}
}