github.com/annchain/OG@v0.0.9/deprecated/ogcrypto/signer_secp256k1_test.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package ogcrypto
    15  
    16  import (
    17  	"bytes"
    18  	"encoding/hex"
    19  	"fmt"
    20  	"github.com/annchain/OG/arefactor/og"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestSignerSecp(t *testing.T) {
    27  	signer := SignerSecp256k1{}
    28  
    29  	pub, priv := signer.RandomKeyPair()
    30  
    31  	fmt.Println(hex.Dump(pub.KeyBytes))
    32  	fmt.Println(hex.Dump(priv.KeyBytes))
    33  	address := og.AddressFromPublicKey(&pub)
    34  	fmt.Println(hex.Dump(address.Bytes()))
    35  
    36  	fmt.Printf("%x\n", priv.KeyBytes[:])
    37  	fmt.Printf("%x\n", pub.KeyBytes[:])
    38  	fmt.Printf("%x\n", address)
    39  
    40  	pub2 := signer.PubKey(priv)
    41  	fmt.Println(hex.Dump(pub2.KeyBytes))
    42  	assert.True(t, bytes.Equal(pub.KeyBytes, pub2.KeyBytes))
    43  
    44  	content := []byte("This is a test")
    45  	sig := signer.Sign(priv, content)
    46  	fmt.Println(hex.Dump(sig.SignatureBytes))
    47  
    48  	assert.True(t, signer.Verify(pub2, sig, content))
    49  
    50  	content[0] = 0x88
    51  	assert.False(t, signer.Verify(pub2, sig, content))
    52  
    53  }
    54  
    55  func TestSignerNewPrivKey(t *testing.T) {
    56  	t.Parallel()
    57  
    58  	signer := SignerSecp256k1{}
    59  	pk, priv := signer.RandomKeyPair()
    60  
    61  	b := []byte("foo")
    62  	sig := signer.Sign(priv, b)
    63  	if !signer.Verify(pk, sig, b) {
    64  		t.Fatalf("vertfy failed")
    65  	}
    66  
    67  }