Skip to content

Commit 5bef693

Browse files
committed
remove error from SignHash
1 parent a665c0e commit 5bef693

13 files changed

+30
-98
lines changed

compatibility/siag_1.0_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ func verifyKeysSiag_1_0(uc types.UnlockConditions, folder string, keyname string
5959
CoveredFields: types.CoveredFields{WholeTransaction: true},
6060
})
6161
sigHash := txn.SigHash(int(j))
62-
sig, err := crypto.SignHash(sigHash, loadedKeys[i].SecretKey)
63-
if err != nil {
64-
return err
65-
}
62+
sig := crypto.SignHash(sigHash, loadedKeys[i].SecretKey)
6663
txn.TransactionSignatures[j].Signature = sig[:]
6764
i++
6865
j++

crypto/signatures.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,10 @@ func ReadSignedObject(r io.Reader, obj interface{}, maxLen uint64, pk PublicKey)
8181
return encoding.Unmarshal(encObj, obj)
8282
}
8383

84-
// SignHash signs a message using a secret key. Though the current
85-
// implementation will never return an error, switching libraries in the future
86-
// may result in errors that can be returned.
87-
func SignHash(data Hash, sk SecretKey) (sig Signature, err error) {
84+
// SignHash signs a message using a secret key.
85+
func SignHash(data Hash, sk SecretKey) Signature {
8886
skNorm := [SecretKeySize]byte(sk)
89-
sig = *ed25519.Sign(&skNorm, data[:])
90-
return sig, nil
87+
return *ed25519.Sign(&skNorm, data[:])
9188
}
9289

9390
// VerifyHash uses a public key and input data to verify a signature.
@@ -104,9 +101,6 @@ func VerifyHash(data Hash, pk PublicKey, sig Signature) error {
104101
// WriteSignedObject writes a length-prefixed object prefixed by its signature.
105102
func WriteSignedObject(w io.Writer, obj interface{}, sk SecretKey) error {
106103
objBytes := encoding.Marshal(obj)
107-
sig, err := SignHash(HashBytes(objBytes), sk)
108-
if err != nil {
109-
return err
110-
}
104+
sig := SignHash(HashBytes(objBytes), sk)
111105
return encoding.NewEncoder(w).EncodeAll(sig, objBytes)
112106
}

crypto/signatures_test.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,7 @@ func TestUnitSignatureEncoding(t *testing.T) {
154154
// Create a signature using the secret key.
155155
var signedData Hash
156156
rand.Read(signedData[:])
157-
sig, err := SignHash(signedData, sk)
158-
if err != nil {
159-
t.Fatal(err)
160-
}
157+
sig := SignHash(signedData, sk)
161158

162159
// Marshal and unmarshal the signature.
163160
marshalledSig := encoding.Marshal(sig)
@@ -195,10 +192,7 @@ func TestUnitSigning(t *testing.T) {
195192
// Generate and sign the data.
196193
var randData Hash
197194
rand.Read(randData[:])
198-
sig, err := SignHash(randData, sk)
199-
if err != nil {
200-
t.Fatal(err)
201-
}
195+
sig := SignHash(randData, sk)
202196

203197
// Verify the signature.
204198
err = VerifyHash(randData, pk, sig)
@@ -242,10 +236,7 @@ func TestIntegrationSigKeyGeneration(t *testing.T) {
242236
if err != nil {
243237
t.Fatal(err)
244238
}
245-
sig, err := SignHash(message, randSecKey)
246-
if err != nil {
247-
t.Fatal(err)
248-
}
239+
sig := SignHash(message, randSecKey)
249240
err = VerifyHash(message, randPubKey, sig)
250241
if err != nil {
251242
t.Error(err)
@@ -261,10 +252,7 @@ func TestIntegrationSigKeyGeneration(t *testing.T) {
261252
var detEntropy [EntropySize]byte
262253
detEntropy[0] = 35
263254
detSecKey, detPubKey := GenerateKeyPairDeterministic(detEntropy)
264-
sig, err = SignHash(message, detSecKey)
265-
if err != nil {
266-
t.Fatal(err)
267-
}
255+
sig = SignHash(message, detSecKey)
268256
err = VerifyHash(message, detPubKey, sig)
269257
if err != nil {
270258
t.Error(err)

modules/consensus/accept_txntypes_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,7 @@ func (cst *consensusSetTester) testFileContractRevision() {
498498
FileContractRevisions: []types.FileContractRevision{fcr},
499499
TransactionSignatures: []types.TransactionSignature{ts},
500500
}
501-
encodedSig, err := crypto.SignHash(txn.SigHash(0), sk)
502-
if err != nil {
503-
panic(err)
504-
}
501+
encodedSig := crypto.SignHash(txn.SigHash(0), sk)
505502
txn.TransactionSignatures[0].Signature = encodedSig[:]
506503
err = cst.tpool.AcceptTransactionSet([]types.Transaction{txn})
507504
if err != nil {

modules/host/negotiate.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,9 @@ func createRevisionSignature(fcr types.FileContractRevision, renterSig types.Tra
151151
TransactionSignatures: []types.TransactionSignature{renterSig, hostSig},
152152
}
153153
sigHash := txn.SigHash(1)
154-
encodedSig, err := crypto.SignHash(sigHash, secretKey)
155-
if err != nil {
156-
return types.Transaction{}, err
157-
}
154+
encodedSig := crypto.SignHash(sigHash, secretKey)
158155
txn.TransactionSignatures[1].Signature = encodedSig[:]
159-
err = modules.VerifyFileContractRevisionTransactionSignatures(fcr, txn.TransactionSignatures, blockHeight)
156+
err := modules.VerifyFileContractRevisionTransactionSignatures(fcr, txn.TransactionSignatures, blockHeight)
160157
if err != nil {
161158
return types.Transaction{}, err
162159
}

modules/negotiate.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,7 @@ func CreateAnnouncement(addr NetAddress, pk types.SiaPublicKey, sk crypto.Secret
342342

343343
// Create a signature for the announcement.
344344
annHash := crypto.HashBytes(annBytes)
345-
sig, err := crypto.SignHash(annHash, sk)
346-
if err != nil {
347-
return nil, err
348-
}
345+
sig := crypto.SignHash(annHash, sk)
349346
// Return the signed announcement.
350347
return append(annBytes, sig[:]...), nil
351348
}

modules/renter/contractor/negotiate_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,7 @@ func TestReviseContract(t *testing.T) {
251251
}
252252

253253
// sign the transaction
254-
encodedSig, err := crypto.SignHash(signedTxn.SigHash(0), sk)
255-
if err != nil {
256-
t.Fatal(err)
257-
}
254+
encodedSig := crypto.SignHash(signedTxn.SigHash(0), sk)
258255
signedTxn.TransactionSignatures[0].Signature = encodedSig[:]
259256

260257
err = signedTxn.StandaloneValid(ct.contractor.blockHeight)

modules/renter/proto/formcontract.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,7 @@ func FormContract(params ContractParams, txnBuilder transactionBuilder, tpool tr
200200
FileContractRevisions: []types.FileContractRevision{initRevision},
201201
TransactionSignatures: []types.TransactionSignature{renterRevisionSig},
202202
}
203-
encodedSig, err := crypto.SignHash(revisionTxn.SigHash(0), ourSK)
204-
if err != nil {
205-
return modules.RenterContract{}, modules.WriteNegotiationRejection(conn, errors.New("failed to sign revision transaction: "+err.Error()))
206-
}
203+
encodedSig := crypto.SignHash(revisionTxn.SigHash(0), ourSK)
207204
revisionTxn.TransactionSignatures[0].Signature = encodedSig[:]
208205

209206
// Send acceptance and signatures

modules/renter/proto/negotiate.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ func verifyRecentRevision(conn net.Conn, contract modules.RenterContract) error
7979
return errors.New("couldn't read challenge: " + err.Error())
8080
}
8181
// sign and return
82-
sig, err := crypto.SignHash(challenge, contract.SecretKey)
83-
if err != nil {
84-
return err
85-
} else if err := encoding.WriteObject(conn, sig); err != nil {
82+
sig := crypto.SignHash(challenge, contract.SecretKey)
83+
if err := encoding.WriteObject(conn, sig); err != nil {
8684
return errors.New("couldn't send challenge response: " + err.Error())
8785
}
8886
// read acceptance
@@ -124,7 +122,7 @@ func negotiateRevision(conn net.Conn, rev types.FileContractRevision, secretKey
124122
}},
125123
}
126124
// sign the transaction
127-
encodedSig, _ := crypto.SignHash(signedTxn.SigHash(0), secretKey) // no error possible
125+
encodedSig := crypto.SignHash(signedTxn.SigHash(0), secretKey)
128126
signedTxn.TransactionSignatures[0].Signature = encodedSig[:]
129127

130128
// send the revision

modules/renter/proto/renew.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,7 @@ func Renew(contract modules.RenterContract, params ContractParams, txnBuilder tr
197197
FileContractRevisions: []types.FileContractRevision{initRevision},
198198
TransactionSignatures: []types.TransactionSignature{renterRevisionSig},
199199
}
200-
encodedSig, err := crypto.SignHash(revisionTxn.SigHash(0), ourSK)
201-
if err != nil {
202-
return modules.RenterContract{}, modules.WriteNegotiationRejection(conn, errors.New("failed to sign revision transaction: "+err.Error()))
203-
}
200+
encodedSig := crypto.SignHash(revisionTxn.SigHash(0), ourSK)
204201
revisionTxn.TransactionSignatures[0].Signature = encodedSig[:]
205202

206203
// Send acceptance and signatures

modules/wallet/transactionbuilder.go

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type transactionBuilder struct {
5050
// addSignatures will sign a transaction using a spendable key, with support
5151
// for multisig spendable keys. Because of the restricted input, the function
5252
// is compatible with both siacoin inputs and siafund inputs.
53-
func addSignatures(txn *types.Transaction, cf types.CoveredFields, uc types.UnlockConditions, parentID crypto.Hash, spendKey spendableKey) (newSigIndices []int, err error) {
53+
func addSignatures(txn *types.Transaction, cf types.CoveredFields, uc types.UnlockConditions, parentID crypto.Hash, spendKey spendableKey) (newSigIndices []int) {
5454
// Try to find the matching secret key for each public key - some public
5555
// keys may not have a match. Some secret keys may be used multiple times,
5656
// which is why public keys are used as the outer loop.
@@ -73,10 +73,7 @@ func addSignatures(txn *types.Transaction, cf types.CoveredFields, uc types.Unlo
7373
txn.TransactionSignatures = append(txn.TransactionSignatures, sig)
7474
sigIndex := len(txn.TransactionSignatures) - 1
7575
sigHash := txn.SigHash(sigIndex)
76-
encodedSig, err := crypto.SignHash(sigHash, spendKey.SecretKeys[j])
77-
if err != nil {
78-
return nil, err
79-
}
76+
encodedSig := crypto.SignHash(sigHash, spendKey.SecretKeys[j])
8077
txn.TransactionSignatures[sigIndex].Signature = encodedSig[:]
8178

8279
// Count that the signature has been added, and break out of the
@@ -91,7 +88,7 @@ func addSignatures(txn *types.Transaction, cf types.CoveredFields, uc types.Unlo
9188
break
9289
}
9390
}
94-
return newSigIndices, nil
91+
return newSigIndices
9592
}
9693

9794
// checkOutput is a helper function used to determine if an output is usable.
@@ -223,10 +220,7 @@ func (tb *transactionBuilder) FundSiacoins(amount types.Currency) error {
223220

224221
// Sign all of the inputs to the parent trancstion.
225222
for _, sci := range parentTxn.SiacoinInputs {
226-
_, err := addSignatures(&parentTxn, types.FullCoveredFields, sci.UnlockConditions, crypto.Hash(sci.ParentID), tb.wallet.keys[sci.UnlockConditions.UnlockHash()])
227-
if err != nil {
228-
return err
229-
}
223+
addSignatures(&parentTxn, types.FullCoveredFields, sci.UnlockConditions, crypto.Hash(sci.ParentID), tb.wallet.keys[sci.UnlockConditions.UnlockHash()])
230224
}
231225
// Mark the parent output as spent. Must be done after the transaction is
232226
// finished because otherwise the txid and output id will change.
@@ -360,10 +354,7 @@ func (tb *transactionBuilder) FundSiafunds(amount types.Currency) error {
360354

361355
// Sign all of the inputs to the parent trancstion.
362356
for _, sfi := range parentTxn.SiafundInputs {
363-
_, err := addSignatures(&parentTxn, types.FullCoveredFields, sfi.UnlockConditions, crypto.Hash(sfi.ParentID), tb.wallet.keys[sfi.UnlockConditions.UnlockHash()])
364-
if err != nil {
365-
return err
366-
}
357+
addSignatures(&parentTxn, types.FullCoveredFields, sfi.UnlockConditions, crypto.Hash(sfi.ParentID), tb.wallet.keys[sfi.UnlockConditions.UnlockHash()])
367358
}
368359

369360
// Add the exact output.
@@ -568,10 +559,7 @@ func (tb *transactionBuilder) Sign(wholeTransaction bool) ([]types.Transaction,
568559
if !ok {
569560
return nil, errors.New("transaction builder added an input that it cannot sign")
570561
}
571-
newSigIndices, err := addSignatures(&tb.transaction, coveredFields, input.UnlockConditions, crypto.Hash(input.ParentID), key)
572-
if err != nil {
573-
return nil, err
574-
}
562+
newSigIndices := addSignatures(&tb.transaction, coveredFields, input.UnlockConditions, crypto.Hash(input.ParentID), key)
575563
tb.transactionSignatures = append(tb.transactionSignatures, newSigIndices...)
576564
tb.signed = true // Signed is set to true after one successful signature to indicate that future signings can cause issues.
577565
}
@@ -581,10 +569,7 @@ func (tb *transactionBuilder) Sign(wholeTransaction bool) ([]types.Transaction,
581569
if !ok {
582570
return nil, errors.New("transaction builder added an input that it cannot sign")
583571
}
584-
newSigIndices, err := addSignatures(&tb.transaction, coveredFields, input.UnlockConditions, crypto.Hash(input.ParentID), key)
585-
if err != nil {
586-
return nil, err
587-
}
572+
newSigIndices := addSignatures(&tb.transaction, coveredFields, input.UnlockConditions, crypto.Hash(input.ParentID), key)
588573
tb.transactionSignatures = append(tb.transactionSignatures, newSigIndices...)
589574
tb.signed = true // Signed is set to true after one successful signature to indicate that future signings can cause issues.
590575
}

types/signatures_test.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -246,18 +246,9 @@ func TestTransactionValidSignatures(t *testing.T) {
246246
sigHash0 := txn.SigHash(0)
247247
sigHash1 := txn.SigHash(1)
248248
sigHash2 := txn.SigHash(2)
249-
sig0, err := crypto.SignHash(sigHash0, sk)
250-
if err != nil {
251-
t.Fatal(err)
252-
}
253-
sig1, err := crypto.SignHash(sigHash1, sk)
254-
if err != nil {
255-
t.Fatal(err)
256-
}
257-
sig2, err := crypto.SignHash(sigHash2, sk)
258-
if err != nil {
259-
t.Fatal(err)
260-
}
249+
sig0 := crypto.SignHash(sigHash0, sk)
250+
sig1 := crypto.SignHash(sigHash1, sk)
251+
sig2 := crypto.SignHash(sigHash2, sk)
261252
txn.TransactionSignatures[0].Signature = sig0[:]
262253
txn.TransactionSignatures[1].Signature = sig1[:]
263254
txn.TransactionSignatures[2].Signature = sig2[:]

types/validtransaction_bench_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ func BenchmarkStandaloneValid(b *testing.B) {
4242
// Transaction must be constructed before signing
4343
for i := 0; i < numSigs; i++ {
4444
sigHash := txn.SigHash(i)
45-
sig0, err := crypto.SignHash(sigHash, sk[i])
46-
if err != nil {
47-
b.Fatal(err)
48-
}
45+
sig0 := crypto.SignHash(sigHash, sk[i])
4946
txn.TransactionSignatures[i].Signature = sig0[:]
5047
}
5148

0 commit comments

Comments
 (0)