Skip to content

Commit

Permalink
wip wip wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Jan 8, 2025
1 parent e44cf83 commit 6d781c0
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 19 deletions.
12 changes: 11 additions & 1 deletion apps/extension/src/Approvals/ConfirmSignLedgerTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { PageHeader } from "App/Common";
import { ApprovalDetails, Status } from "Approvals/Approvals";
import {
QueryPendingTxBytesMsg,
SubmitApprovedMaspSignLedgerTxMsg,
SubmitApprovedSignLedgerTxMsg,
} from "background/approvals";
import { QueryAccountDetailsMsg } from "background/keyring";
Expand Down Expand Up @@ -77,11 +78,13 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
bytes: Uint8Array,
_path: string
): Promise<{ sbar: Uint8Array; rbar: Uint8Array }> => {
console.log("bytes", bytes);
const _response = await ledger.namadaApp.signMaspSpends(
// TODO:
"m/32'/877'/0'",
Buffer.from(bytes)
);
console.log("RESPONSE", _response);
// TODO
return await ledger.namadaApp.getSpendSignature();
};
Expand Down Expand Up @@ -183,12 +186,19 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
);
}
const asd = await signMaspTx(ledger, fromBase64(tx), bip44Path);
console.log("ASD", asd);
const maspSignature = [...asd.rbar, ...asd.sbar];
maspSignatures.push(maspSignature);

const qwe = await requester.sendMessage(
Ports.Background,
new SubmitApprovedMaspSignLedgerTxMsg(msgId, maspSignature)
);
console.log("QWE222", new Uint8Array(qwe));

const signature = await signLedgerTx(
ledger,
fromBase64(tx),
new Uint8Array(qwe),
bip44Path
);
signatures.push(signature);
Expand Down
14 changes: 14 additions & 0 deletions apps/extension/src/background/approvals/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
RejectSignArbitraryMsg,
RejectSignTxMsg,
RevokeConnectionMsg,
SubmitApprovedMaspSignLedgerTxMsg,
SubmitApprovedSignArbitraryMsg,
SubmitApprovedSignLedgerTxMsg,
SubmitApprovedSignTxMsg,
Expand Down Expand Up @@ -113,6 +114,11 @@ export const getHandler: (service: ApprovalsService) => Handler = (service) => {
env,
msg as SubmitApprovedSignLedgerTxMsg
);
case SubmitApprovedMaspSignLedgerTxMsg:
return handleSubmitApprovedMaspSignLedgerTxMsg(service)(
env,
msg as SubmitApprovedMaspSignLedgerTxMsg
);

default:
throw new Error("Unknown msg type");
Expand Down Expand Up @@ -287,6 +293,14 @@ const handleSubmitApprovedSignLedgerTxMsg: (
};
};

const handleSubmitApprovedMaspSignLedgerTxMsg: (
service: ApprovalsService
) => InternalHandler<SubmitApprovedMaspSignLedgerTxMsg> = (service) => {
return async (_, { msgId, maspSignatures }) => {
return await service.submitSignMaspLedgerTx(msgId, maspSignatures);
};
};

