Skip to content

Commit

Permalink
add a return error to NewInMemorySigner
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarak Ben Youssef committed May 12, 2022
1 parent 9636824 commit 7c53dc1
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 29 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ and is not limited to in-memory implementations.

```go
// construct a signer from your private key and configured hash algorithm
mySigner := crypto.NewInMemorySigner(myPrivateKey, myAccountKey.HashAlgo)
mySigner, err := crypto.NewInMemorySigner(myPrivateKey, myAccountKey.HashAlgo)
if err != nil {
panic("failed to create a signer")
}

err := tx.SignEnvelope(myAddress, myAccountKey.Index, mySigner)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ tx := flow.NewTransaction().

```go
// 通过你的私钥构造一个签名器,通过哈希算法完成签名
mySigner := crypto.NewInMemorySigner(myPrivateKey, myAccountKey.HashAlgo)
mySigner, err := crypto.NewInMemorySigner(myPrivateKey, myAccountKey.HashAlgo)
if err != nil {
panic("failed to create a signer")
}

err := tx.SignEnvelope(myAddress, myAccountKey.Index, mySigner)
if err != nil {
Expand Down
12 changes: 7 additions & 5 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,13 @@ var _ Signer = (*InMemorySigner)(nil)

// NewInMemorySigner initializes and returns a new in-memory signer with the provided private key
// and hashing algorithm.
func NewInMemorySigner(privateKey PrivateKey, hashAlgo HashAlgorithm) InMemorySigner {
//
// It returns an error if the signature and hashing algorithms are not compatible.
func NewInMemorySigner(privateKey PrivateKey, hashAlgo HashAlgorithm) (InMemorySigner, error) {
// check compatibility to form a signing key
if !CompatibleAlgorithms(privateKey.Algorithm(), hashAlgo) {
// TODO: panic?
return InMemorySigner{}
return InMemorySigner{}, fmt.Errorf("signature algorithm %s and hashing algorithm are incompatible %s",
privateKey.Algorithm(), hashAlgo)
}

// The error is ignored because the hash algorithm is valid at this point
Expand All @@ -107,7 +109,7 @@ func NewInMemorySigner(privateKey PrivateKey, hashAlgo HashAlgorithm) InMemorySi
return InMemorySigner{
PrivateKey: privateKey,
Hasher: hasher,
}
}, nil
}

func (s InMemorySigner) Sign(message []byte) ([]byte, error) {
Expand All @@ -122,7 +124,7 @@ func (s InMemorySigner) PublicKey() PublicKey {
type NaiveSigner = InMemorySigner

// NewNaiveSigner is an alias for NewInMemorySigner.
func NewNaiveSigner(privateKey PrivateKey, hashAlgo HashAlgorithm) NaiveSigner {
func NewNaiveSigner(privateKey PrivateKey, hashAlgo HashAlgorithm) (NaiveSigner, error) {
return NewInMemorySigner(privateKey, hashAlgo)
}

Expand Down
5 changes: 4 additions & 1 deletion docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,10 @@ Signatures can be generated more securely using keys stored in a hardware device
Simple signature example:
```go
// construct a signer from your private key and configured hash algorithm
mySigner := crypto.NewInMemorySigner(myPrivateKey, myAccountKey.HashAlgo)
mySigner, err := crypto.NewInMemorySigner(myPrivateKey, myAccountKey.HashAlgo)
if err != nil {
panic("failed to create a signer")
}

err := tx.SignEnvelope(myAddress, myAccountKey.Index, mySigner)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion examples/deploy_contract/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func DeployContractDemo() {
FromPrivateKey(myPrivateKey).
SetHashAlgo(crypto.SHA3_256).
SetWeight(flow.AccountKeyWeightThreshold)
mySigner := crypto.NewInMemorySigner(myPrivateKey, myAcctKey.HashAlgo)
mySigner, err := crypto.NewInMemorySigner(myPrivateKey, myAcctKey.HashAlgo)
examples.Handle(err)

referenceBlockID := examples.GetReferenceBlockId(flowClient)
createAccountTx := templates.CreateAccount([]*flow.AccountKey{myAcctKey}, nil, serviceAcctAddr)
Expand Down
6 changes: 4 additions & 2 deletions examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ func ServiceAccount(flowClient *client.Client) (flow.Address, *flow.AccountKey,
Handle(err)

accountKey := acc.Keys[0]
signer := crypto.NewInMemorySigner(privateKey, accountKey.HashAlgo)
signer, err := crypto.NewInMemorySigner(privateKey, accountKey.HashAlgo)
Handle(err)
return addr, accountKey, signer
}

Expand Down Expand Up @@ -138,7 +139,8 @@ func RandomAccount(flowClient *client.Client) (flow.Address, *flow.AccountKey, c

account := CreateAccount(flowClient, []*flow.AccountKey{accountKey})
FundAccountInEmulator(flowClient, account.Address, 10.0)
signer := crypto.NewInMemorySigner(privateKey, accountKey.HashAlgo)
signer, err := crypto.NewInMemorySigner(privateKey, accountKey.HashAlgo)
Handle(err)
return account.Address, account.Keys[0], signer
}

Expand Down
3 changes: 2 additions & 1 deletion examples/storage_usage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func StorageUsageDemo() {
SetHashAlgo(crypto.SHA3_256).
SetWeight(flow.AccountKeyWeightThreshold)

keySigner := crypto.NewInMemorySigner(privateKey, key.HashAlgo)
keySigner, err := crypto.NewInMemorySigner(privateKey, key.HashAlgo)
examples.Handle(err)

demoAccount := examples.CreateAccountWithContracts(flowClient,
[]*flow.AccountKey{key}, []templates.Contract{{
Expand Down
6 changes: 4 additions & 2 deletions examples/transaction_signing/multi_party/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,17 @@ func MultiPartySingleSignatureDemo() {
SetHashAlgo(crypto.SHA3_256).
SetWeight(flow.AccountKeyWeightThreshold)

key1Signer := crypto.NewInMemorySigner(privateKey1, key1.HashAlgo)
key1Signer, err := crypto.NewInMemorySigner(privateKey1, key1.HashAlgo)
examples.Handle(err)

key3 := flow.NewAccountKey().
SetPublicKey(privateKey3.PublicKey()).
SetSigAlgo(privateKey3.Algorithm()).
SetHashAlgo(crypto.SHA3_256).
SetWeight(flow.AccountKeyWeightThreshold)

key3Signer := crypto.NewInMemorySigner(privateKey3, key3.HashAlgo)
key3Signer, err := crypto.NewInMemorySigner(privateKey3, key3.HashAlgo)
examples.Handle(err)

account1 := examples.CreateAccount(flowClient, []*flow.AccountKey{key1})
account2 := examples.CreateAccount(flowClient, []*flow.AccountKey{key3})
Expand Down
12 changes: 8 additions & 4 deletions examples/transaction_signing/multi_party_multisig/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,35 @@ func MultiPartyMultiSignatureDemo() {
SetHashAlgo(crypto.SHA3_256).
SetWeight(flow.AccountKeyWeightThreshold / 2)

key1Signer := crypto.NewInMemorySigner(privateKey1, key1.HashAlgo)
key1Signer, err := crypto.NewInMemorySigner(privateKey1, key1.HashAlgo)
examples.Handle(err)

key2 := flow.NewAccountKey().
SetPublicKey(privateKey2.PublicKey()).
SetSigAlgo(privateKey2.Algorithm()).
SetHashAlgo(crypto.SHA3_256).
SetWeight(flow.AccountKeyWeightThreshold / 2)

key2Signer := crypto.NewInMemorySigner(privateKey2, key2.HashAlgo)
key2Signer, err := crypto.NewInMemorySigner(privateKey2, key2.HashAlgo)
examples.Handle(err)

key3 := flow.NewAccountKey().
SetPublicKey(privateKey3.PublicKey()).
SetSigAlgo(privateKey3.Algorithm()).
SetHashAlgo(crypto.SHA3_256).
SetWeight(flow.AccountKeyWeightThreshold / 2)

key3Signer := crypto.NewInMemorySigner(privateKey3, key3.HashAlgo)
key3Signer, err := crypto.NewInMemorySigner(privateKey3, key3.HashAlgo)
examples.Handle(err)

key4 := flow.NewAccountKey().
SetPublicKey(privateKey4.PublicKey()).
SetSigAlgo(privateKey4.Algorithm()).
SetHashAlgo(crypto.SHA3_256).
SetWeight(flow.AccountKeyWeightThreshold / 2)

key4Signer := crypto.NewInMemorySigner(privateKey4, key4.HashAlgo)
key4Signer, err := crypto.NewInMemorySigner(privateKey4, key4.HashAlgo)
examples.Handle(err)

account1 := examples.CreateAccount(flowClient, []*flow.AccountKey{key1, key2})
account2 := examples.CreateAccount(flowClient, []*flow.AccountKey{key3, key4})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,17 @@ func MultiPartySingleSignatureDemo() {
SetHashAlgo(crypto.SHA3_256).
SetWeight(flow.AccountKeyWeightThreshold)

key1Signer := crypto.NewInMemorySigner(privateKey1, key1.HashAlgo)
key1Signer, err := crypto.NewInMemorySigner(privateKey1, key1.HashAlgo)
examples.Handle(err)

key3 := flow.NewAccountKey().
SetPublicKey(privateKey3.PublicKey()).
SetSigAlgo(privateKey3.Algorithm()).
SetHashAlgo(crypto.SHA3_256).
SetWeight(flow.AccountKeyWeightThreshold)

key3Signer := crypto.NewInMemorySigner(privateKey3, key3.HashAlgo)
key3Signer, err := crypto.NewInMemorySigner(privateKey3, key3.HashAlgo)
examples.Handle(err)

account1 := examples.CreateAccount(flowClient, []*flow.AccountKey{key1})
account2 := examples.CreateAccount(flowClient, []*flow.AccountKey{key3})
Expand Down
3 changes: 2 additions & 1 deletion examples/transaction_signing/single_party/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func SinglePartySingleSignatureDemo() {
SetHashAlgo(crypto.SHA3_256).
SetWeight(flow.AccountKeyWeightThreshold)

key1Signer := crypto.NewInMemorySigner(privateKey1, key1.HashAlgo)
key1Signer, err := crypto.NewInMemorySigner(privateKey1, key1.HashAlgo)
examples.Handle(err)

account1 := examples.CreateAccount(flowClient, []*flow.AccountKey{key1})
// Add some flow for the transaction fees
Expand Down
6 changes: 4 additions & 2 deletions examples/transaction_signing/single_party_multisig/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,17 @@ func SinglePartyMultiSignatureDemo() {
SetHashAlgo(crypto.SHA3_256).
SetWeight(flow.AccountKeyWeightThreshold / 2)

key1Signer := crypto.NewInMemorySigner(privateKey1, key1.HashAlgo)
key1Signer, err := crypto.NewInMemorySigner(privateKey1, key1.HashAlgo)
examples.Handle(err)

key2 := flow.NewAccountKey().
SetPublicKey(privateKey2.PublicKey()).
SetSigAlgo(privateKey2.Algorithm()).
SetHashAlgo(crypto.SHA3_256).
SetWeight(flow.AccountKeyWeightThreshold / 2)

key2Signer := crypto.NewInMemorySigner(privateKey2, key2.HashAlgo)
key2Signer, err := crypto.NewInMemorySigner(privateKey2, key2.HashAlgo)
examples.Handle(err)

account1 := examples.CreateAccount(flowClient, []*flow.AccountKey{key1, key2})
// Add some flow for the transaction fees
Expand Down
6 changes: 4 additions & 2 deletions examples/verify_signature/user_signature/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ func UserSignatureDemo() {
message := append(toAddress.Bytes(), fromAddress.Bytes()...)
message = append(message, amount.ToBigEndianBytes()...)

signerAlice := crypto.NewInMemorySigner(privateKeyAlice, crypto.SHA3_256)
signerBob := crypto.NewInMemorySigner(privateKeyBob, crypto.SHA3_256)
signerAlice, err := crypto.NewInMemorySigner(privateKeyAlice, crypto.SHA3_256)
examples.Handle(err)
signerBob, err := crypto.NewInMemorySigner(privateKeyBob, crypto.SHA3_256)
examples.Handle(err)

// sign the message with Alice and Bob
signatureAlice, err := flow.SignUserMessage(signerAlice, message)
Expand Down
6 changes: 4 additions & 2 deletions examples/verify_signature/user_signature_validate_all/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ func UserSignatureValidateAll() {
// create the message that will be signed
message := []byte("ananas")

signerAlice := crypto.NewInMemorySigner(privateKeyAlice, crypto.SHA3_256)
signerBob := crypto.NewInMemorySigner(privateKeyBob, crypto.SHA3_256)
signerAlice, err := crypto.NewInMemorySigner(privateKeyAlice, crypto.SHA3_256)
examples.Handle(err)
signerBob, err := crypto.NewInMemorySigner(privateKeyBob, crypto.SHA3_256)
examples.Handle(err)

// sign the message with Alice and Bob
signatureAlice, err := flow.SignUserMessage(signerAlice, message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ func UserSignatureValidateAny() {
// create the message that will be signed with one key
message := []byte("ananas")

signerAlice := crypto.NewInMemorySigner(privateKeyAlice, crypto.SHA3_256)
signerAlice, err := crypto.NewInMemorySigner(privateKeyAlice, crypto.SHA3_256)
examples.Handle(err)

// sign the message only with Alice
signatureAlice, err := flow.SignUserMessage(signerAlice, message)
Expand Down
9 changes: 8 additions & 1 deletion test/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ func (g *AccountKeys) NewWithSigner() (*flow.AccountKey, crypto.Signer) {
SequenceNumber: 42,
}

return &accountKey, crypto.NewInMemorySigner(privateKey, accountKey.HashAlgo)
// error here is nil since ECDSA_P256 and SHA3_256 are compatible,
// but keeping the error check for sanity
signer, err := crypto.NewInMemorySigner(privateKey, accountKey.HashAlgo)
if err != nil {
panic(err)
}

return &accountKey, signer
}

type Addresses struct {
Expand Down

0 comments on commit 7c53dc1

Please sign in to comment.