|
| 1 | +// Copyright 2022 Coinbase, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package keys |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + |
| 22 | + "github.com/coinbase/rosetta-sdk-go/types" |
| 23 | +) |
| 24 | + |
| 25 | +var signerBls12381 Signer |
| 26 | + |
| 27 | +func init() { |
| 28 | + bls12381Keypair, _ := GenerateKeypair(types.Bls12381) |
| 29 | + signerBls12381, _ = bls12381Keypair.Signer() |
| 30 | + |
| 31 | + unsignedTxStr := "a7ca4bce10200d073ef10c46e9d27c3b4e31263d4c07fbec447650fcc1b286" + |
| 32 | + "300e8ecf25c0560f9cb5aa673247fb6a6f95fb73164d1bf5d41288f57ea40517d9da86c44a289ed" + |
| 33 | + "2b6f9d3bf9a750ee96dbf905073f8ae56c80100ef47f5585acf70baff8c72c3b8a833181fb3edf4" + |
| 34 | + "328fccd5bb71183532bff220ba46c268991a3ff07eb358e8255a65c30a2dce0e5fbb" |
| 35 | + txnBytes = []byte(unsignedTxStr) |
| 36 | +} |
| 37 | + |
| 38 | +func TestSignBls12381(t *testing.T) { |
| 39 | + type payloadTest struct { |
| 40 | + payload *types.SigningPayload |
| 41 | + err bool |
| 42 | + errMsg error |
| 43 | + } |
| 44 | + |
| 45 | + var payloadTests = []payloadTest{ |
| 46 | + {mockPayload(txnBytes, types.BlsG2Element), false, nil}, |
| 47 | + {mockPayload(txnBytes, ""), false, nil}, |
| 48 | + {mockPayload(txnBytes, types.Ecdsa), true, ErrSignUnsupportedPayloadSignatureType}, |
| 49 | + { |
| 50 | + mockPayload(txnBytes, types.EcdsaRecovery), |
| 51 | + true, |
| 52 | + ErrSignUnsupportedPayloadSignatureType, |
| 53 | + }, |
| 54 | + } |
| 55 | + for _, test := range payloadTests { |
| 56 | + signature, err := signerBls12381.Sign(test.payload, types.BlsG2Element) |
| 57 | + |
| 58 | + if !test.err { |
| 59 | + assert.NoError(t, err) |
| 60 | + assert.Len(t, signature.Bytes, 96) |
| 61 | + assert.Equal(t, signerBls12381.PublicKey(), signature.PublicKey) |
| 62 | + } else { |
| 63 | + assert.Contains(t, err.Error(), test.errMsg.Error()) |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestVerifyBls(t *testing.T) { |
| 69 | + type signatureTest struct { |
| 70 | + signature *types.Signature |
| 71 | + errMsg error |
| 72 | + } |
| 73 | + |
| 74 | + payload := mockPayload(txnBytes, types.BlsG2Element) |
| 75 | + testSignature, err := signerBls12381.Sign(payload, types.BlsG2Element) |
| 76 | + assert.NoError(t, err) |
| 77 | + |
| 78 | + simpleBytes := make([]byte, 32) |
| 79 | + copy(simpleBytes, "hello") |
| 80 | + |
| 81 | + var signatureTests = []signatureTest{ |
| 82 | + {mockSignature( |
| 83 | + types.Ecdsa, |
| 84 | + signerBls12381.PublicKey(), |
| 85 | + txnBytes, |
| 86 | + simpleBytes), ErrVerifyUnsupportedPayloadSignatureType}, |
| 87 | + {mockSignature( |
| 88 | + types.EcdsaRecovery, |
| 89 | + signerBls12381.PublicKey(), |
| 90 | + txnBytes, |
| 91 | + simpleBytes), ErrVerifyUnsupportedPayloadSignatureType}, |
| 92 | + {mockSignature( |
| 93 | + types.BlsG2Element, |
| 94 | + signerBls12381.PublicKey(), |
| 95 | + simpleBytes, |
| 96 | + testSignature.Bytes), ErrVerifyFailed}, |
| 97 | + } |
| 98 | + |
| 99 | + for _, test := range signatureTests { |
| 100 | + err := signerBls12381.Verify(test.signature) |
| 101 | + assert.Contains(t, err.Error(), test.errMsg.Error()) |
| 102 | + } |
| 103 | + |
| 104 | + // happy path |
| 105 | + goodSignature := mockSignature( |
| 106 | + types.BlsG2Element, |
| 107 | + signerBls12381.PublicKey(), |
| 108 | + txnBytes, |
| 109 | + testSignature.Bytes, |
| 110 | + ) |
| 111 | + |
| 112 | + assert.Equal(t, nil, signerBls12381.Verify(goodSignature)) |
| 113 | +} |
0 commit comments