github.com/annchain/OG@v0.0.9/tests/testCrypto/main.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 main
    15  
    16  import (
    17  	"fmt"
    18  	types2 "github.com/annchain/OG/arefactor/og/types"
    19  	"github.com/annchain/OG/common"
    20  	"github.com/annchain/OG/common/crypto"
    21  	"github.com/annchain/OG/common/hexutil"
    22  	"github.com/annchain/OG/og/types"
    23  )
    24  
    25  func main() {
    26  	//pHash := common.FromHex("0x3f2d2085f7ad5243118fa033675ee867c179582606b4f7e05b04749ca019d254")
    27  	signer := crypto.NewSigner(crypto.CryptoTypeEd25519)
    28  	privKey, err := crypto.PrivateKeyFromString(
    29  		"0x009d9d0fe5e9ef0bb3bb4934db878688500fd0fd8e026c1ff1249b7e268c8a363aa7d45d13a5accb299dc7fe0f3b5fb0e9526b67008f7ead02c51c7b1f5a1d7b00")
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  	pubKey := signer.PubKey(privKey)
    34  	tx := newUnsignedSequencer(signer.Address(pubKey), 1, nil, 0)
    35  	// do sign work
    36  	data := tx.SignatureTargets()
    37  	signature := signer.Sign(privKey, data)
    38  	tx.GetBase().Signature = signature.SignatureBytes
    39  	tx.GetBase().PublicKey = signer.PubKey(privKey).KeyBytes
    40  	fmt.Println("dump ", tx.Dump())
    41  	fmt.Println("data ", hexutil.Encode(data))
    42  	fmt.Println("sig ", hexutil.Encode(signature.SignatureBytes))
    43  	//data2 :=tx.SignatureTargets()
    44  	ok := signer.Verify(
    45  		//ogcrypto.PublicKey{Type: signer.GetCryptoType(), KeyBytes: tx.GetBase().PublicKey},
    46  		pubKey,
    47  		//ogcrypto.Signature{Type: signer.GetCryptoType(), KeyBytes: tx.GetBase().Signature},
    48  		signature,
    49  		data)
    50  	if !ok {
    51  		panic(fmt.Sprintf("fail  %v", ok))
    52  	}
    53  
    54  }
    55  
    56  func newUnsignedSequencer(issuer common.Address, id uint64, contractHashOrder types2.Hashes, accountNonce uint64) types.Txi {
    57  	tx := types.Sequencer{
    58  		Issuer:            &issuer,
    59  		Id:                id,
    60  		ContractHashOrder: contractHashOrder,
    61  		TxBase: types.TxBase{
    62  			AccountNonce: accountNonce,
    63  			Type:         types.TxBaseTypeSequencer,
    64  		},
    65  	}
    66  	return &tx
    67  }