add types.Ed25519PublicKey helper

parent 9fdfb42d
......@@ -189,10 +189,7 @@ func (h *Host) managedFinalizeContract(builder modules.TransactionBuilder, rente
ParentID: contractTxn.FileContractID(0),
UnlockConditions: types.UnlockConditions{
PublicKeys: []types.SiaPublicKey{
{
Algorithm: types.SignatureEd25519,
Key: renterPK[:],
},
types.Ed25519PublicKey(renterPK),
hostSPK,
},
SignaturesRequired: 2,
......
......@@ -280,10 +280,7 @@ func (h *Host) managedVerifyNewContract(txnSet []types.Transaction, renterPK cry
// the host knows how to spend.
expectedUH := types.UnlockConditions{
PublicKeys: []types.SiaPublicKey{
{
Algorithm: types.SignatureEd25519,
Key: renterPK[:],
},
types.Ed25519PublicKey(renterPK),
publicKey,
},
SignaturesRequired: 2,
......
......@@ -293,10 +293,7 @@ func (h *Host) managedVerifyRenewedContract(so storageObligation, txnSet []types
// the host knows how to spend.
expectedUH := types.UnlockConditions{
PublicKeys: []types.SiaPublicKey{
{
Algorithm: types.SignatureEd25519,
Key: renterPK[:],
},
types.Ed25519PublicKey(renterPK),
publicKey,
},
SignaturesRequired: 2,
......
......@@ -96,10 +96,7 @@ func (h *Host) establishDefaults() error {
return err
}
h.secretKey = sk
h.publicKey = types.SiaPublicKey{
Algorithm: types.SignatureEd25519,
Key: pk[:],
}
h.publicKey = types.Ed25519PublicKey(pk)
// Subscribe to the consensus set.
err = h.initConsensusSubscription()
......
......@@ -28,13 +28,12 @@ func FormContract(params ContractParams, txnBuilder transactionBuilder, tpool tr
if err != nil {
return modules.RenterContract{}, err
}
ourPublicKey := types.SiaPublicKey{
Algorithm: types.SignatureEd25519,
Key: ourPK[:],
}
// create unlock conditions
uc := types.UnlockConditions{
PublicKeys: []types.SiaPublicKey{ourPublicKey, host.PublicKey},
PublicKeys: []types.SiaPublicKey{
types.Ed25519PublicKey(ourPK),
host.PublicKey,
},
SignaturesRequired: 2,
}
......
......@@ -37,27 +37,16 @@ type (
}
)
// generateUnlockConditions provides the unlock conditions that would be
// automatically generated from the input public key.
func generateUnlockConditions(pk crypto.PublicKey) types.UnlockConditions {
return types.UnlockConditions{
PublicKeys: []types.SiaPublicKey{{
Algorithm: types.SignatureEd25519,
Key: pk[:],
}},
SignaturesRequired: 1,
}
}
// generateSpendableKey creates the keys and unlock conditions a given index of a
// seed.
// generateSpendableKey creates the keys and unlock conditions for seed at a
// given index.
func generateSpendableKey(seed modules.Seed, index uint64) spendableKey {
// Generate the keys and unlock conditions.
entropy := crypto.HashAll(seed, index)
sk, pk := crypto.GenerateKeyPairDeterministic(entropy)
sk, pk := crypto.GenerateKeyPairDeterministic(crypto.HashAll(seed, index))
return spendableKey{
UnlockConditions: generateUnlockConditions(pk),
SecretKeys: []crypto.SecretKey{sk},
UnlockConditions: types.UnlockConditions{
PublicKeys: []types.SiaPublicKey{types.Ed25519PublicKey(pk)},
SignaturesRequired: 1,
},
SecretKeys: []crypto.SecretKey{sk},
}
}
......
......@@ -125,6 +125,15 @@ type (
}
)
// Ed25519PublicKey returns pk as a SiaPublicKey, denoting its algorithm as
// Ed25519.
func Ed25519PublicKey(pk crypto.PublicKey) SiaPublicKey {
return SiaPublicKey{
Algorithm: SignatureEd25519,
Key: pk[:],
}
}
// UnlockHash calculates the root hash of a Merkle tree of the
// UnlockConditions object. The leaves of this tree are formed by taking the
// hash of the timelock, the hash of the public keys (one leaf each), and the
......
package types
import (
"bytes"
"testing"
"github.com/NebulousLabs/Sia/crypto"
)
// TestEd25519PublicKey tests the Ed25519PublicKey function.
func TestEd25519PublicKey(t *testing.T) {
_, pk, err := crypto.GenerateKeyPair()
if err != nil {
t.Fatal(err)
}
spk := Ed25519PublicKey(pk)
if spk.Algorithm != SignatureEd25519 {
t.Error("Ed25519PublicKey created key with wrong algorithm specifier:", spk.Algorithm)
}
if !bytes.Equal(spk.Key, pk[:]) {
t.Error("Ed25519PublicKey created key with wrong data")
}
}
// TestUnlockHash runs the UnlockHash code.
func TestUnlockHash(t *testing.T) {
uc := UnlockConditions{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment