pub struct Identity { /* private fields */ }
Expand description

An Identity is an abstraction where an Account that you don’t control delegates the ability to sign messages to a new address (encapsulated in the Identity) for a limited amount of time using a signature.

Implementations§

source§

impl Identity

source

pub fn from_json<J: AsRef<str>>(json: J) -> Result<Self, Error>

Creates a new Identity from the given JSON

use dcl_crypto::Identity;

let identity = Identity::from_json(r#"{
  "ephemeralIdentity": {
    "address": "0x84452bbFA4ca14B7828e2F3BBd106A2bD495CD34",
    "publicKey": "0x0420c548d960b06dac035d1daf826472eded46b8b9d123294f1199c56fa235c89f2515158b1e3be0874bfb15b42d1551db8c276787a654d0b8d7b4d4356e70fe42",
    "privateKey": "0xbc453a92d9baeb3d10294cbc1d48ef6738f718fd31b4eb8085efe7b311299399"
  },
  "expiration": "3021-10-16T22:32:29.626Z",
  "authChain": [
    {
      "type": "SIGNER",
      "payload": "0x7949f9f239d1a0816ce5eb364a1f588ae9cc1bf5",
      "signature": ""
    },
    {
      "type": "ECDSA_EPHEMERAL",
      "payload": "Decentraland Login\nEphemeral address: 0x84452bbFA4ca14B7828e2F3BBd106A2bD495CD34\nExpiration: 3021-10-16T22:32:29.626Z",
      "signature": "0x39dd4ddf131ad2435d56c81c994c4417daef5cf5998258027ef8a1401470876a1365a6b79810dc0c4a2e9352befb63a9e4701d67b38007d83ffc4cd2b7a38ad51b"
    }
  ]
 }"#);

assert!(identity.is_ok());
source

pub fn from_signer<S: Signer, E: Into<Expiration>>(signer: &S, exp: E) -> Self

Creates a new Identity from the given Signer

use dcl_crypto::{Identity, Account, Signer, Expiration};

let signer = Account::random();
let identity = Identity::from_signer(&signer, Expiration::try_from("3021-10-16T22:32:29.626Z").unwrap());
let chain = identity.sign_payload("Hello World");

assert_eq!(chain.owner(), Some(&signer.address()));
assert_eq!(chain.len(), 3);
source

pub fn from_identity<E: Into<Expiration>>(identity: &Identity, exp: E) -> Self

Creates a new Identity extended from a given Identity

use dcl_crypto::{Identity, Account, Signer, Expiration};

let signer = Account::random();
let identity1 = Identity::from_signer(&signer, Expiration::try_from("3021-10-16T22:32:29.626Z").unwrap());
let identity2 = Identity::from_identity(&identity1, Expiration::try_from("3021-10-16T22:32:29.626Z").unwrap());
let chain = identity2.sign_payload("Hello World");

assert_eq!(chain.owner(), Some(&signer.address()));
assert_eq!(chain.len(), 4);
source

pub fn create_signature<M: AsRef<str>>(&self, payload: M) -> PersonalSignature

Creates a PersonalSignature for the given payload

use dcl_crypto::Identity;

let identity = Identity::from_json(r#"{
  "ephemeralIdentity": {
    "address": "0x84452bbFA4ca14B7828e2F3BBd106A2bD495CD34",
    "publicKey": "0x0420c548d960b06dac035d1daf826472eded46b8b9d123294f1199c56fa235c89f2515158b1e3be0874bfb15b42d1551db8c276787a654d0b8d7b4d4356e70fe42",
    "privateKey": "0xbc453a92d9baeb3d10294cbc1d48ef6738f718fd31b4eb8085efe7b311299399"
  },
  "expiration": "3021-10-16T22:32:29.626Z",
  "authChain": [
    {
      "type": "SIGNER",
      "payload": "0x7949f9f239d1a0816ce5eb364a1f588ae9cc1bf5",
      "signature": ""
    },
    {
      "type": "ECDSA_EPHEMERAL",
      "payload": "Decentraland Login\nEphemeral address: 0x84452bbFA4ca14B7828e2F3BBd106A2bD495CD34\nExpiration: 3021-10-16T22:32:29.626Z",
      "signature": "0x39dd4ddf131ad2435d56c81c994c4417daef5cf5998258027ef8a1401470876a1365a6b79810dc0c4a2e9352befb63a9e4701d67b38007d83ffc4cd2b7a38ad51b"
    }
  ]
 }"#).unwrap();

