spacemeshos / svm

SVM - Spacemesh Virtual Machine
https://spacemesh.io
MIT License
85 stars 14 forks source link

Exend the `svm-codec` to support `Signatures` #483

Open YaronWittenstein opened 2 years ago

YaronWittenstein commented 2 years ago

Once we have a binary Transaction, we'd like to be able to sign it.

The algorithms and Signatures Schemes would vary between different Templates. Not only that, but the ABI used for the sigdata can also vary between Templates. Finally, the sigdata will be appended to the binary Transaction.

Transaction Binary Format

+----------------------+
|       Envelope       |
+----------------------+
|       Message        |
+----------------------+
|      Signatures      |
+----------------------+

Implementation Proposal

pub trait Signer {
  type Params;

  fn sign(&self, tx: &[u8], params: &Self::Params) -> Vec<u8>;
}

// EdDSA is one example of an Algorithm to sign the Transaction digitally.
pub struct EdDSA {
  hasher: Hasher,
  config: Config
}

impl EdDSA { 
  pub fn new(hasher: Hasher, config: Config) -> Self {
    Self { hasher, config }
  }
}

impl SignAlgorithm for EdDSA {
  type Params = EdsaParams;

  fn sign(&self, tx: &[u8], params: &EdsaParams) -> Vec<u8> {
    // ...
  }
}

Example

let tx: &[u8] = ...

let params1 = EdsaParams::new(private_key1);
let sig1 = svm_codec::sign<EdDSA>(tx, &params1);

let params2 = EdsaParams::new(private_key2);
let sig2 = svm_codec::sign<EdDSA>(tx, &params2);

// Concatenate the given signatures to form the `sigdata`.
// Different Templates could apply different encodings for the `sigdata`.
// That said, most will end up just concatening the signatures.
let sigdata = svm_codec::concat_sigs(&[sig1, sig2]);

/// Append the `sigdata` to the transaction
/// Returning the fully formed transaction (`envelope`, `message` and `sigdata`)
let tx = svm_codec::append_sigs(&sigdata);