Skip to content

Commit

Permalink
Merge pull request #142 from pshenmic/feat/identity-aliases
Browse files Browse the repository at this point in the history
Add DPNS names (identity aliases)
  • Loading branch information
pshenmic authored Jun 4, 2024
2 parents c419f17 + 961259a commit 73a509a
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ ___

### Indexer

Verify `packages/indexer/.env` with your PostgreSQL credentials and Tenderdash RPC URL, then do:
Verify `packages/indexer/.env` with your PostgreSQL credentials and Core RPC URL, Tenderdash RPC URL, then do:
```bash
cd packages/indexer
cargo run
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/utils/drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { getKnex } = require('../../src/utils')

const knex = getKnex()

const tables = ['transfers', 'documents', 'identities', 'data_contracts', 'state_transitions', 'blocks', 'validators', 'refinery_schema_history']
const tables = ['transfers', 'documents', 'identity_aliases', 'identities', 'data_contracts', 'state_transitions', 'blocks', 'validators', 'refinery_schema_history']

const sql = tables.reduce((acc, table) => acc + `DROP TABLE IF EXISTS ${table};`, '')

Expand Down
7 changes: 7 additions & 0 deletions packages/indexer/migrations/V39__add_identity_aliases.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE identity_aliases (
id SERIAL PRIMARY KEY,
identity_identifier varchar(44) NOT NULL,
alias varchar(64) NOT NULL
);

CREATE UNIQUE INDEX identity_aliases_alias ON identity_aliases(alias);
25 changes: 23 additions & 2 deletions packages/indexer/src/entities/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use dpp::state_transition::identity_update_transition::accessors::IdentityUpdate
use dpp::state_transition::identity_update_transition::IdentityUpdateTransition;
use dashcore_rpc::{Auth, Client, RpcApi};
use dpp::dashcore::{Txid};
use dpp::platform_value::string_encoding::Encoding::Base58;
use tokio_postgres::Row;

#[derive(Clone)]
pub struct Identity {
Expand All @@ -17,7 +19,7 @@ pub struct Identity {
pub owner: Identifier,
pub revision: Revision,
pub balance: Option<u64>,
pub is_system: bool
pub is_system: bool,
}

impl From<IdentityCreateTransition> for Identity {
Expand Down Expand Up @@ -92,7 +94,26 @@ impl From<SystemDataContract> for Identity {
revision: 0,
balance: None,
is_system: true,
}
};
}
}

impl From<Row> for Identity {
fn from(row: Row) -> Self {
let id: i32 = row.get(0);
let owner: String = row.get(1);
let identifier: String = row.get(2);
let revision: i32 = row.get(3);
let is_system: bool = row.get(4);

return Identity {
id: Some(id as u32),
owner: Identifier::from_string(&owner.trim(), Base58).unwrap(),
revision: Revision::from(revision as u64),
identifier: Identifier::from_string(&identifier.trim(), Base58).unwrap(),
is_system,
balance: None,
};
}
}

37 changes: 37 additions & 0 deletions packages/indexer/src/processor/psql/dao/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,23 @@ impl PostgresDAO {
Ok(())
}

pub async fn create_identity_alias(&self, identity: Identity, alias: String) -> Result<(), PoolError> {
let client = self.connection_pool.get().await.unwrap();

let query = "INSERT INTO identity_aliases(identity_identifier,alias) VALUES ($1, $2);";

let stmt = client.prepare_cached(query).await.unwrap();

client.query(&stmt, &[
&identity.identifier.to_string(Base58),
&alias,
]).await.unwrap();

println!("Created Identity Alias {} -> {}", identity.identifier.to_string(Base58), alias);

Ok(())
}

pub async fn create_transfer(&self, transfer: Transfer, st_hash: String) -> Result<(), PoolError> {
let amount = transfer.amount as i64;

Expand Down Expand Up @@ -295,6 +312,26 @@ impl PostgresDAO {
Ok(validators.first().cloned())
}

pub async fn get_identity_by_identifier(&self, identifier: String) -> Result<Option<Identity>, PoolError> {
let client = self.connection_pool.get().await?;

let stmt = client.prepare_cached("SELECT id, owner, identifier, revision, \
is_system FROM identities where identifier = $1 LIMIT 1;")
.await.unwrap();

let rows: Vec<Row> = client.query(&stmt, &[
&identifier
]).await.unwrap();

let identities: Vec<Identity> = rows
.into_iter()
.map(|row| {
row.into()
}).collect::<Vec<Identity>>();

Ok(identities.first().cloned())
}


pub async fn create_validator(&self, validator: Validator) -> Result<(), PoolError> {
let client = self.connection_pool.get().await.unwrap();
Expand Down
45 changes: 43 additions & 2 deletions packages/indexer/src/processor/psql/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod dao;

use std::convert::identity;
use std::num::ParseIntError;
use dpp::state_transition::{StateTransition, StateTransitionLike};
use deadpool_postgres::{PoolError};
Expand All @@ -8,18 +9,21 @@ use crate::processor::psql::dao::PostgresDAO;
use base64::{Engine as _, engine::{general_purpose}};
use data_contracts::SystemDataContract;
use dpp::identifier::Identifier;
use dpp::platform_value::{platform_value, BinaryData};
use dpp::platform_value::{platform_value, BinaryData, Value};
use dpp::platform_value::btreemap_extensions::BTreeValueMapPathHelper;
use dpp::platform_value::string_encoding::Encoding::Base58;
use dpp::serialization::PlatformSerializable;
use dpp::state_transition::documents_batch_transition::accessors::DocumentsBatchTransitionAccessorsV0;
use dpp::state_transition::documents_batch_transition::{DocumentsBatchTransition};
use sha256::digest;
use dpp::state_transition::data_contract_update_transition::DataContractUpdateTransition;
use dpp::state_transition::documents_batch_transition::document_transition::DocumentTransitionV0Methods;
use dpp::state_transition::identity_create_transition::IdentityCreateTransition;
use dpp::state_transition::identity_credit_transfer_transition::IdentityCreditTransferTransition;
use dpp::state_transition::identity_credit_withdrawal_transition::IdentityCreditWithdrawalTransition;
use dpp::state_transition::identity_topup_transition::IdentityTopUpTransition;
use dpp::state_transition::identity_update_transition::IdentityUpdateTransition;
use dpp::util::json_value::JsonValueExt;
use crate::decoder::decoder::StateTransitionDecoder;
use crate::entities::block::Block;
use crate::entities::data_contract::DataContract;
Expand Down Expand Up @@ -85,6 +89,43 @@ impl PSQLProcessor {

for (_, document_transition) in transitions.iter().enumerate() {
let document = Document::from(document_transition.clone());
let document_type = document_transition.document_type_name();

if document_type == "domain" && document_transition.data_contract_id() == SystemDataContract::DPNS.id() {
let label = document_transition
.data()
.unwrap()
.get_str_at_path("label")
.unwrap();

let normalizedParentDomainName = document_transition
.data()
.unwrap()
.get_str_at_path("normalizedParentDomainName")
.unwrap();

let primary_alias = document_transition
.data()
.unwrap()
.get_optional_at_path("records.dashUniqueIdentityId").unwrap();

let identity_identifier = match primary_alias {
None => {
document_transition
.data()
.unwrap()
.get_optional_at_path("records.dashAliasIdentityId").unwrap()
.expect("Could not find dashAliasIdentityId")
}
Some(value) => value
};

let identity_identifier = Identifier::from_bytes(&identity_identifier.clone().into_identifier_bytes().unwrap()).unwrap().to_string(Base58);
let identity = self.dao.get_identity_by_identifier(identity_identifier.clone()).await.unwrap().expect(&format!("Could not find identity with identifier {}", identity_identifier));
let alias = format!("{}.{}", label, normalizedParentDomainName);

self.dao.create_identity_alias(identity, alias).await.unwrap();
}


self.dao.create_document(document, Some(st_hash.clone())).await.unwrap();
Expand All @@ -97,7 +138,7 @@ impl PSQLProcessor {
id: None,
sender: None,
recipient: Some(identity.identifier),
amount: identity.balance.expect("Balance missing from identity")
amount: identity.balance.expect("Balance missing from identity"),
};

self.dao.create_identity(identity, Some(st_hash.clone())).await.unwrap();
Expand Down

0 comments on commit 73a509a

Please sign in to comment.