github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/consensus/segwit/segwit_test.go (about) 1 package segwit 2 3 import ( 4 "encoding/hex" 5 "testing" 6 ) 7 8 func TestConvertProgram(t *testing.T) { 9 cases := []struct { 10 desc string 11 program string 12 script string 13 fun func(prog []byte) ([]byte, error) 14 }{ 15 { 16 desc: "multi sign 2-1", 17 program: "0020e402787b2bf9749f8fcdcc132a44e86bacf36780ec5df2189a11020d590533ee", 18 script: "76aa20e402787b2bf9749f8fcdcc132a44e86bacf36780ec5df2189a11020d590533ee88007c00c0", 19 fun: ConvertP2SHProgram, 20 }, 21 { 22 desc: "multi sign 5-3", 23 program: "00200824e931fb806bd77fdcd291aad3bd0a4493443a4120062bd659e64a3e0bac66", 24 script: "76aa200824e931fb806bd77fdcd291aad3bd0a4493443a4120062bd659e64a3e0bac6688007c00c0", 25 fun: ConvertP2SHProgram, 26 }, 27 { 28 desc: "single sign", 29 program: "001437e1aec83a4e6587ca9609e4e5aa728db7007449", 30 script: "76ab1437e1aec83a4e6587ca9609e4e5aa728db700744988ae7cac", 31 fun: ConvertP2PKHSigProgram, 32 }, 33 } 34 35 for i, c := range cases { 36 progBytes, err := hex.DecodeString(c.program) 37 if err != nil { 38 t.Fatal(err) 39 } 40 41 gotScript, err := c.fun(progBytes) 42 if c.script != hex.EncodeToString(gotScript) { 43 t.Errorf("case #%d (%s) got script:%s, expect script:%s", i, c.desc, hex.EncodeToString(gotScript), c.script) 44 } 45 } 46 } 47 48 func TestProgramType(t *testing.T) { 49 cases := []struct { 50 desc string 51 program string 52 fun func(prog []byte) bool 53 yes bool 54 }{ 55 { 56 desc: "normal P2WPKHScript", 57 program: "001437e1aec83a4e6587ca9609e4e5aa728db7007449", 58 fun: IsP2WPKHScript, 59 yes: true, 60 }, 61 { 62 desc: "ugly P2WPKHScript", 63 program: "511437e1aec83a4e6587ca9609e4e5aa728db7007449", 64 fun: IsP2WPKHScript, 65 yes: false, 66 }, 67 { 68 desc: "ugly P2WPKHScript", 69 program: "00200824e931fb806bd77fdcd291aad3bd0a4493443a4120062bd659e64a3e0bac66", 70 fun: IsP2WPKHScript, 71 yes: false, 72 }, 73 { 74 desc: "ugly P2WPKHScript", 75 program: "51", 76 fun: IsP2WPKHScript, 77 yes: false, 78 }, 79 { 80 desc: "bcrp script", 81 program: "6a046263727001012820e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c0", 82 fun: IsP2WPKHScript, 83 yes: false, 84 }, 85 { 86 desc: "call contract script", 87 program: "0462637270204e4f02d43bf50171f7f25d046b7f016002da410fc00d2e8902e7b170c98cf946", 88 fun: IsP2WPKHScript, 89 yes: false, 90 }, 91 { 92 desc: "normal P2WSHScript", 93 program: "00200824e931fb806bd77fdcd291aad3bd0a4493443a4120062bd659e64a3e0bac66", 94 fun: IsP2WSHScript, 95 yes: true, 96 }, 97 { 98 desc: "ugly P2WSHScript", 99 program: "51200824e931fb806bd77fdcd291aad3bd0a4493443a4120062bd659e64a3e0bac66", 100 fun: IsP2WSHScript, 101 yes: false, 102 }, 103 { 104 desc: "ugly P2WSHScript", 105 program: "001437e1aec83a4e6587ca9609e4e5aa728db7007449", 106 fun: IsP2WSHScript, 107 yes: false, 108 }, 109 { 110 desc: "ugly P2WSHScript", 111 program: "51", 112 fun: IsP2WSHScript, 113 yes: false, 114 }, 115 { 116 desc: "bcrp script", 117 program: "6a046263727001012820e9108d3ca8049800727f6a3505b3a2710dc579405dde03c250f16d9a7e1e6e787403ae7cac00c0", 118 fun: IsP2WSHScript, 119 yes: false, 120 }, 121 { 122 desc: "call contract script", 123 program: "0462637270204e4f02d43bf50171f7f25d046b7f016002da410fc00d2e8902e7b170c98cf946", 124 fun: IsP2WSHScript, 125 yes: false, 126 }, 127 { 128 desc: "normal IsStraightforward", 129 program: "51", 130 fun: IsStraightforward, 131 yes: true, 132 }, 133 { 134 desc: "ugly IsStraightforward", 135 program: "001437e1aec83a4e6587ca9609e4e5aa728db7007449", 136 fun: IsStraightforward, 137 yes: false, 138 }, 139 { 140 desc: "ugly IsStraightforward", 141 program: "00200824e931fb806bd77fdcd291aad3bd0a4493443a4120062bd659e64a3e0bac66", 142 fun: IsStraightforward, 143 yes: false, 144 }, 145 } 146 147 for i, c := range cases { 148 progBytes, err := hex.DecodeString(c.program) 149 if err != nil { 150 t.Fatal(err) 151 } 152 153 if c.fun(progBytes) != c.yes { 154 t.Errorf("case #%d (%s) got %t, expect %t", i, c.desc, c.fun(progBytes), c.yes) 155 } 156 } 157 }