Skip to content

Commit

Permalink
feat: support ledger masp tx wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Jan 8, 2025
1 parent bb66cf2 commit 260add8
Show file tree
Hide file tree
Showing 18 changed files with 632 additions and 35 deletions.
24 changes: 22 additions & 2 deletions apps/extension/src/Approvals/ConfirmSignLedgerTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
}
}, [status]);

const signMaspTx = async (
ledger: Ledger,
bytes: Uint8Array,
_path: string
): Promise<{ sbar: Uint8Array; rbar: Uint8Array }> => {
const _response = await ledger.namadaApp.signMaspSpends(
// TODO:
"m/32'/877'/0'",
Buffer.from(bytes)
);
// TODO
return await ledger.namadaApp.getSpendSignature();
};

const signLedgerTx = async (
ledger: Ledger,
bytes: Uint8Array,
Expand Down Expand Up @@ -122,6 +136,7 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
setStepTwoDescription("Preparing transaction...");

try {
console.log("Querying account details for", signer);
const accountDetails = await requester.sendMessage(
Ports.Background,
new QueryAccountDetailsMsg(signer)
Expand All @@ -147,6 +162,7 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
}

const signatures: ResponseSign[] = [];
const maspSignatures: number[][] = [];

let txIndex = 0;
const txCount = pendingTxs.length;
Expand All @@ -156,7 +172,7 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
setStepTwoDescription(<p>{stepTwoText}</p>);
}

for await (const tx of pendingTxs) {
for await (const { bytes: tx } of pendingTxs) {
if (txCount > 1) {
setStepTwoDescription(
<p>
Expand All @@ -166,6 +182,10 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
</p>
);
}
const asd = await signMaspTx(ledger, fromBase64(tx), bip44Path);
const maspSignature = [...asd.rbar, ...asd.sbar];
maspSignatures.push(maspSignature);

const signature = await signLedgerTx(
ledger,
fromBase64(tx),
Expand All @@ -178,7 +198,7 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
setStepTwoDescription(<p>Submitting...</p>);
await requester.sendMessage(
Ports.Background,
new SubmitApprovedSignLedgerTxMsg(msgId, signatures)
new SubmitApprovedSignLedgerTxMsg(msgId, signatures, maspSignatures)
);

setStatus(Status.Completed);
Expand Down
1 change: 1 addition & 0 deletions apps/extension/src/Approvals/ConfirmSignTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const ConfirmSignTx: React.FC<Props> = ({ details }) => {
if (!isAuthenticated) {
throw new Error("Invalid password!");
}
console.log("signer", signer);

await requester.sendMessage(
Ports.Background,
Expand Down
12 changes: 10 additions & 2 deletions apps/extension/src/background/approvals/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,16 @@ const handleQuerySignArbitraryData: (
const handleSubmitApprovedSignLedgerTxMsg: (
service: ApprovalsService
) => InternalHandler<SubmitApprovedSignLedgerTxMsg> = (service) => {
return async ({ senderTabId: popupTabId }, { msgId, responseSign }) => {
return await service.submitSignLedgerTx(popupTabId, msgId, responseSign);
return async (
{ senderTabId: popupTabId },
{ msgId, responseSign, maspSignatures }
) => {
return await service.submitSignLedgerTx(
popupTabId,
msgId,
responseSign,
maspSignatures
);
};
};

Expand Down
9 changes: 6 additions & 3 deletions apps/extension/src/background/approvals/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ export class SubmitApprovedSignLedgerTxMsg extends Message<void> {

constructor(
public readonly msgId: string,
public readonly responseSign: ResponseSign[]
public readonly responseSign: ResponseSign[],
public readonly maspSignatures: number[][]
) {
super();
}

validate(): void {
validateProps(this, ["msgId", "responseSign"]);
validateProps(this, ["msgId", "responseSign", "maspSignatures"]);
}

route(): string {
Expand Down Expand Up @@ -258,7 +259,9 @@ export class QueryTxDetailsMsg extends Message<TxDetails[]> {
}
}

export class QueryPendingTxBytesMsg extends Message<string[] | undefined> {
export class QueryPendingTxBytesMsg extends Message<
{ bytes: string; signingData: string[] }[] | undefined
> {
public static type(): MessageType {
return MessageType.QueryPendingTxBytes;
}
Expand Down
43 changes: 37 additions & 6 deletions apps/extension/src/background/approvals/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { v4 as uuid } from "uuid";
import browser, { Windows } from "webextension-polyfill";

import { KVStore } from "@namada/storage";
import { SignArbitraryResponse, TxDetails } from "@namada/types";
import {
Message,
SignArbitraryResponse,
SigningDataMsgValue,
TxDetails,
} from "@namada/types";
import { paramsToUrl } from "@namada/utils";

import { ResponseSign } from "@zondax/ledger-namada";
Expand Down Expand Up @@ -131,7 +136,8 @@ export class ApprovalsService {
async submitSignLedgerTx(
popupTabId: number,
msgId: string,
responseSign: ResponseSign[]
responseSign: ResponseSign[],
maspSignatures: number[][]
): Promise<void> {
const pendingTx = await this.txStore.get(msgId);
const resolvers = this.getResolver(popupTabId);
Expand All @@ -146,9 +152,19 @@ export class ApprovalsService {

const { tx } = this.sdkService.getSdk();

console.log("maspSignatures", maspSignatures);
try {
const signedTxs = pendingTx.txs.map(({ bytes }, i) => {
return tx.appendSignature(bytes, responseSign[i]);
const signedTxs = pendingTx.txs.map(({ bytes, signingData }, i) => {
const sd = signingData.map((sd) =>
new Message().encode(new SigningDataMsgValue(sd))
);
const wwww = new Uint8Array(maspSignatures[i]);

console.log("sd", sd, maspSignatures[i], i);
const asd = tx.appendMaspSignature(bytes, sd, wwww);
console.log("asd", asd);
// return tx.appendSignature(asd, responseSign[i]);
return asd;
});
resolvers.resolve(signedTxs);
} catch (e) {
Expand Down Expand Up @@ -333,6 +349,7 @@ export class ApprovalsService {
}

async approveUpdateDefaultAccount(address: string): Promise<void> {
console.log("approveUpdateDefaultAccount");
const account = await this.keyRingService.queryAccountDetails(address);

return this.launchApprovalPopup(TopLevelRoute.ApproveUpdateDefaultAccount, {
Expand Down Expand Up @@ -368,15 +385,29 @@ export class ApprovalsService {
);
}

async queryPendingTxBytes(msgId: string): Promise<string[] | undefined> {
// TODO: all of this is not needed most likelyk
async queryPendingTxBytes(
msgId: string
): Promise<{ bytes: string; signingData: string[] }[] | undefined> {
const pendingTx = await this.txStore.get(msgId);

if (!pendingTx) {
throw new Error(ApprovalErrors.TransactionDataNotFound(msgId));
}

if (pendingTx.txs) {
return pendingTx.txs.map(({ bytes }) => toBase64(bytes));
// TODO:
return pendingTx.txs.map(({ bytes, signingData }) => {
console.log("signingData223455", signingData);
const www = {
bytes: toBase64(bytes),
signingData: signingData.map((sd) =>
toBase64(new Message().encode(new SigningDataMsgValue(sd)))
),
};
console.log("www", www.signingData);
return www;
});
}
}

Expand Down
1 change: 1 addition & 0 deletions apps/extension/src/background/keyring/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ const handleQueryAccountDetails: (
service: KeyRingService
) => InternalHandler<QueryAccountDetailsMsg> = (service) => {
return async (_, { address }) => {
console.log("queryAccountDetails 2222");
return await service.queryAccountDetails(address);
};
};
Expand Down
6 changes: 3 additions & 3 deletions apps/extension/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const plugins = [
MANIFEST_PATH,
...(NODE_ENV === "development" && TARGET === "firefox" ?
[MANIFEST_V2_DEV_ONLY_PATH]
: []),
: []),
],
output: {
fileName: "./manifest.json",
Expand Down Expand Up @@ -140,7 +140,7 @@ module.exports = {
devtool:
NODE_ENV === "development" && TARGET === "firefox" ?
"eval-source-map"
: false,
: false,
entry: {
content: "./src/content",
background: "./src/background",
Expand Down Expand Up @@ -224,7 +224,7 @@ module.exports = {
hints: "warning",
maxAssetSize: 200000,
maxEntrypointSize: 400000,
assetFilter: function (assetFilename) {
assetFilter: function(assetFilename) {
assetFilename.endsWith(".wasm");
},
},
Expand Down
2 changes: 2 additions & 0 deletions apps/namadillo/src/App/WorkerTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ export function WorkerTest(): JSX.Element {
};

const { payload: encodedTx } = await shieldWorker.unshield(msg);
console.log("Encoded tx", encodedTx);
const signedTxs = await signTx(encodedTx, disposableSigner?.address || "");
console.log("Signed txs", signedTxs);

await shieldWorker.broadcast({
type: "broadcast",
Expand Down
Loading

0 comments on commit 260add8

Please sign in to comment.