Skip to content

Commit

Permalink
Refactor submit calls to include signing
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Jul 14, 2023
1 parent 658ac14 commit 6ce3cf3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ export const ConfirmLedgerBond: React.FC<Props> = ({
const ledger = await Ledger.init();

try {
setStatusInfo("Querying for public key on chain...");
const pk = await queryPublicKey(address);

if (!pk) {
setStatusInfo("Review and approve reveal pk on your Ledger");
setStatusInfo(
"Public key not found! Review and approve reveal pk on your Ledger"
);
await revealPk(ledger, publicKey);
}

Expand Down
62 changes: 35 additions & 27 deletions apps/extension/src/background/ledger/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,14 @@ export class LedgerService {
signatures: ResponseSign
): Promise<void> {
const {
wrapperSignature: { raw: wrapperSig },
rawSignature: { raw: rawSig },
} = signatures;
wrapperSignature: {
raw: { data: wrapperSig },
},
rawSignature: {
raw: { data: rawSig },
},
// TODO: We need the correct type updated in ResponseSign
} = signatures as any; // eslint-disable-line

if (!wrapperSig) {
throw new Error("No wrapper signature was produced!");
Expand All @@ -88,14 +93,13 @@ export class LedgerService {
throw new Error("No raw signature was produced!");
}

const signedRevealPk = await this.sdk.sign_tx(
fromBase64(bytes),
new Uint8Array(wrapperSig),
new Uint8Array(rawSig)
);

try {
await this.sdk.submit_signed_reveal_pk(fromBase64(txMsg), signedRevealPk);
await this.sdk.submit_signed_reveal_pk(
fromBase64(txMsg),
fromBase64(bytes),
new Uint8Array(wrapperSig),
new Uint8Array(rawSig)
);
} catch (e) {
console.warn(e);
}
Expand Down Expand Up @@ -148,9 +152,14 @@ export class LedgerService {
}

const {
wrapperSignature: { raw: wrapperSig },
rawSignature: { raw: rawSig },
} = signatures;
wrapperSignature: {
raw: { data: wrapperSig },
},
rawSignature: {
raw: { data: rawSig },
},
// TODO: We need the correct type updated in ResponseSign
} = signatures as any; // eslint-disable-line

if (!wrapperSig) {
throw new Error("No wrapper signature was produced!");
Expand All @@ -160,14 +169,13 @@ export class LedgerService {
throw new Error("No raw signature was produced!");
}

const signedTransfer = await this.sdk.sign_tx(
fromBase64(bytes),
new Uint8Array(wrapperSig),
new Uint8Array(rawSig)
);

try {
await this.sdk.submit_signed_transfer(fromBase64(txMsg), signedTransfer);
await this.sdk.submit_signed_transfer(
fromBase64(txMsg),
fromBase64(bytes),
new Uint8Array(wrapperSig),
new Uint8Array(rawSig)
);

// Clear pending tx if successful
await this.txStore.set(msgId, null);
Expand Down Expand Up @@ -233,6 +241,7 @@ export class LedgerService {
// TODO: We need the correct type updated in ResponseSign
} = signatures as any; // eslint-disable-line

console.log({ wrapperSig, rawSig });
if (!wrapperSig) {
throw new Error("No wrapper signature was produced!");
}
Expand All @@ -241,14 +250,13 @@ export class LedgerService {
throw new Error("No raw signature was produced!");
}

const signedBond = await this.sdk.sign_tx(
fromBase64(bytes),
new Uint8Array(wrapperSig),
new Uint8Array(rawSig)
);

try {
await this.sdk.submit_signed_bond(fromBase64(txMsg), signedBond);
await this.sdk.submit_signed_bond(
fromBase64(txMsg),
fromBase64(bytes),
new Uint8Array(wrapperSig),
new Uint8Array(rawSig)
);

// Clear pending tx if successful
await this.txStore.set(msgId, null);
Expand Down
27 changes: 18 additions & 9 deletions packages/shared/lib/src/sdk/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::to_js_result;
use crate::utils::{console_log, to_js_result};
use crate::{
rpc_client::HttpClient,
sdk::masp::WebShieldedUtils,
Expand Down Expand Up @@ -183,8 +183,10 @@ impl Sdk {
&mut self,
tx_msg: &[u8],
tx_bytes: &[u8],
wrapper_sig_bytes: &[u8],
raw_sig_bytes: &[u8],
) -> Result<(), JsError> {
let reveal_pk_tx = Tx::try_from_slice(&tx_bytes).map_err(JsError::from)?;
let reveal_pk_tx = self.sign_tx(tx_bytes, wrapper_sig_bytes, raw_sig_bytes)?;
let args = tx::reveal_pk_tx_args(tx_msg).map_err(JsError::from)?;

namada::ledger::tx::process_tx(&self.client, &mut self.wallet, &args.tx, reveal_pk_tx)
Expand Down Expand Up @@ -239,12 +241,12 @@ impl Sdk {
}

// Append signatures and return tx bytes
pub fn sign_tx(
fn sign_tx(
&self,
tx_bytes: &[u8],
wrapper_sig_bytes: &[u8],
raw_sig_bytes: &[u8],
) -> Result<JsValue, JsError> {
) -> Result<Tx, JsError> {
let mut tx: Tx = Tx::try_from_slice(tx_bytes).map_err(JsError::from)?;

let wrapper_sig = Signature::try_from_slice(wrapper_sig_bytes).map_err(JsError::from)?;
Expand All @@ -254,17 +256,20 @@ impl Sdk {
tx.protocol_filter();
tx.add_section(Section::Signature(wrapper_sig));

let bytes = tx.try_to_vec().map_err(JsError::from)?;
to_js_result(Vec::from(bytes))
console_log(&format!("Tx: {:?}", &tx));

Ok(tx)
}

/// Submit signed transfer tx
pub async fn submit_signed_transfer(
&mut self,
tx_msg: &[u8],
tx_bytes: &[u8],
wrapper_sig_bytes: &[u8],
raw_sig_bytes: &[u8],
) -> Result<(), JsError> {
let transfer_tx = Tx::try_from_slice(&tx_bytes).map_err(JsError::from)?;
let transfer_tx = self.sign_tx(tx_bytes, wrapper_sig_bytes, raw_sig_bytes)?;
let args = tx::transfer_tx_args(tx_msg, None, None).map_err(JsError::from)?;
let verification_key = args.tx.verification_key.clone();
let pk = validate_pk(verification_key)?;
Expand Down Expand Up @@ -339,8 +344,10 @@ impl Sdk {
&mut self,
tx_msg: &[u8],
tx_bytes: &[u8],
wrapper_sig_bytes: &[u8],
raw_sig_bytes: &[u8],
) -> Result<(), JsError> {
let bond_tx = Tx::try_from_slice(&tx_bytes).map_err(JsError::from)?;
let bond_tx = self.sign_tx(tx_bytes, wrapper_sig_bytes, raw_sig_bytes)?;
let args = tx::bond_tx_args(tx_msg, None).map_err(JsError::from)?;

namada::ledger::tx::process_tx(&self.client, &mut self.wallet, &args.tx, bond_tx)
Expand Down Expand Up @@ -373,8 +380,10 @@ impl Sdk {
&mut self,
tx_msg: &[u8],
tx_bytes: &[u8],
wrapper_sig_bytes: &[u8],
raw_sig_bytes: &[u8],
) -> Result<(), JsError> {
let bond_tx = Tx::try_from(tx_bytes).map_err(JsError::from)?;
let bond_tx = self.sign_tx(tx_bytes, wrapper_sig_bytes, raw_sig_bytes)?;
let args = tx::unbond_tx_args(tx_msg, None).map_err(JsError::from)?;
let verification_key = args.tx.verification_key.clone();
let pk = validate_pk(verification_key)?;
Expand Down

0 comments on commit 6ce3cf3

Please sign in to comment.