github.com/annchain/OG@v0.0.9/deprecated/ogcrypto/signer_ed25519_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/common/hexutil"
    21  	"github.com/annchain/OG/arefactor/common/utilfuncs"
    22  	"github.com/annchain/OG/arefactor/og"
    23  	"github.com/stretchr/testify/assert"
    24  	"testing"
    25  )
    26  
    27  func TestSigner(t *testing.T) {
    28  	signer := SignerEd25519{}
    29  
    30  	pub, priv := signer.RandomKeyPair()
    31  
    32  	fmt.Println(hex.Dump(pub.KeyBytes))
    33  
    34  	fmt.Println(hex.Dump(priv.KeyBytes))
    35  	address := og.AddressFromPublicKey(&pub)
    36  	fmt.Println(hex.Dump(address.Bytes()))
    37  
    38  	fmt.Printf("%x\n", priv.KeyBytes[:])
    39  	fmt.Printf("%x\n", pub.KeyBytes[:])
    40  	fmt.Printf("%x\n", address)
    41  
    42  	pub2 := signer.PubKey(priv)
    43  	fmt.Println(hex.Dump(pub2.KeyBytes))
    44  	assert.True(t, bytes.Equal(pub.KeyBytes, pub2.KeyBytes))
    45  
    46  	content := []byte("This is a test")
    47  	sig := signer.Sign(priv, content)
    48  	fmt.Println(hex.Dump(sig.SignatureBytes))
    49  
    50  	assert.True(t, signer.Verify(pub2, sig, content))
    51  
    52  	content[0] = 0x88
    53  	assert.False(t, signer.Verify(pub2, sig, content))
    54  
    55  }
    56  
    57  func TestSignerEd25519_Sign(t *testing.T) {
    58  	data, err := hexutil.FromHex("0x000000000000000099fa25cbb3d8fdd1ec29aa499cf651972f0ee67b0000000000000001")
    59  	utilfuncs.PanicIfError(err, "fromhex")
    60  	signer := &SignerEd25519{}
    61  	//privKey,_:= PrivateKeyFromString("0x009d9d0fe5e9ef0bb3bb4934db878688500fd0fd8e026c1ff1249b7e268c8a363aa7d45d13a5accb299dc7fe0f3b5fb0e9526b67008f7ead02c51c7b1f5a1d7b00")
    62  	_, privKey := signer.RandomKeyPair()
    63  	sig := signer.Sign(privKey, data)
    64  	fmt.Printf("%x\n", sig.SignatureBytes[:])
    65  	ok := signer.Verify(signer.PubKey(privKey), sig, data)
    66  	if !ok {
    67  		t.Fatal(ok)
    68  	}
    69  }