|
| 1 | +// Copyright 2019 The Swarm Authors |
| 2 | +// This file is part of the Swarm library. |
| 3 | +// |
| 4 | +// The Swarm library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// The Swarm library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with the Swarm library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +package pss |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "crypto/ecdsa" |
| 21 | + |
| 22 | + ethCrypto "github.com/ethereum/go-ethereum/crypto" |
| 23 | + whisper "github.com/ethereum/go-ethereum/whisper/whisperv6" |
| 24 | +) |
| 25 | + |
| 26 | +var cryptoBackend defaultCryptoBackend |
| 27 | + |
| 28 | +type CryptoBackend interface { |
| 29 | + GetSymKey(id string) ([]byte, error) |
| 30 | + GenerateSymKey() (string, error) |
| 31 | + AddSymKeyDirect(bytes []byte) (string, error) |
| 32 | + FromECDSAPub(pub *ecdsa.PublicKey) []byte |
| 33 | + UnmarshalPubkey(pub []byte) (*ecdsa.PublicKey, error) |
| 34 | + CompressPubkey(pubkey *ecdsa.PublicKey) []byte |
| 35 | +} |
| 36 | + |
| 37 | +//Used only in tests |
| 38 | +type CryptoUtils interface { |
| 39 | + GenerateKey() (*ecdsa.PrivateKey, error) |
| 40 | + NewKeyPair(ctx context.Context) (string, error) |
| 41 | + GetPrivateKey(id string) (*ecdsa.PrivateKey, error) |
| 42 | +} |
| 43 | + |
| 44 | +type defaultCryptoBackend struct { |
| 45 | + whisper *whisper.Whisper |
| 46 | + wapi *whisper.PublicWhisperAPI |
| 47 | +} |
| 48 | + |
| 49 | +func NewCryptoBackend() CryptoBackend { |
| 50 | + w := whisper.New(&whisper.DefaultConfig) |
| 51 | + cryptoBackend = defaultCryptoBackend{ |
| 52 | + whisper: w, |
| 53 | + wapi: whisper.NewPublicWhisperAPI(w), |
| 54 | + } |
| 55 | + return &cryptoBackend |
| 56 | +} |
| 57 | + |
| 58 | +func NewCryptoUtils() CryptoUtils { |
| 59 | + if cryptoBackend.whisper == nil { |
| 60 | + NewCryptoBackend() |
| 61 | + } |
| 62 | + return &cryptoBackend |
| 63 | +} |
| 64 | + |
| 65 | +func (crypto *defaultCryptoBackend) GetSymKey(id string) ([]byte, error) { |
| 66 | + return crypto.whisper.GetSymKey(id) |
| 67 | +} |
| 68 | + |
| 69 | +func (crypto *defaultCryptoBackend) GenerateSymKey() (string, error) { |
| 70 | + return crypto.whisper.GenerateSymKey() |
| 71 | +} |
| 72 | + |
| 73 | +func (crypto *defaultCryptoBackend) AddSymKeyDirect(bytes []byte) (string, error) { |
| 74 | + return crypto.whisper.AddSymKeyDirect(bytes) |
| 75 | +} |
| 76 | + |
| 77 | +// FromECDSA exports a public key into a binary dump. |
| 78 | +func (crypto *defaultCryptoBackend) FromECDSAPub(pub *ecdsa.PublicKey) []byte { |
| 79 | + return ethCrypto.FromECDSAPub(pub) |
| 80 | +} |
| 81 | + |
| 82 | +// CompressPubkey encodes a public key to the 33-byte compressed format. |
| 83 | +func (crypto *defaultCryptoBackend) CompressPubkey(pubkey *ecdsa.PublicKey) []byte { |
| 84 | + return ethCrypto.CompressPubkey(pubkey) |
| 85 | +} |
| 86 | + |
| 87 | +// UnmarshalPubkey converts bytes to a secp256k1 public key. |
| 88 | +func (crypto *defaultCryptoBackend) UnmarshalPubkey(pub []byte) (*ecdsa.PublicKey, error) { |
| 89 | + return ethCrypto.UnmarshalPubkey(pub) |
| 90 | +} |
| 91 | + |
| 92 | +// CryptoUtils |
| 93 | + |
| 94 | +func (crypto *defaultCryptoBackend) GenerateKey() (*ecdsa.PrivateKey, error) { |
| 95 | + return ethCrypto.GenerateKey() |
| 96 | +} |
| 97 | + |
| 98 | +func (crypto *defaultCryptoBackend) NewKeyPair(ctx context.Context) (string, error) { |
| 99 | + return crypto.wapi.NewKeyPair(ctx) |
| 100 | +} |
| 101 | + |
| 102 | +func (crypto *defaultCryptoBackend) GetPrivateKey(id string) (*ecdsa.PrivateKey, error) { |
| 103 | + return crypto.whisper.GetPrivateKey(id) |
| 104 | +} |
0 commit comments