Skip to content

Commit

Permalink
Continue hooking up new signature logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Jul 21, 2023
1 parent d4016a4 commit d291f26
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 38 deletions.
42 changes: 13 additions & 29 deletions apps/extension/src/background/ledger/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
TabStore,
syncTabs,
} from "background/keyring";
import { generateId } from "utils";
import { encodeSignature, generateId } from "utils";
import { ExtensionRequester } from "extension";
import { Ports } from "router";
import { UpdatedStakingEventMsg } from "content/events";
Expand Down Expand Up @@ -77,25 +77,18 @@ export class LedgerService {
bytes: string,
signatures: ResponseSign
): Promise<void> {
const {
wrapperSignature: { raw: wrapperSig },
rawSignature: { raw: rawSig },
} = signatures;

if (!wrapperSig) {
throw new Error("No wrapper signature was produced!");
}

if (!rawSig) {
throw new Error("No raw signature was produced!");
}
const { wrapperSignature, rawSignature } = signatures;

try {
// Serialize signatures
const rawSig = encodeSignature(rawSignature);
const wrapperSig = encodeSignature(wrapperSignature);

await this.sdk.submit_signed_reveal_pk(
fromBase64(txMsg),
fromBase64(bytes),
new Uint8Array(wrapperSig),
new Uint8Array(rawSig)
rawSig,
wrapperSig
);
} catch (e) {
console.warn(e);
Expand Down Expand Up @@ -223,25 +216,16 @@ export class LedgerService {
throw new Error(`Bond Transaction ${msgId} not found!`);
}

const {
wrapperSignature: { raw: wrapperSig },
rawSignature: { raw: rawSig },
} = signatures;

if (!wrapperSig) {
throw new Error("No wrapper signature was produced!");
}

if (!rawSig) {
throw new Error("No raw signature was produced!");
}
const { wrapperSignature, rawSignature } = signatures;

try {
const rawSig = encodeSignature(rawSignature);
const wrapperSig = encodeSignature(wrapperSignature);
await this.sdk.submit_signed_bond(
fromBase64(txMsg),
fromBase64(bytes),
new Uint8Array(wrapperSig),
new Uint8Array(rawSig)
rawSig,
wrapperSig
);

await this.broadcastUpdateStaking();
Expand Down
31 changes: 30 additions & 1 deletion apps/extension/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import browser from "webextension-polyfill";
import { v5 as uuid } from "uuid";

import { DerivedAccount } from "@namada/types";
import { DerivedAccount, Message, SignatureMsgValue } from "@namada/types";
import { pick } from "@namada/utils";
import { AccountStore } from "background/keyring";
import { ISignature } from "@namada/ledger-namada";

/**
* Query the current extension tab and close it
Expand Down Expand Up @@ -48,3 +49,31 @@ export const generateId = (
): string => {
return uuid(args.join(":"), namespace);
};

/**
* Convert ISignature into serialized and encoded signature
*/
export const encodeSignature = (sig: ISignature): Uint8Array => {
const { salt, indicies, pubkey, signature } = sig;

// Validate props
if (!salt || !indicies || !pubkey || !signature) {
throw new Error("Invalid signature!");
}

// TODO: Note that the following "any" type usage below is a result of the buffer responses
// from the Ledger do not match the ISignature type! This will be fixed in a future release.

/* eslint-disable */
const props = {
salt: new Uint8Array((salt as any).data),
indicies: new Uint8Array((indicies as any).data),
pubkey: new Uint8Array((pubkey as any).data),
signature: new Uint8Array((signature as any).data),
};
/* eslint-enable */

const value = new SignatureMsgValue(props);
const msg = new Message<SignatureMsgValue>();
return msg.encode(value);
};
6 changes: 3 additions & 3 deletions packages/shared/lib/src/sdk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ impl Sdk {
&mut self,
tx_msg: &[u8],
tx_bytes: &[u8],
wrapper_sig_bytes: &[u8],
raw_sig_bytes: &[u8],
wrapper_sig_bytes: &[u8],
) -> Result<(), JsError> {
let reveal_pk_tx = self.sign_tx(tx_bytes, wrapper_sig_bytes, raw_sig_bytes)?;
let reveal_pk_tx = self.sign_tx(tx_bytes, raw_sig_bytes, wrapper_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 @@ -245,8 +245,8 @@ impl Sdk {
fn sign_tx(
&self,
tx_bytes: &[u8],
wrapper_sig_bytes: &[u8],
raw_sig_bytes: &[u8],
wrapper_sig_bytes: &[u8],
) -> Result<Tx, JsError> {
let mut tx: Tx = Tx::try_from_slice(tx_bytes).map_err(JsError::from)?;

Expand Down
2 changes: 1 addition & 1 deletion packages/shared/lib/src/sdk/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub fn construct_signature(sig_msg: &[u8], tx: &Tx) -> Result<Signature, JsError

let SignatureMsg {
salt,
pubkey,
indicies,
pubkey,
signature,
} = sig_msg;

Expand Down
3 changes: 0 additions & 3 deletions packages/types/src/tx/schema/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import { field, vec } from "@dao-xyz/borsh";
import { SignatureProps } from "../types";

export class SignatureMsgValue {
@field({ type: vec("u8") })
raw!: Uint8Array;

@field({ type: vec("u8") })
salt!: Uint8Array;

Expand Down
1 change: 0 additions & 1 deletion packages/types/src/tx/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export type InitAccountProps = {
};

export type SignatureProps = {
raw: Uint8Array;
salt: Uint8Array;
indicies: Uint8Array;
pubkey: Uint8Array;
Expand Down

0 comments on commit d291f26

Please sign in to comment.