Skip to content

Commit

Permalink
Update AuthKeyMultikeyLayout model to include pk prefixes within th…
Browse files Browse the repository at this point in the history
…e layout
  • Loading branch information
0xjunha committed Jan 12, 2025
1 parent f54f614 commit 76ce1aa
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
-- Public key to associated multikey auth keys
CREATE TABLE public_key_auth_keys
(
public_key VARCHAR(132) NOT NULL,
CREATE TABLE public_key_auth_keys (
public_key VARCHAR(200) NOT NULL,
public_key_type VARCHAR(50) NOT NULL,
auth_key VARCHAR(66) NOT NULL,
verified BOOLEAN NOT NULL,
Expand All @@ -14,19 +13,16 @@ CREATE TABLE public_key_auth_keys
);

-- Auth key to its corresponding multikey layout
CREATE TABLE auth_key_multikey_layout
(
auth_key VARCHAR(66) UNIQUE PRIMARY KEY NOT NULL,
CREATE TABLE auth_key_multikey_layout (
auth_key VARCHAR(66) PRIMARY KEY NOT NULL,
signatures_required BIGINT NOT NULL,
multikey_layout jsonb NOT NULL, -- with key type prefixes
multikey_layout_with_prefixes jsonb NOT NULL,
multikey_type VARCHAR(50) NOT NULL
);

-- Auth key to account addresses
CREATE TABLE auth_key_account_addresses (
auth_key VARCHAR(66) NOT NULL,
address VARCHAR(66) NOT NULL,
verified BOOLEAN NOT NULL,
-- Constraints
PRIMARY KEY (address)
);
address VARCHAR(66) PRIMARY KEY NOT NULL,
verified BOOLEAN NOT NULL
);
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

#![allow(clippy::extra_unused_lifetimes)]

use crate::{
db::postgres::models::account_restoration_models::{
auth_key_account_addresses::AuthKeyAccountAddress,
Expand Down Expand Up @@ -133,7 +131,7 @@ impl AuthKeyScheme for MultiKeyAuthKeyScheme {
let mut preimage = vec![total_keys];

Check warning on line 131 in rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs#L130-L131

Added lines #L130 - L131 were not covered by tests

for (key_type, public_key) in self.key_types.iter().zip(&self.public_keys) {
preimage.push(key_type.unwrap().to_u8());
preimage.push(key_type.expect("should not be None").to_u8());
preimage.extend_from_slice(public_key);
}

Check warning on line 136 in rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs#L133-L136

Added lines #L133 - L136 were not covered by tests

Expand Down Expand Up @@ -218,6 +216,15 @@ impl SignatureInfo {
}
}

Check warning on line 217 in rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs#L217

Added line #L217 was not covered by tests

fn multikey_key_types(&self) -> Vec<Option<AnyPublicKeyType>> {
match self {
Self::Ed25519(_) => vec![],
Self::MultiEd25519(info) => vec![None; info.public_keys.len()],
Self::SingleKey(_) => vec![],
Self::MultiKey(info) => info.key_types.clone(),

Check warning on line 224 in rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs#L219-L224

Added lines #L219 - L224 were not covered by tests
}
}

Check warning on line 226 in rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs#L226

Added line #L226 was not covered by tests

fn multikey_threshold(&self) -> Option<u32> {
match self {
Self::Ed25519(_) => None,
Expand Down Expand Up @@ -334,34 +341,57 @@ pub fn parse_account_restoration_models_from_transaction(
};

Check warning on line 341 in rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs#L333-L341

Added lines #L333 - L341 were not covered by tests

let (auth_key_multikey_layout, public_key_auth_keys) = if signature_info.is_multikey() {
let multikey_layout = signature_info
let multikey_layouts = signature_info
.multikey_public_keys()
.iter()
.map(|pk| format!("0x{}", hex::encode(pk)))
.zip(signature_info.multikey_key_types().iter())
.map(|(pk, prefix)| {
let pk_with_prefix = prefix.map_or_else(
|| pk.clone(),
|key_type| {
let mut extended = vec![key_type.to_u8()]; // Public key type prefix
extended.extend(pk);
extended
},
);
format!("0x{}", hex::encode(pk_with_prefix))
})
.collect::<Vec<_>>();

Check warning on line 359 in rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs#L343-L359

Added lines #L343 - L359 were not covered by tests

let multikey_pk_types = match signature_info {
SignatureInfo::MultiEd25519(_) => vec![String::from("ed25519"); multikey_layout.len()],
SignatureInfo::MultiKey(ref scheme) => scheme
let multikey_pk_types = match &signature_info {

Check warning on line 361 in rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs#L361

Added line #L361 was not covered by tests
SignatureInfo::MultiEd25519(_) => {
vec![String::from("ed25519"); multikey_layouts.len()]

Check warning on line 363 in rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs#L363

Added line #L363 was not covered by tests
},
SignatureInfo::MultiKey(scheme) => scheme
.key_types
.iter()
.filter_map(Option::as_ref)
.map(AnyPublicKeyType::key_type_string)
.map(|maybe_key_type| match maybe_key_type {
Some(key_type) => key_type.key_type_string(),
None => String::new(),
})
.collect(),
_ => vec![],

Check warning on line 373 in rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs#L365-L373

Added lines #L365 - L373 were not covered by tests
};

let multikey_verified = signature_info.multikey_verified();
let multikey_threshold = signature_info.multikey_threshold();

Check warning on line 377 in rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs#L376-L377

Added lines #L376 - L377 were not covered by tests

let multikey_layout_with_prefixes = match serde_json::to_value(&multikey_layouts) {
Ok(value) => value,

Check warning on line 380 in rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs#L379-L380

Added lines #L379 - L380 were not covered by tests
Err(_) => {
return Some((auth_key_account_address, vec![], None));

Check warning on line 382 in rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs

View check run for this annotation

Codecov / codecov/patch

rust/processor/src/db/postgres/models/account_restoration_models/account_restoration_utils.rs#L382

Added line #L382 was not covered by tests
},
};

let mut public_key_auth_keys = vec![];
for ((pk, pk_type), verified) in multikey_layout
for ((pk, pk_type), verified) in signature_info
.multikey_public_keys()
.iter()
.zip(multikey_pk_types.iter())
.zip(multikey_verified.iter())
{
public_key_auth_keys.push(PublicKeyAuthKey {
public_key: pk.clone(),
public_key: format!("0x{}", hex::encode(pk)),
public_key_type: pk_type.clone(),
auth_key: auth_key.clone(),
verified: *verified,
Expand All @@ -371,8 +401,8 @@ pub fn parse_account_restoration_models_from_transaction(
(
Some(AuthKeyMultikeyLayout {
auth_key: auth_key.clone(),
signatures_required: multikey_threshold.unwrap() as i64,
multikey_layout: serde_json::to_value(multikey_layout.clone()).unwrap(),
signatures_required: multikey_threshold.expect("should not be None") as i64,
multikey_layout_with_prefixes,
multikey_type: signature_info.signature_type_string(),
}),
public_key_auth_keys,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

#![allow(clippy::extra_unused_lifetimes)]

use crate::schema::auth_key_account_addresses::{self};
use field_count::FieldCount;
use serde::{Deserialize, Serialize};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

#![allow(clippy::extra_unused_lifetimes)]

use crate::schema::auth_key_multikey_layout::{self};
use field_count::FieldCount;
use serde::{Deserialize, Serialize};
Expand All @@ -13,6 +11,6 @@ use serde::{Deserialize, Serialize};
pub struct AuthKeyMultikeyLayout {
pub auth_key: String,
pub signatures_required: i64,
pub multikey_layout: serde_json::Value, // TODO: this should include the type prefixes
pub multikey_layout_with_prefixes: serde_json::Value,
pub multikey_type: String,
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

#![allow(clippy::extra_unused_lifetimes)]

use crate::schema::public_key_auth_keys::{self};
use field_count::FieldCount;
use serde::{Deserialize, Serialize};
Expand Down
4 changes: 2 additions & 2 deletions rust/processor/src/db/postgres/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ diesel::table! {
#[max_length = 66]
auth_key -> Varchar,
signatures_required -> Int8,
multikey_layout -> Jsonb,
multikey_layout_with_prefixes -> Jsonb,
#[max_length = 50]
multikey_type -> Varchar,
}
Expand Down Expand Up @@ -989,7 +989,7 @@ diesel::table! {

diesel::table! {
public_key_auth_keys (public_key, public_key_type, auth_key) {
#[max_length = 132]
#[max_length = 200]
public_key -> Varchar,
#[max_length = 50]
public_key_type -> Varchar,
Expand Down

0 comments on commit 76ce1aa

Please sign in to comment.