Skip to content

Commit

Permalink
add bip39_entropy as alternative to mnemonic for signer-service (#993)
Browse files Browse the repository at this point in the history
* add bip39_entropy as alternative to mnemonic for signer-service

Co-authored-by: Nick Santana <nick@mobilecoin.com>
  • Loading branch information
holtzman and nick-mobilecoin authored May 29, 2024
1 parent 81af297 commit 23f7d2e
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ __pycache__

# Python/Poetry projects
.venv/
env/
venv/

# Node
node_modules/
Expand Down
91 changes: 73 additions & 18 deletions signer/src/service/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,86 @@ fn signer_service_api_inner(command: JsonCommandRequest) -> Result<JsonCommandRe
account_info,
}
}
JsonCommandRequest::get_account { mnemonic } => {
let account_info = service::get_account(&mnemonic)?;
JsonCommandResponse::get_account { account_info }
}
JsonCommandRequest::get_account {
mnemonic,
bip39_entropy,
} => match (mnemonic, bip39_entropy) {
(Some(mnemonic), None) => {
let account_info = service::get_account_by_mnemonic(&mnemonic)?;
JsonCommandResponse::get_account { account_info }
}
(None, Some(bip39_entropy)) => {
let account_info = service::get_account_by_bip39_entropy(&bip39_entropy)?;
JsonCommandResponse::get_account { account_info }
}
(None, None) => {
return Err(anyhow!("Either mnemonic or bip39_entropy must be provided"));
}
_ => {
return Err(anyhow!(
"Only one of mnemonic or bip39_entropy can be provided"
))
}
},
JsonCommandRequest::sign_tx {
mnemonic,
bip39_entropy,
unsigned_tx_proposal,
} => {
let signed_tx = service::sign_tx(
&mnemonic,
(&unsigned_tx_proposal)
.try_into()
.map_err(|e: String| anyhow!(e))?,
)?;
JsonCommandResponse::sign_tx {
tx_proposal: (&signed_tx).try_into().map_err(|e: String| anyhow!(e))?,
} => match (mnemonic, bip39_entropy) {
(Some(mnemonic), None) => {
let signed_tx = service::sign_tx_with_mnemonic(
&mnemonic,
(&unsigned_tx_proposal)
.try_into()
.map_err(|e: String| anyhow!(e))?,
)?;
JsonCommandResponse::sign_tx {
tx_proposal: (&signed_tx).try_into().map_err(|e: String| anyhow!(e))?,
}
}
}
(None, Some(bip39_entropy)) => {
let signed_tx = service::sign_tx_with_bip39_entropy(
&bip39_entropy,
(&unsigned_tx_proposal)
.try_into()
.map_err(|e: String| anyhow!(e))?,
)?;
JsonCommandResponse::sign_tx {
tx_proposal: (&signed_tx).try_into().map_err(|e: String| anyhow!(e))?,
}
}
(None, None) => {
return Err(anyhow!("Either mnemonic or bip39_entropy must be provided"));
}
_ => {
return Err(anyhow!(
"Only one of mnemonic or bip39_entropy can be provided"
))
}
},
JsonCommandRequest::sync_txos {
mnemonic,
bip39_entropy,
txos_unsynced,
} => {
let txos_synced = service::sync_txos(&mnemonic, txos_unsynced)?;
JsonCommandResponse::sync_txos { txos_synced }
}
} => match (mnemonic, bip39_entropy) {
(Some(mnemonic), None) => {
let txos_synced = service::sync_txos_by_mnemonic(&mnemonic, txos_unsynced)?;
JsonCommandResponse::sync_txos { txos_synced }
}
(None, Some(bip39_entropy)) => {
let txos_synced =
service::sync_txos_by_bip39_entropy(&bip39_entropy, txos_unsynced)?;
JsonCommandResponse::sync_txos { txos_synced }
}
(None, None) => {
return Err(anyhow!("Either mnemonic or bip39_entropy must be provided"));
}
_ => {
return Err(anyhow!(
"Only one of mnemonic or bip39_entropy can be provided"
))
}
},
};

