Skip to content

Commit

Permalink
Add reveal pk for submitting signed transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Jul 5, 2023
1 parent 6758f0a commit 91ef63b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export const ConfirmLedgerTransfer: React.FC<Props> = ({ msgId }) => {
new GetTransferBytesMsg(msgId)
);

// Retrieve publicKey from Ledger
const { publicKey } = await ledger.getAddressAndPublicKey(path);

// Sign with Ledger
const signatures = await ledger.sign(bytes, path);
const { errorMessage, returnCode } = signatures;
Expand All @@ -53,7 +56,12 @@ export const ConfirmLedgerTransfer: React.FC<Props> = ({ msgId }) => {
// Submit signatures for tx
await requester.sendMessage(
Ports.Background,
new SubmitSignedTransferMsg(msgId, toBase64(bytes), signatures)
new SubmitSignedTransferMsg(
msgId,
toBase64(bytes),
signatures,
publicKey
)
);
setStatus(Status.Completed);
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions apps/extension/src/background/ledger/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const handleSubmitSignedTransferMsg: (
service: LedgerService
) => InternalHandler<SubmitSignedTransferMsg> = (service) => {
return async (_, msg) => {
const { bytes, msgId, signatures } = msg;
return await service.submitTransfer(msgId, bytes, signatures);
const { bytes, publicKey, msgId, signatures } = msg;
return await service.submitTransfer(msgId, bytes, signatures, publicKey);
};
};
7 changes: 6 additions & 1 deletion apps/extension/src/background/ledger/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export class SubmitSignedTransferMsg extends Message<void> {
constructor(
public readonly msgId: string,
public readonly bytes: string,
public readonly signatures: ResponseSign
public readonly signatures: ResponseSign,
public readonly publicKey: string
) {
super();
}
Expand All @@ -104,6 +105,10 @@ export class SubmitSignedTransferMsg extends Message<void> {
if (!this.signatures) {
throw new Error("No signatures were provided!");
}

if (!this.publicKey) {
throw new Error("No publicKey provided!");
}
}

route(): string {
Expand Down
9 changes: 7 additions & 2 deletions apps/extension/src/background/ledger/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export class LedgerService {
async submitTransfer(
msgId: string,
bytes: string,
signatures: ResponseSign
signatures: ResponseSign,
publicKey: string
): Promise<void> {
const txMsg = await this.txStore.get(msgId);

Expand All @@ -89,7 +90,11 @@ export class LedgerService {
);

try {
await this.sdk.submit_signed_transfer(fromBase64(txMsg), signedTransfer);
await this.sdk.submit_signed_transfer(
publicKey,
fromBase64(txMsg),
signedTransfer
);

// Clear pending tx if successful
await this.txStore.set(msgId, null);
Expand Down
8 changes: 7 additions & 1 deletion packages/shared/lib/src/sdk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl Sdk {
mut tx: Tx,
pk: PublicKey,
) -> Result<(), JsError> {
// Build a transaction to reveal the signer of this transaction
// Submit a reveal pk tx if necessary
self.submit_reveal_pk(&args, tx.clone(), &pk).await?;

// Sign tx
Expand Down Expand Up @@ -203,11 +203,17 @@ impl Sdk {
/// Submit signed transfer tx
pub async fn submit_signed_transfer(
&mut self,
pk: String,
tx_msg: &[u8],
tx_bytes: &[u8],
) -> Result<(), JsError> {
let transfer_tx = Tx::try_from(tx_bytes).map_err(|e| JsError::from(e))?;
let args = tx::transfer_tx_args(tx_msg, None, None).map_err(|e| JsError::from(e))?;
let pk = PublicKey::from_str(&pk).map_err(JsError::from)?;

self.submit_reveal_pk(&args.tx, transfer_tx.clone(), &pk)
.await?;

namada::ledger::tx::process_tx(&self.client, &mut self.wallet, &args.tx, transfer_tx)
.await
.map_err(JsError::from)?;
Expand Down

0 comments on commit 91ef63b

Please sign in to comment.