const handleCheckIsApprovedSite: (
service: ApprovalsService
) => InternalHandler<CheckIsApprovedSiteMsg> = (service) => {
Expand Down
2 changes: 2 additions & 0 deletions apps/extension/src/background/approvals/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
RejectSignArbitraryMsg,
RejectSignTxMsg,
RevokeConnectionMsg,
SubmitApprovedMaspSignLedgerTxMsg,
SubmitApprovedSignArbitraryMsg,
SubmitApprovedSignLedgerTxMsg,
SubmitApprovedSignTxMsg,
Expand All @@ -36,6 +37,7 @@ export function init(router: Router, service: ApprovalsService): void {
router.registerMessage(SubmitApprovedSignTxMsg);
router.registerMessage(SubmitApprovedSignArbitraryMsg);
router.registerMessage(SubmitApprovedSignLedgerTxMsg);
router.registerMessage(SubmitApprovedMaspSignLedgerTxMsg);
router.registerMessage(IsConnectionApprovedMsg);
router.registerMessage(ApproveConnectInterfaceMsg);
router.registerMessage(ConnectInterfaceResponseMsg);
Expand Down
26 changes: 26 additions & 0 deletions apps/extension/src/background/approvals/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export enum MessageType {
SubmitApprovedSignTx = "submit-approved-sign-tx",
SubmitApprovedSignArbitrary = "submit-approved-sign-arbitrary",
SubmitApprovedSignLedgerTx = "submit-approved-sign-ledger-tx",
SubmitApprovedSignMaspLedgerTx = "submit-approved-sign-masp-ledger-tx",
RejectSignArbitrary = "reject-sign-arbitrary",
ConnectInterfaceResponse = "connect-interface-response",
DisconnectInterfaceResponse = "disconnect-interface-response",
Expand Down Expand Up @@ -72,6 +73,31 @@ export class SubmitApprovedSignLedgerTxMsg extends Message<void> {
}
}

export class SubmitApprovedMaspSignLedgerTxMsg extends Message<Uint8Array> {
public static type(): MessageType {
return MessageType.SubmitApprovedSignMaspLedgerTx;
}

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

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

route(): string {
return ROUTE;
}

type(): string {
return SubmitApprovedMaspSignLedgerTxMsg.type();
}
}

export class RejectSignTxMsg extends Message<void> {
public static type(): MessageType {
return MessageType.RejectSignTx;
Expand Down
31 changes: 27 additions & 4 deletions apps/extension/src/background/approvals/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,9 @@ export class ApprovalsService {
);
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;
return tx.appendSignature(asd, responseSign[i]);
// return asd;
});
resolvers.resolve(signedTxs);
} catch (e) {
Expand All @@ -174,6 +172,31 @@ export class ApprovalsService {
await this.clearPendingSignature(msgId);
}

async submitSignMaspLedgerTx(
msgId: string,
maspSignatures: number[]
): Promise<Uint8Array> {
const pendingTx = await this.txStore.get(msgId);

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

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

// TODO: this is not correct but should work for one tx
return pendingTx.txs.map(({ bytes, signingData }, i) => {
const sd = signingData.map((sd) =>
new Message().encode(new SigningDataMsgValue(sd))
);
const wwww = new Uint8Array(maspSignatures);

const asd = tx.appendMaspSignature(bytes, sd, wwww);
// return tx.appendSignature(asd, responseSign[i]);
return asd;
})[0];
}

async submitSignArbitrary(
popupTabId: number,
msgId: string,
Expand Down
2 changes: 1 addition & 1 deletion apps/namadillo/public/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#indexer_url = ""
#rpc_url = ""
#masp_indexer_url = ""
#localnet_enabled = false
localnet_enabled = true
13 changes: 11 additions & 2 deletions apps/namadillo/src/atoms/transfer/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,31 @@ export const createUnshieldingTransferTx = async (
const token = props[0]?.data[0]?.token;
const amount = props[0]?.data[0]?.amount;

const sdk = await getSdkInstance();
const ledger = await sdk.initLedger();

const bparams = await ledger.getBparams();
ledger.closeTransport();
console.log("bparams closed", bparams);

return await workerBuildTxPair({
rpcUrl,
token,
signerAddress: disposableSigner.address,
signerAddress: account.address,
// signerAddress: disposableSigner.address,
buildTxFn: async (workerLink) => {
const msgValue = new UnshieldingTransferMsgValue({
source,
gasSpendingKey: source,
data: [{ target: destination, token, amount }],
bparams,
});
const msg: Unshield = {
type: "unshield",
payload: {
account: {
...account,
publicKey: disposableSigner.publicKey,
// publicKey: disposableSigner.publicKey,
},
gasConfig,
props: [msgValue],
Expand Down
2 changes: 1 addition & 1 deletion apps/namadillo/src/workers/MaspTxWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ async function unshield(

await sdk.masp.loadMaspParams("", chain.chainId);

console.log("Unshielding props", unshieldingProps);
const encodedTxData = await buildTx<UnshieldingTransferMsgValue>(
sdk,
account,
Expand All @@ -125,6 +124,7 @@ async function unshield(
sdk.tx.buildUnshieldingTransfer,
true
);
console.log("encodedTxData", encodedTxData);

return encodedTxData;
}
Expand Down
26 changes: 16 additions & 10 deletions packages/shared/lib/src/sdk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,22 +596,28 @@ impl Sdk {
(tx, masp_signing_data)
}
BuildParams::StoredBuildParams(mut bparams) => {
web_sys::console::log_1(&"StoredBuildParams".into());
let tx = build_unshielding_transfer(&self.namada, &mut args, &mut bparams).await?;
let masp_signing_data = MaspSigningData::new(bparams, xfvks);

(tx, masp_signing_data)
}
};

if let Some(shielded_hash) = signing_data.shielded_hash {
web_sys::console::log_1(
&format!(
"tx: {:?}",
borsh::to_vec(tx.get_masp_section(&shielded_hash).unwrap())
)
.into(),
);
}
web_sys::console::log_1(&format!("signing_data owner: {:?}", signing_data.owner).into());
web_sys::console::log_1(
&format!("signing_data public_keys: {:?}", signing_data.public_keys).into(),
);
web_sys::console::log_1(&format!("signing_data fee: {:?}", signing_data.fee_payer).into());

// if let Some(shielded_hash) = signing_data.shielded_hash {
// web_sys::console::log_1(
// &format!(
// "tx: {:?}",
// borsh::to_vec(tx.get_masp_section(&shielded_hash).unwrap())
// )
// .into(),
// );
// }

self.serialize_tx_result(tx, wrapper_tx_msg, signing_data, Some(masp_signing_data))
}
Expand Down

0 comments on commit 6d781c0

Please sign in to comment.