planet-nine-app / sessionless

A repo for the sessionless protocol
https://sessionless.org
MIT License
38 stars 10 forks source link

C#: Add more flexibility for signed messages #34

Closed Galacticai closed 4 months ago

Galacticai commented 4 months ago

A signed message does not always have to contain the public key within the object since the key may be retrieved from IVault in some cases

Example:

public interface ISessionless {
//...
// > this uses ISessionless.GetKeys to get the public key in a scene where a signed message has no public key attached to it
public bool VerifySignature(SignedMessage);
//...

SignedMessage

classDiagram
  class SignedMessage {
    +Message: string
    +Signature: MessageSignatureHex
    +constructor(string, MessageSignatureHex)
    +WithKey(string): SignedMessageWithKey
    +WithKey(ECPublicKeyParameters): SignedMessageWithECKey
  }
  class SignedMessageWithKey {
    +PublicKey: string
    +constructor(string, MessageSignatureHex, string)
    +constructor(SignedMessage, string)
  }
  class SignedMessageWithECKey {
    +PublicKey: ECPublicKeyParameters
    +constructor(string, MessageSignatureHex, ECPublicKeyParameters)
    +constructor(SignedMessage, ECPublicKeyParameters)
  }

  SignedMessage <-- SignedMessageWithKey
  SignedMessage <-- SignedMessageWithECKey

Note:

These model classes only ensure format, not correctness. SignedMessageWithKey will make sure that the key is made up of a string of bytes

Note 2:

! This contains breaking changes for SignedMessage since PublicKey property has been removed (use SignedMessageWithKey for identical behavior as the outdated SignedMessage, or SignedMessageWithECKey if you got EC public key objects)

ISessionless

classDiagram
  class ISessionless {
    +Vault: IVault
    +GenerateUUID(): string
    +GenerateKeys(): KeyPairHex
    +GenerateKeysAsync(): Task<`KeyPairHex>
    +GetKeys(): KeypairHex?
    +Sign(string): MessageSignatureHex
    +Sign(string, string): MessageSignatureHex
    +Sign(string, ECPrivateKeyParameters): MessageSignatureHex
    +VerifySignature(SignedMessage): bool
    +VerifySignature(SignedMessageWithKey): bool
    +VerifySignature(SignedMessageWithECKey): bool
    +Associate(SignedMessages[]): bool
  }
  class Sessionless {
    constructor(IVault)
  }
  ISessionless <-- Sessionless

Note:

Sign and VerifySignature functions are chained respectively so the 1st would call return the 2nd, 3rd...

Example: Sign(string) will do work then return Sign(string, string) to continue the process which will return Sign(string, ECPrivateKeyParameters) which will return the final result Same with VerifySignature

The more useful parameters you provide, the less work they have to do (call the 3rd for the least amount of work required by each of them)


Sorry for the amount of commits, I decided to make the PR even tho it contains the merged commits along with the new ones since I spent too many hours for 2days on this and failed to fix it image