/// <summary> /// Implement the signing algorithm. In the case of an Ed25519 /// it will use the private key to sign the transaction and /// return immediately. In the case of the callback method, it /// will pass the invoice to the async method and async await /// for the method to return. /// </summary> /// <param name="invoice"> /// The information for the transaction, including the Transaction /// ID, Memo and serialized bytes of the crypto transfers and other /// embedded information making up the transaction. /// </param> /// <returns></returns> async Task ISignatory.SignAsync(IInvoice invoice) { switch (_type) { case Type.Ed25519: var ed25519Key = (Key)_data; invoice.AddSignature( KeyType.Ed25519, ed25519Key.PublicKey.Export(KeyBlobFormat.PkixPublicKey).TakeLast(32).Take(6).ToArray(), SignatureAlgorithm.Ed25519.Sign(ed25519Key, invoice.TxBytes.Span)); break; case Type.List: foreach (ISignatory signer in (Signatory[])_data) { await signer.SignAsync(invoice); } break; case Type.Callback: await((Func <IInvoice, Task>)_data)(invoice); break; case Type.OtherSigner: await((ISignatory)_data).SignAsync(invoice); break; default: throw new InvalidOperationException("Not a presently supported Signatory key type, please consider the callback signatory as an alternative."); } }
/// <summary> /// Implement the signing algorithm. In the case of an Ed25519 /// it will use the private key to sign the transaction and /// return immediately. In the case of the callback method, it /// will pass the invoice to the async method and async await /// for the method to return. /// </summary> /// <param name="invoice"> /// The information for the transaction, including the Transaction /// ID, Memo and serialized bytes of the crypto transfers and other /// embedded information making up the transaction. /// </param> /// <returns></returns> async Task ISignatory.SignAsync(IInvoice invoice) { switch (_type) { case Type.Ed25519: var privateKey = (Ed25519PrivateKeyParameters)_data; var ed25519Signer = new Ed25519Signer(); ed25519Signer.Init(true, privateKey); ed25519Signer.BlockUpdate(invoice.TxBytes.ToArray(), 0, invoice.TxBytes.Length); invoice.AddSignature( KeyType.Ed25519, privateKey.GeneratePublicKey().GetEncoded()[..6],
internal static void Sign(IInvoice invoice, Ed25519PrivateKeyParameters privateKey) { var ed25519Signer = new Ed25519Signer(); ed25519Signer.Init(true, privateKey); ed25519Signer.BlockUpdate(invoice.TxBytes.ToArray(), 0, invoice.TxBytes.Length); var signature = ed25519Signer.GenerateSignature(); ed25519Signer.Reset(); var publicKey = privateKey.GeneratePublicKey().GetEncoded(); var prefix = new ReadOnlyMemory <byte>(publicKey, 0, Math.Min(Math.Max(6, invoice.MinimumDesiredPrefixSize), publicKey.Length)); invoice.AddSignature(KeyType.Ed25519, prefix, signature); }
internal static void Sign(IInvoice invoice, ECPrivateKeyParameters privateKey) { var digest = new KeccakDigest(256); digest.BlockUpdate(invoice.TxBytes.ToArray(), 0, invoice.TxBytes.Length); var hash = new byte[digest.GetByteLength()]; digest.DoFinal(hash, 0); var signer = new ECDsaSigner(new HMacDsaKCalculator(new Sha256Digest())); signer.Init(true, privateKey); var components = signer.GenerateSignature(hash); var encoded = new byte[64]; insert256Int(components[0], 0, encoded); insert256Int(components[1], 32, encoded); var publicKey = domain.G.Multiply(privateKey.D).GetEncoded(true); var prefix = new ReadOnlyMemory <byte>(publicKey, 0, Math.Min(Math.Max(6, invoice.MinimumDesiredPrefixSize), publicKey.Length)); invoice.AddSignature(KeyType.ECDSASecp256K1, prefix, encoded); }