Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Jan 9, 2025
1 parent 694ba67 commit cc9625c
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 139 deletions.
4 changes: 1 addition & 3 deletions apps/extension/src/Approvals/ConfirmSignLedgerTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ 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 Down Expand Up @@ -188,7 +187,7 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
setStepTwoDescription(<p>{stepTwoText}</p>);
}

for await (const { bytes: tx } of pendingTxs) {
for await (const tx of pendingTxs) {
if (txCount > 1) {
setStepTwoDescription(
<p>
Expand All @@ -210,7 +209,6 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
Ports.Background,
new ReplaceMaspSignatureMsg(msgId, maspSignature, txIndex)
);
console.log("txWithMaspSection", txWithMaspSection);

const signature = await signLedgerTx(
ledger,
Expand Down
1 change: 0 additions & 1 deletion apps/extension/src/Approvals/ConfirmSignTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ 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
4 changes: 1 addition & 3 deletions apps/extension/src/background/approvals/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,7 @@ export class QueryTxDetailsMsg extends Message<TxDetails[]> {
}
}

export class QueryPendingTxBytesMsg extends Message<
{ bytes: string; signingData: string[] }[] | undefined
> {
export class QueryPendingTxBytesMsg extends Message<string[] | undefined> {
public static type(): MessageType {
return MessageType.QueryPendingTxBytes;
}
Expand Down
19 changes: 2 additions & 17 deletions apps/extension/src/background/approvals/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ 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 @@ -413,29 +412,15 @@ export class ApprovalsService {
);
}

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

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

if (pendingTx.txs) {
// 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;
});
return pendingTx.txs.map(({ bytes }) => toBase64(bytes));
}
}

Expand Down
3 changes: 1 addition & 2 deletions apps/extension/src/background/keyring/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ const handleQueryAccountsMsg: (
const output =
query && query.accountId ?
await service.queryAccountsByParentId(query.accountId)
: await service.queryAccounts();
: await service.queryAccounts();

return output;
};
Expand Down Expand Up @@ -262,7 +262,6 @@ const handleQueryAccountDetails: (
service: KeyRingService
) => InternalHandler<QueryAccountDetailsMsg> = (service) => {
return async (_, { address }) => {
console.log("queryAccountDetails 2222");
return await service.queryAccountDetails(address);
};
};
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 = true
#localnet_enabled = false
2 changes: 0 additions & 2 deletions apps/namadillo/src/App/WorkerTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ 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
32 changes: 20 additions & 12 deletions apps/namadillo/src/atoms/transfer/services.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
Account,
AccountType,
BparamsMsgValue,
GenDisposableSignerResponse,
ShieldedTransferMsgValue,
ShieldedTransferProps,
Expand Down Expand Up @@ -102,7 +104,9 @@ export const createShieldedTransferTx = async (
rpcUrl: string,
memo?: string
): Promise<TransactionPair<ShieldedTransferProps> | undefined> => {
const disposableSigner = await getDisposableSigner();
// For now we only support disposableSigner for non-ledger accounts
const { address: signerAddress, publicKey: signerPublicKey } =
account.type === AccountType.Ledger ? account : await getDisposableSigner();
const source = props[0]?.data[0]?.source;
const destination = props[0]?.data[0]?.target;
const token = props[0]?.data[0]?.token;
Expand All @@ -111,7 +115,7 @@ export const createShieldedTransferTx = async (
return await workerBuildTxPair({
rpcUrl,
token,
signerAddress: disposableSigner.address,
signerAddress,
buildTxFn: async (workerLink) => {
const msgValue = new ShieldedTransferMsgValue({
gasSpendingKey: source,
Expand All @@ -122,7 +126,7 @@ export const createShieldedTransferTx = async (
payload: {
account: {
...account,
publicKey: disposableSigner.publicKey,
publicKey: signerPublicKey,
},
gasConfig,
props: [msgValue],
Expand Down Expand Up @@ -182,24 +186,28 @@ export const createUnshieldingTransferTx = async (
rpcUrl: string,
memo?: string
): Promise<TransactionPair<UnshieldingTransferProps> | undefined> => {
const disposableSigner = await getDisposableSigner();
// For now we only support disposableSigner for non-ledger accounts
const { address: signerAddress, publicKey: signerPublicKey } =
account.type === AccountType.Ledger ? account : await getDisposableSigner();

const source = props[0]?.source;
const destination = props[0]?.data[0]?.target;
const token = props[0]?.data[0]?.token;
const amount = props[0]?.data[0]?.amount;

const sdk = await getSdkInstance();
const ledger = await sdk.initLedger();
let bparams: BparamsMsgValue[] | undefined;

const bparams = await ledger.getBparams();
ledger.closeTransport();
console.log("bparams closed", bparams);
if (account.type === AccountType.Ledger) {
const sdk = await getSdkInstance();
const ledger = await sdk.initLedger();
bparams = await ledger.getBparams();
ledger.closeTransport();
}

return await workerBuildTxPair({
rpcUrl,
token,
signerAddress: account.address,
// signerAddress: disposableSigner.address,
signerAddress,
buildTxFn: async (workerLink) => {
const msgValue = new UnshieldingTransferMsgValue({
source,
Expand All @@ -212,7 +220,7 @@ export const createUnshieldingTransferTx = async (
payload: {
account: {
...account,
// publicKey: disposableSigner.publicKey,
publicKey: signerPublicKey,
},
gasConfig,
props: [msgValue],
Expand Down
11 changes: 5 additions & 6 deletions apps/namadillo/src/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export type TransactionPair<T> = {
export type EncodedTxData<T> = {
type: string;
txs: TxProps[] &
{
innerTxHashes: string[];
}[];
{
innerTxHashes: string[];
}[];
wrapperTxProps: WrapperTxProps;
meta?: {
props: T[];
Expand Down Expand Up @@ -67,7 +67,7 @@ const getTxProps = (
feeAmount:
gasConfig.asset ?
toDisplayAmount(gasConfig.asset, gasConfig.gasPrice)
: gasConfig.gasPrice,
: gasConfig.gasPrice,
gasLimit: gasConfig.gasLimit,
chainId: chain.chainId,
publicKey: account.publicKey!,
Expand All @@ -83,7 +83,7 @@ export const isPublicKeyRevealed = async (
try {
publicKey = (await api.apiV1RevealedPublicKeyAddressGet(address)).data
?.publicKey;
} catch { }
} catch {}
return Boolean(publicKey);
};

Expand Down Expand Up @@ -166,7 +166,6 @@ export const signTx = async <T>(
owner,
checksums
);
console.log("Signed txs", signedTxBytes);

if (!signedTxBytes) {
throw new Error("Signing batch Tx failed");
Expand Down
1 change: 0 additions & 1 deletion apps/namadillo/src/workers/MaspTxWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ async function unshield(
sdk.tx.buildUnshieldingTransfer,
true
);
console.log("encodedTxData", encodedTxData);

return encodedTxData;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/sdk/src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ export class Ledger {
* @returns bparams
*/
public async getBparams(): Promise<Bparams[]> {
// We need to clean the randomness buffers before getting randomness
// to ensure that the randomness is not reused
await this.namadaApp.cleanRandomnessBuffers();
const results: Bparams[] = [];

// TODO: not sure why ledger sometimes returns errors, so we try to get 15 valid responses
Expand Down
12 changes: 6 additions & 6 deletions packages/sdk/src/tx/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class Tx {
/**
* @param sdk - Instance of Sdk struct from wasm lib
*/
constructor(protected readonly sdk: SdkWasm) { }
constructor(protected readonly sdk: SdkWasm) {}

/**
* Build Transparent Transfer Tx
Expand Down Expand Up @@ -367,11 +367,11 @@ export class Tx {
}

/**
* TODO
* @param txBytes - [TODO:description]
* @param signingData - [TODO:description]
* @param signature - [TODO:description]
* @returns [TODO:description]
* Append signature for transactions signed by Ledger Hardware Wallet
* @param txBytes - bytes of the transaction
* @param signingData - signing data
* @param signature - masp signature
* @returns transaction bytes with signature appended
*/
appendMaspSignature(
txBytes: Uint8Array,
Expand Down
40 changes: 3 additions & 37 deletions packages/shared/lib/src/sdk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,27 +243,22 @@ impl Sdk {
to_js_result(borsh::to_vec(&tx)?)
}

// TODO: this should be unified with sign_masp somehow
pub fn sign_masp_ledger(
&self,
tx: Vec<u8>,
signing_data: Box<[Uint8Array]>,
signature: Vec<u8>,
) -> Result<JsValue, JsError> {
web_sys::console::log_1(&"sign_masp_ledger".into());
let mut namada_tx: Tx = borsh::from_slice(&tx)?;
web_sys::console::log_1(&format!("namada_tx: {:?}", namada_tx).into());
let signing_data = signing_data
.iter()
.map(|sd| {
borsh::from_slice(&sd.to_vec()).expect("Expected to deserialize signing data")
})
.collect::<Vec<tx::SigningData>>();

web_sys::console::log_1(&format!("signing_data: {:?}", signing_data).into());
// let signing_data = <Vec<tx::SigningData>>::try_from_slice(&signing_data)?;

for signing_data in signing_data {
web_sys::console::log_1(&format!("signing_data222: {:?}", signing_data).into());
let signing_tx_data = signing_data.to_signing_tx_data()?;
if let Some(shielded_hash) = signing_tx_data.shielded_hash {
let mut masp_tx = namada_tx
Expand All @@ -276,28 +271,16 @@ impl Sdk {
.expect("Expected to find the indicated MASP Builder");

let sapling_inputs = masp_builder.builder.sapling_inputs();
// let mut descriptor_map = vec![0; sapling_inputs.len()];
// for i in 0.. {
// if let Some(pos) = masp_builder.metadata.spend_index(i) {
// descriptor_map[pos] = i;
// } else {
// break;
// };
// }

web_sys::console::log_1(
&format!("sapling_inputs len: {:?}", sapling_inputs.len()).into(),
);
let mut authorizations = HashMap::new();

web_sys::console::log_1(&format!("signature: {:?}", signature).into());
let signature =
namada_sdk::masp_primitives::sapling::redjubjub::Signature::try_from_slice(
&signature.to_vec(),
)?;
// TODO: this works only if we assume that we do one
// shielded transfer in the transaction
authorizations.insert(0_usize, signature);

web_sys::console::log_1(&format!("authorizations: {:?}", authorizations).into());
masp_tx = (*masp_tx)
.clone()
.map_authorization::<namada_sdk::masp_primitives::transaction::Authorized>(
Expand All @@ -312,7 +295,6 @@ impl Sdk {
}
}

web_sys::console::log_1(&format!("namada_tx: {:?}", namada_tx).into());
to_js_result(borsh::to_vec(&namada_tx)?)
}

Expand Down Expand Up @@ -596,28 +578,12 @@ 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)
}
};
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
Loading

0 comments on commit cc9625c

Please sign in to comment.