func (c *CommitChain) Sign(privateKey []byte) error { sig, err := primitives.SignSignable(privateKey, c) if err != nil { return err } if c.Sig == nil { c.Sig = new(primitives.ByteSlice64) } err = c.Sig.UnmarshalBinary(sig) if err != nil { return err } pub, err := primitives.PrivateKeyToPublicKey(privateKey) if err != nil { return err } if c.ECPubKey == nil { c.ECPubKey = new(primitives.ByteSlice32) } err = c.ECPubKey.UnmarshalBinary(pub) if err != nil { return err } return nil }
func TestSignature(t *testing.T) { pub, priv, err := ed25519.GenerateKey(rand.Reader) if err != nil { t.Error(err) } t.Logf("priv, pub - %x, %x", *priv, *pub) testData := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07} sig := ed25519.Sign(priv, testData) ok := ed25519.Verify(pub, testData, sig) if ok == false { t.Error("Signature could not be verified") } pub2, err := primitives.PrivateKeyToPublicKey(priv[:]) if err != nil { t.Error(err) } t.Logf("pub1 - %x", pub) t.Logf("pub2 - %x", pub2) if primitives.AreBytesEqual(pub[:], pub2[:]) == false { t.Error("Public keys are not equal") } }