github.com/lbryio/lbcd@v0.22.119/txscript/claimscript_test.go (about)

     1  package txscript
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestCreationParseLoopClaim(t *testing.T) {
    10  
    11  	r := require.New(t)
    12  
    13  	// OP_CLAIMNAME <Name> <Value> OP_2DROP OP_DROP <P2PKH>
    14  	script, err := ClaimNameScript("tester", "value")
    15  	r.NoError(err)
    16  	cs, err := ExtractClaimScript(script)
    17  	r.NoError(err)
    18  	r.Equal(byte(OP_CLAIMNAME), cs.Opcode)
    19  	r.Equal([]byte("tester"), cs.Name)
    20  	r.Equal([]byte("value"), cs.Value)
    21  }
    22  
    23  func TestCreationParseLoopUpdate(t *testing.T) {
    24  
    25  	r := require.New(t)
    26  
    27  	claimID := []byte("12345123451234512345")
    28  	claim, err := ClaimUpdateScript("tester", claimID, "value")
    29  	r.NoError(err)
    30  	cs, err := ExtractClaimScript(claim)
    31  	r.NoError(err)
    32  	r.Equal(byte(OP_UPDATECLAIM), cs.Opcode)
    33  	r.Equal([]byte("tester"), cs.Name)
    34  	r.Equal(claimID, cs.ClaimID)
    35  	r.Equal([]byte("value"), cs.Value)
    36  }
    37  
    38  func TestCreationParseLoopSupport(t *testing.T) {
    39  
    40  	r := require.New(t)
    41  
    42  	claimID := []byte("12345123451234512345")
    43  
    44  	// case 1: OP_SUPPORTCLAIM <Name> <ClaimID> OP_2DROP OP_DROP <P2PKH>
    45  	script, err := ClaimSupportScript("tester", claimID, nil)
    46  	r.NoError(err)
    47  	cs, err := ExtractClaimScript(script)
    48  	r.NoError(err)
    49  
    50  	r.Equal(byte(OP_SUPPORTCLAIM), cs.Opcode)
    51  	r.Equal([]byte("tester"), cs.Name)
    52  	r.Equal(claimID, cs.ClaimID)
    53  	r.Nil(cs.Value)
    54  
    55  	// case 2: OP_SUPPORTCLAIM <Name> <ClaimID> <Value> OP_2DROP OP_2DROP <P2PKH>
    56  	script, err = ClaimSupportScript("tester", claimID, []byte("value"))
    57  	r.NoError(err)
    58  	cs, err = ExtractClaimScript(script)
    59  	r.NoError(err)
    60  
    61  	r.Equal(byte(OP_SUPPORTCLAIM), cs.Opcode)
    62  	r.Equal([]byte("tester"), cs.Name)
    63  	r.Equal(claimID, cs.ClaimID)
    64  	r.Equal([]byte("value"), cs.Value)
    65  
    66  }
    67  
    68  func TestInvalidChars(t *testing.T) {
    69  	r := require.New(t)
    70  
    71  	script, err := ClaimNameScript("tester", "value")
    72  	r.NoError(err)
    73  	r.NoError(AllClaimsAreSane(script, true))
    74  
    75  	for i := range []byte(illegalChars) {
    76  		script, err := ClaimNameScript("a"+illegalChars[i:i+1], "value")
    77  		r.NoError(err)
    78  		r.Error(AllClaimsAreSane(script, true))
    79  	}
    80  }