assert_eq!(
    identity.create_signature("Hello World!").to_string(),
    "0x33e6b0f71b69d9dca7e25fa279cd70e2e9a44d0c11ab973a936a5b67ba3dbb6554ec2ea5cb4112b2f051dd95b47874c40b8bb7443a0bc4f16c67e2017e0dcb0c1c"
);
source

pub fn sign_payload<M: AsRef<str>>(&self, payload: M) -> AuthChain

Creates an AuthChain signing the the given payload

use dcl_crypto::{Identity, Authenticator, Signer, Account, Expiration};

    let signer = Account::random();
    let identity = Identity::from_signer(&signer, Expiration::try_from("3021-10-16T22:32:29.626Z").unwrap());
    let chain = identity.sign_payload("Hello World!");
    let address = Authenticator::new().verify_signature(&chain, "Hello World!").await.unwrap();
    assert_eq!(address, &signer.address());
})
use dcl_crypto::{Identity, Authenticator, Signer, AuthChain};

let identity = Identity::from_json(r#"{
  "ephemeralIdentity": {
    "address": "0x84452bbFA4ca14B7828e2F3BBd106A2bD495CD34",
    "publicKey": "0x0420c548d960b06dac035d1daf826472eded46b8b9d123294f1199c56fa235c89f2515158b1e3be0874bfb15b42d1551db8c276787a654d0b8d7b4d4356e70fe42",
    "privateKey": "0xbc453a92d9baeb3d10294cbc1d48ef6738f718fd31b4eb8085efe7b311299399"
  },
  "expiration": "3021-10-16T22:32:29.626Z",
  "authChain": [
    {
      "type": "SIGNER",
      "payload": "0x7949f9f239d1a0816ce5eb364a1f588ae9cc1bf5",
      "signature": ""
    },
    {
      "type": "ECDSA_EPHEMERAL",
      "payload": "Decentraland Login\nEphemeral address: 0x84452bbFA4ca14B7828e2F3BBd106A2bD495CD34\nExpiration: 3021-10-16T22:32:29.626Z",
      "signature": "0x39dd4ddf131ad2435d56c81c994c4417daef5cf5998258027ef8a1401470876a1365a6b79810dc0c4a2e9352befb63a9e4701d67b38007d83ffc4cd2b7a38ad51b"
    }
  ]
 }"#).unwrap();


    let chain = identity.sign_payload("Hello World!");
    let address = Authenticator::new().verify_signature(&chain, "Hello World!").await.unwrap();
    assert_eq!(identity.address().to_string(), "0x84452bbfa4ca14b7828e2f3bbd106a2bd495cd34");
    assert_eq!(address.to_string(), "0x7949f9f239d1a0816ce5eb364a1f588ae9cc1bf5");
    assert_eq!(
        chain,
        AuthChain::from_json(r#"[
            {
              "type": "SIGNER",
              "payload": "0x7949f9f239d1a0816ce5eb364a1f588ae9cc1bf5",
              "signature": ""
            },
            {
              "type": "ECDSA_EPHEMERAL",
              "payload": "Decentraland Login\nEphemeral address: 0x84452bbFA4ca14B7828e2F3BBd106A2bD495CD34\nExpiration: 3021-10-16T22:32:29.626Z",
              "signature": "0x39dd4ddf131ad2435d56c81c994c4417daef5cf5998258027ef8a1401470876a1365a6b79810dc0c4a2e9352befb63a9e4701d67b38007d83ffc4cd2b7a38ad51b"
            },
            {
              "type": "ECDSA_SIGNED_ENTITY",
              "payload": "Hello World!",
              "signature": "0x33e6b0f71b69d9dca7e25fa279cd70e2e9a44d0c11ab973a936a5b67ba3dbb6554ec2ea5cb4112b2f051dd95b47874c40b8bb7443a0bc4f16c67e2017e0dcb0c1c"
            }
        ]"#).unwrap()
    );
})

Trait Implementations§

source§

impl Clone for Identity

source§

fn clone(&self) -> Identity

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Identity

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for Identity

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Serialize for Identity

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Signer for Identity

Implements Signer trait for Identity

source§

fn address(&self) -> Address

Returns the address of the ephemeral identity

source§

fn sign<M: AsRef<[u8]>>(&self, message: M) -> PersonalSignature

Signs the given message with the ephemeral identity

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,