Ok(response)
Expand Down
9 changes: 6 additions & 3 deletions signer/src/service/api/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ impl TryFrom<&JsonRPCRequest> for JsonCommandRequest {
pub enum JsonCommandRequest {
create_account {},
get_account {
mnemonic: String,
mnemonic: Option<String>,
bip39_entropy: Option<String>,
},
sign_tx {
mnemonic: String,
mnemonic: Option<String>,
bip39_entropy: Option<String>,
unsigned_tx_proposal: UnsignedTxProposal,
},
sync_txos {
mnemonic: String,
mnemonic: Option<String>,
bip39_entropy: Option<String>,
txos_unsynced: Vec<TxoUnsynced>,
},
}
49 changes: 45 additions & 4 deletions signer/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,19 @@ pub fn create_account() -> (Mnemonic, AccountInfo) {
(mnemonic, account_info)
}

pub fn get_account(mnemonic: &str) -> Result<AccountInfo> {
pub fn get_account_by_mnemonic(mnemonic: &str) -> Result<AccountInfo> {
let mnemonic = Mnemonic::from_phrase(mnemonic, Language::English)?;
get_account(mnemonic)
}

pub fn get_account_by_bip39_entropy(bip39_entropy: &str) -> Result<AccountInfo> {
let mut entropy = [0u8; 32];
hex::decode_to_slice(bip39_entropy, &mut entropy)?;
let mnemonic = Mnemonic::from_entropy(&entropy, Language::English)?;
get_account(mnemonic)
}

fn get_account(mnemonic: Mnemonic) -> Result<AccountInfo> {
let account = get_account_from_mnemonic(mnemonic);

Ok(AccountInfo {
Expand All @@ -34,9 +45,22 @@ pub fn get_account(mnemonic: &str) -> Result<AccountInfo> {
account_index: 0,
})
}

pub fn sync_txos(mnemonic: &str, txos: Vec<TxoUnsynced>) -> Result<Vec<TxoSynced>> {
pub fn sync_txos_by_mnemonic(mnemonic: &str, txos: Vec<TxoUnsynced>) -> Result<Vec<TxoSynced>> {
let mnemonic = Mnemonic::from_phrase(mnemonic, Language::English)?;
sync_txos(mnemonic, txos)
}

pub fn sync_txos_by_bip39_entropy(
bip39_entropy: &str,
txos: Vec<TxoUnsynced>,
) -> Result<Vec<TxoSynced>> {
let mut entropy = [0u8; 32];
hex::decode_to_slice(bip39_entropy, &mut entropy)?;
let mnemonic = Mnemonic::from_entropy(&entropy, Language::English)?;
sync_txos(mnemonic, txos)
}

pub fn sync_txos(mnemonic: Mnemonic, txos: Vec<TxoUnsynced>) -> Result<Vec<TxoSynced>> {
let account = get_account_from_mnemonic(mnemonic);

let mut synced: Vec<TxoSynced> = Vec::new();
Expand All @@ -56,8 +80,25 @@ pub fn sync_txos(mnemonic: &str, txos: Vec<TxoUnsynced>) -> Result<Vec<TxoSynced
Ok(synced)
}

pub fn sign_tx(mnemonic: &str, unsigned_tx_proposal: UnsignedTxProposal) -> Result<TxProposal> {
pub fn sign_tx_with_mnemonic(
mnemonic: &str,
unsigned_tx_proposal: UnsignedTxProposal,
) -> Result<TxProposal> {
let mnemonic = Mnemonic::from_phrase(mnemonic, Language::English)?;
sign_tx(mnemonic, unsigned_tx_proposal)
}

pub fn sign_tx_with_bip39_entropy(
bip39_entropy: &str,
unsigned_tx_proposal: UnsignedTxProposal,
) -> Result<TxProposal> {
let mut entropy = [0u8; 32];
hex::decode_to_slice(bip39_entropy, &mut entropy)?;
let mnemonic = Mnemonic::from_entropy(&entropy, Language::English)?;
sign_tx(mnemonic, unsigned_tx_proposal)
}

pub fn sign_tx(mnemonic: Mnemonic, unsigned_tx_proposal: UnsignedTxProposal) -> Result<TxProposal> {
let account = get_account_from_mnemonic(mnemonic);
let account_key = AccountKey::new(
account.spend_private_key().as_ref(),
Expand Down

0 comments on commit 23f7d2e

Please sign in to comment.