|
| 1 | +package arkade |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "encoding/json" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/btcsuite/btcd/btcec/v2" |
| 11 | + "github.com/btcsuite/btcd/btcec/v2/schnorr" |
| 12 | + "github.com/btcsuite/btcd/btcutil/psbt" |
| 13 | + "github.com/btcsuite/btcd/txscript" |
| 14 | + "github.com/btcsuite/btcd/wire" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +func TestReadArkadeScript(t *testing.T) { |
| 20 | + fix := readScriptFixtures(t) |
| 21 | + |
| 22 | + t.Run("valid", func(t *testing.T) { |
| 23 | + for _, f := range fix.Valid { |
| 24 | + t.Run(f.Name, func(t *testing.T) { |
| 25 | + ptx := decodePSBT(t, f.Psbt) |
| 26 | + signerPubKey := decodeXOnlyPubKey(t, f.SignerPublicKey) |
| 27 | + entry := decodeEntry(t, f.Entry) |
| 28 | + |
| 29 | + result, err := ReadArkadeScript(ptx, signerPubKey, entry) |
| 30 | + require.NoError(t, err) |
| 31 | + require.NotNil(t, result) |
| 32 | + |
| 33 | + require.Equal(t, entry.Script, result.script) |
| 34 | + require.Equal(t, ArkadeScriptHash(entry.Script), result.hash) |
| 35 | + require.Equal(t, len(entry.Witness), len(result.witness)) |
| 36 | + for i := range entry.Witness { |
| 37 | + require.Equal(t, entry.Witness[i], result.witness[i]) |
| 38 | + } |
| 39 | + |
| 40 | + expectedPubKey := ComputeArkadeScriptPublicKey(signerPubKey, result.hash) |
| 41 | + require.True(t, expectedPubKey.IsEqual(result.pubkey)) |
| 42 | + |
| 43 | + tapscript := ptx.Inputs[entry.Vin].TaprootLeafScript[0].Script |
| 44 | + require.Equal(t, txscript.NewBaseTapLeaf(tapscript), result.tapLeaf) |
| 45 | + }) |
| 46 | + } |
| 47 | + }) |
| 48 | + |
| 49 | + t.Run("invalid", func(t *testing.T) { |
| 50 | + for _, f := range fix.Invalid { |
| 51 | + t.Run(f.Name, func(t *testing.T) { |
| 52 | + ptx := decodePSBT(t, f.Psbt) |
| 53 | + signerPubKey := decodeXOnlyPubKey(t, f.SignerPublicKey) |
| 54 | + entry := decodeEntry(t, f.Entry) |
| 55 | + |
| 56 | + _, err := ReadArkadeScript(ptx, signerPubKey, entry) |
| 57 | + require.Error(t, err) |
| 58 | + require.Contains(t, err.Error(), f.ErrorContains) |
| 59 | + }) |
| 60 | + } |
| 61 | + }) |
| 62 | +} |
| 63 | + |
| 64 | +type scriptFixtureEntry struct { |
| 65 | + Vin int `json:"vin"` |
| 66 | + Script string `json:"script"` |
| 67 | + Witness []string `json:"witness"` |
| 68 | +} |
| 69 | + |
| 70 | +type validScriptFixture struct { |
| 71 | + Name string `json:"name"` |
| 72 | + SignerPublicKey string `json:"signerPublicKey"` |
| 73 | + Psbt string `json:"psbt"` |
| 74 | + Entry scriptFixtureEntry `json:"entry"` |
| 75 | +} |
| 76 | + |
| 77 | +type invalidScriptFixture struct { |
| 78 | + Name string `json:"name"` |
| 79 | + SignerPublicKey string `json:"signerPublicKey"` |
| 80 | + Psbt string `json:"psbt"` |
| 81 | + Entry scriptFixtureEntry `json:"entry"` |
| 82 | + ErrorContains string `json:"errorContains"` |
| 83 | +} |
| 84 | + |
| 85 | +type scriptFixtures struct { |
| 86 | + Valid []validScriptFixture `json:"valid"` |
| 87 | + Invalid []invalidScriptFixture `json:"invalid"` |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +func readScriptFixtures(t *testing.T) scriptFixtures { |
| 92 | + t.Helper() |
| 93 | + data, err := os.ReadFile("testdata/read_arkade_script.json") |
| 94 | + require.NoError(t, err) |
| 95 | + |
| 96 | + var fix scriptFixtures |
| 97 | + require.NoError(t, json.Unmarshal(data, &fix)) |
| 98 | + return fix |
| 99 | +} |
| 100 | + |
| 101 | +func decodePSBT(t *testing.T, b64 string) *psbt.Packet { |
| 102 | + t.Helper() |
| 103 | + ptx, err := psbt.NewFromRawBytes(strings.NewReader(b64), true) |
| 104 | + require.NoError(t, err) |
| 105 | + return ptx |
| 106 | +} |
| 107 | + |
| 108 | +func decodeXOnlyPubKey(t *testing.T, hexStr string) *btcec.PublicKey { |
| 109 | + t.Helper() |
| 110 | + data, err := hex.DecodeString(hexStr) |
| 111 | + require.NoError(t, err) |
| 112 | + pubKey, err := schnorr.ParsePubKey(data) |
| 113 | + require.NoError(t, err) |
| 114 | + return pubKey |
| 115 | +} |
| 116 | + |
| 117 | +func decodeEntry(t *testing.T, raw scriptFixtureEntry) IntrospectorEntry { |
| 118 | + t.Helper() |
| 119 | + script, err := hex.DecodeString(raw.Script) |
| 120 | + require.NoError(t, err) |
| 121 | + |
| 122 | + witness := make(wire.TxWitness, len(raw.Witness)) |
| 123 | + for i, w := range raw.Witness { |
| 124 | + witness[i], err = hex.DecodeString(w) |
| 125 | + require.NoError(t, err) |
| 126 | + } |
| 127 | + |
| 128 | + return IntrospectorEntry{ |
| 129 | + Vin: uint16(raw.Vin), |
| 130 | + Script: script, |
| 131 | + Witness: witness, |
| 132 | + } |
| 133 | +} |
0 commit comments