-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the ability to deploy and fetch Braavos accounts. Only the seed signer is supported at the moment.
- Loading branch information
1 parent
abc8bf6
commit 331173c
Showing
10 changed files
with
473 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use async_trait::async_trait; | ||
use starknet::{ | ||
accounts::{AccountFactory, PreparedAccountDeployment, RawAccountDeployment}, | ||
core::{crypto::compute_hash_on_elements, types::FieldElement}, | ||
macros::selector, | ||
providers::Provider, | ||
signers::Signer, | ||
}; | ||
|
||
pub struct BraavosAccountFactory<S, P> { | ||
proxy_class_hash: FieldElement, | ||
mock_impl_class_hash: FieldElement, | ||
impl_class_hash: FieldElement, | ||
chain_id: FieldElement, | ||
signer_public_key: FieldElement, | ||
signer: S, | ||
provider: P, | ||
} | ||
|
||
impl<S, P> BraavosAccountFactory<S, P> | ||
where | ||
S: Signer, | ||
{ | ||
pub async fn new( | ||
proxy_class_hash: FieldElement, | ||
mock_impl_class_hash: FieldElement, | ||
impl_class_hash: FieldElement, | ||
chain_id: FieldElement, | ||
signer: S, | ||
provider: P, | ||
) -> Result<Self, S::GetPublicKeyError> { | ||
let signer_public_key = signer.get_public_key().await?; | ||
Ok(Self { | ||
proxy_class_hash, | ||
mock_impl_class_hash, | ||
impl_class_hash, | ||
chain_id, | ||
signer_public_key: signer_public_key.scalar(), | ||
signer, | ||
provider, | ||
}) | ||
} | ||
} | ||
|
||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
impl<S, P> AccountFactory for BraavosAccountFactory<S, P> | ||
where | ||
S: Signer + Sync + Send, | ||
P: Provider + Sync + Send, | ||
{ | ||
type Provider = P; | ||
type SignError = S::SignError; | ||
|
||
fn class_hash(&self) -> FieldElement { | ||
self.proxy_class_hash | ||
} | ||
|
||
fn calldata(&self) -> Vec<FieldElement> { | ||
vec![ | ||
self.mock_impl_class_hash, | ||
selector!("initializer"), | ||
FieldElement::ONE, | ||
self.signer_public_key, | ||
] | ||
} | ||
|
||
fn chain_id(&self) -> FieldElement { | ||
self.chain_id | ||
} | ||
|
||
fn provider(&self) -> &Self::Provider { | ||
&self.provider | ||
} | ||
|
||
async fn sign_deployment( | ||
&self, | ||
deployment: &RawAccountDeployment, | ||
) -> Result<Vec<FieldElement>, Self::SignError> { | ||
let tx_hash = | ||
PreparedAccountDeployment::from_raw(deployment.clone(), self).transaction_hash(); | ||
|
||
let sig_hash = compute_hash_on_elements(&[ | ||
tx_hash, | ||
self.impl_class_hash, | ||
FieldElement::ZERO, | ||
FieldElement::ZERO, | ||
FieldElement::ZERO, | ||
FieldElement::ZERO, | ||
FieldElement::ZERO, | ||
FieldElement::ZERO, | ||
FieldElement::ZERO, | ||
]); | ||
|
||
let signature = self.signer.sign_hash(&sig_hash).await?; | ||
|
||
Ok(vec![ | ||
signature.r, | ||
signature.s, | ||
self.impl_class_hash, | ||
FieldElement::ZERO, | ||
FieldElement::ZERO, | ||
FieldElement::ZERO, | ||
FieldElement::ZERO, | ||
FieldElement::ZERO, | ||
FieldElement::ZERO, | ||
FieldElement::ZERO, | ||
]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.