@@ -59,6 +59,29 @@ void LocalIdentity::printTo(Stream& s) const {
5959 s.print (" prv_key: " ); Utils::printHex (s, prv_key, PRV_KEY_SIZE); s.println ();
6060}
6161
62+ size_t LocalIdentity::writeTo (uint8_t * dest, size_t max_len) {
63+ if (max_len < PRV_KEY_SIZE) return 0 ; // not big enough
64+
65+ if (max_len < PRV_KEY_SIZE + PUB_KEY_SIZE) { // only room for prv_key
66+ memcpy (dest, prv_key, PRV_KEY_SIZE);
67+ return PRV_KEY_SIZE;
68+ }
69+ memcpy (dest, prv_key, PRV_KEY_SIZE); // otherwise can fit prv + pub keys
70+ memcpy (&dest[PRV_KEY_SIZE], pub_key, PUB_KEY_SIZE);
71+ return PRV_KEY_SIZE + PUB_KEY_SIZE;
72+ }
73+
74+ void LocalIdentity::readFrom (const uint8_t * src, size_t len) {
75+ if (len == PRV_KEY_SIZE + PUB_KEY_SIZE) { // has prv + pub keys
76+ memcpy (prv_key, src, PRV_KEY_SIZE);
77+ memcpy (pub_key, &src[PRV_KEY_SIZE], PUB_KEY_SIZE);
78+ } else if (len == PRV_KEY_SIZE) {
79+ memcpy (prv_key, src, PRV_KEY_SIZE);
80+ // now need to re-calculate the pub_key
81+ ed25519_derive_pub (pub_key, prv_key);
82+ }
83+ }
84+
6285void LocalIdentity::sign (uint8_t * sig, const uint8_t * message, int msg_len) const {
6386 ed25519_sign (sig, message, msg_len, pub_key, prv_key);
6487}
0 commit comments