github.com/incognitochain/go-incognito-sdk@v1.0.1/privacy/key.go (about)

     1  package privacy
     2  
     3  import (
     4  	"encoding/hex"
     5  )
     6  
     7  // 32-byte spending key
     8  type PrivateKey []byte
     9  
    10  // 32-byte public key
    11  type PublicKey []byte
    12  
    13  // 32-byte receiving key
    14  type ReceivingKey []byte
    15  
    16  // 32-byte transmission key
    17  type TransmissionKey []byte
    18  
    19  // ViewingKey is a public/private key pair to encrypt coins in an outgoing transaction
    20  // and decrypt coins in an incoming transaction
    21  type ViewingKey struct {
    22  	Pk PublicKey    // 33 bytes, use to receive coin
    23  	Rk ReceivingKey // 32 bytes, use to decrypt pointByte
    24  }
    25  
    26  // PaymentAddress is an address of a payee
    27  type PaymentAddress struct {
    28  	Pk PublicKey       // 33 bytes, use to receive coin
    29  	Tk TransmissionKey // 33 bytes, use to encrypt pointByte
    30  }
    31  
    32  // PaymentInfo contains an address of a payee and a value of coins he/she will receive
    33  type PaymentInfo struct {
    34  	PaymentAddress PaymentAddress
    35  	Amount         uint64
    36  	Message        []byte // 512 bytes
    37  }
    38  
    39  // GeneratePrivateKey generates a random 32-byte spending key
    40  func GeneratePrivateKey(seed []byte) PrivateKey {
    41  	bip32PrivKey := HashToScalar(seed)
    42  	privateKey := bip32PrivKey.ToBytesS()
    43  	return privateKey
    44  }
    45  
    46  // GeneratePublicKey computes a 32-byte public-key corresponding to a spending key
    47  func GeneratePublicKey(privateKey []byte) PublicKey {
    48  	privScalar := new(Scalar).FromBytesS(privateKey)
    49  	publicKey := new(Point).ScalarMultBase(privScalar)
    50  	return publicKey.ToBytesS()
    51  }
    52  
    53  // GenerateReceivingKey generates a 32-byte receiving key
    54  func GenerateReceivingKey(privateKey []byte) ReceivingKey {
    55  	receivingKey := HashToScalar(privateKey[:])
    56  	return receivingKey.ToBytesS()
    57  }
    58  
    59  // GenerateTransmissionKey computes a 33-byte transmission key corresponding to a receiving key
    60  func GenerateTransmissionKey(receivingKey []byte) TransmissionKey {
    61  	receiScalar := new(Scalar).FromBytesS(receivingKey)
    62  	transmissionKey := new(Point).ScalarMultBase(receiScalar)
    63  	return transmissionKey.ToBytesS()
    64  }
    65  
    66  // GenerateViewingKey generates a viewingKey corresponding to a spending key
    67  func GenerateViewingKey(privateKey []byte) ViewingKey {
    68  	var viewingKey ViewingKey
    69  	viewingKey.Pk = GeneratePublicKey(privateKey)
    70  	viewingKey.Rk = GenerateReceivingKey(privateKey)
    71  	return viewingKey
    72  }
    73  
    74  // GeneratePaymentAddress generates a payment address corresponding to a spending key
    75  func GeneratePaymentAddress(privateKey []byte) PaymentAddress {
    76  	var paymentAddress PaymentAddress
    77  	paymentAddress.Pk = GeneratePublicKey(privateKey)
    78  	paymentAddress.Tk = GenerateTransmissionKey(GenerateReceivingKey(privateKey))
    79  	return paymentAddress
    80  }
    81  
    82  // Bytes converts payment address to bytes array
    83  func (addr *PaymentAddress) Bytes() []byte {
    84  	return append(addr.Pk[:], addr.Tk[:]...)
    85  }
    86  
    87  // SetBytes reverts bytes array to payment address
    88  func (addr *PaymentAddress) SetBytes(bytes []byte) *PaymentAddress {
    89  	// the first 33 bytes are public key
    90  	addr.Pk = bytes[:Ed25519KeySize]
    91  	// the last 33 bytes are transmission key
    92  	addr.Tk = bytes[Ed25519KeySize:]
    93  	return addr
    94  }
    95  
    96  // String encodes a payment address as a hex string
    97  func (addr PaymentAddress) String() string {
    98  	byteArrays := addr.Bytes()
    99  	return hex.EncodeToString(byteArrays[:])
   100  }
   101  
   102  func SliceToArray(slice []byte) [Ed25519KeySize]byte {
   103  	var array [Ed25519KeySize]byte
   104  	copy(array[:], slice)
   105  	return array
   106  }
   107  
   108  func ArrayToSlice(array [Ed25519KeySize]byte) []byte {
   109  	var slice []byte
   110  	slice = array[:]
   111  	return slice
   112  }