Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Commit

Permalink
LIVE-1890 Replace all deprecated uses of open(deviceId) with withDevi…
Browse files Browse the repository at this point in the history
…ce (#1856)
  • Loading branch information
Hakim authored and lambertkevin committed Apr 11, 2022
1 parent 2bda9ea commit 3a60120
Show file tree
Hide file tree
Showing 8 changed files with 634 additions and 653 deletions.
426 changes: 213 additions & 213 deletions src/bridge/jsHelpers.ts

Large diffs are not rendered by default.

31 changes: 14 additions & 17 deletions src/families/bitcoin/js-signOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { log } from "@ledgerhq/logs";
import type { Account, Operation, SignOperationEvent } from "./../../types";
import { isSegwitDerivationMode } from "./../../derivation";
import { encodeOperationId } from "./../../operation";
import { open, close } from "./../../hw";
import { withDevice } from "../../hw/deviceAccess";
import type { Transaction } from "./types";
import { getNetworkParameters } from "./networks";
import { buildTransaction } from "./js-buildTransaction";
Expand All @@ -22,14 +22,13 @@ const signOperation = ({
deviceId: any;
transaction: Transaction;
}): Observable<SignOperationEvent> =>
Observable.create((o) => {
async function main() {
const { currency } = account;
const transport = await open(deviceId);
const hwApp = new Btc(transport);
const walletAccount = getWalletAccount(account);

try {
withDevice(deviceId)((transport) =>
Observable.create((o) => {
async function main() {
const { currency } = account;
const hwApp = new Btc(transport);
const walletAccount = getWalletAccount(account);

log("hw", `signTransaction ${currency.id} for account ${account.id}`);
const txInfo = await buildTransaction(account, transaction);
let senders = new Set<string>();
Expand Down Expand Up @@ -152,15 +151,13 @@ const signOperation = ({
expirationDate: null,
},
});
} finally {
close(transport, deviceId);
}
}

main().then(
() => o.complete(),
(e) => o.error(e)
);
});
main().then(
() => o.complete(),
(e) => o.error(e)
);
})
);

export default signOperation;
28 changes: 12 additions & 16 deletions src/families/crypto_org/js-signOperation.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { BigNumber } from "bignumber.js";
import { Observable } from "rxjs";
import { utils } from "@crypto-com/chain-jslib";
import { FeeNotLoaded } from "@ledgerhq/errors";
import CryptoOrgApp from "@ledgerhq/hw-app-cosmos";
import {
CryptoOrgWrongSignatureHeader,
CryptoOrgSignatureSize,
} from "./errors";
import type { Transaction } from "./types";
import type { Account, Operation, SignOperationEvent } from "../../types";
import { open, close } from "../../hw";
import { encodeOperationId } from "../../operation";
import CryptoOrgApp from "@ledgerhq/hw-app-cosmos";
import { utils } from "@crypto-com/chain-jslib";
import { withDevice } from "../../hw/deviceAccess";
import { buildTransaction } from "./js-buildTransaction";
import { isTestNet } from "./logic";

Expand Down Expand Up @@ -104,11 +104,9 @@ const signOperation = ({
deviceId: any;
transaction: Transaction;
}): Observable<SignOperationEvent> =>
Observable.create((o) => {
async function main() {
const transport = await open(deviceId);

try {
withDevice(deviceId)((transport) =>
Observable.create((o) => {
async function main() {
o.next({
type: "device-signature-requested",
});
Expand Down Expand Up @@ -165,15 +163,13 @@ const signOperation = ({
},
});
}
} finally {
close(transport, deviceId);
}
}

main().then(
() => o.complete(),
(e) => o.error(e)
);
});
main().then(
() => o.complete(),
(e) => o.error(e)
);
})
);

export default signOperation;
24 changes: 10 additions & 14 deletions src/families/elrond/js-signOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Observable } from "rxjs";
import { FeeNotLoaded } from "@ledgerhq/errors";
import type { Transaction } from "./types";
import type { Account, Operation, SignOperationEvent } from "../../types";
import { open, close } from "../../hw";
import { withDevice } from "../../hw/deviceAccess";
import { encodeOperationId } from "../../operation";
import Elrond from "./hw-app-elrond";
import { buildTransaction } from "./js-buildTransaction";
Expand Down Expand Up @@ -48,11 +48,9 @@ const signOperation = ({
deviceId: any;
transaction: Transaction;
}): Observable<SignOperationEvent> =>
Observable.create((o) => {
async function main() {
const transport = await open(deviceId);

try {
withDevice(deviceId)((transport) =>
Observable.create((o) => {
async function main() {
if (!transaction.fees) {
throw new FeeNotLoaded();
}
Expand Down Expand Up @@ -89,15 +87,13 @@ const signOperation = ({
expirationDate: null,
},
});
} finally {
close(transport, deviceId);
}
}

main().then(
() => o.complete(),
(e) => o.error(e)
);
});
main().then(
() => o.complete(),
(e) => o.error(e)
);
})
);

export default signOperation;
122 changes: 61 additions & 61 deletions src/families/polkadot/js-signOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
OperationType,
SignOperationEvent,
} from "../../types";
import { open, close } from "../../hw";
import { withDevice } from "../../hw/deviceAccess";
import { encodeOperationId } from "../../operation";
import { buildTransaction } from "./js-buildTransaction";
import { calculateAmount, getNonce, isFirstBond } from "./logic";
Expand Down Expand Up @@ -157,69 +157,69 @@ const signOperation = ({
deviceId: any;
transaction: Transaction;
}): Observable<SignOperationEvent> =>
new Observable((o) => {
async function main() {
const transport = await open(deviceId);

try {
o.next({
type: "device-signature-requested",
});
withDevice(deviceId)(
(transport) =>
new Observable((o) => {
async function main() {
o.next({
type: "device-signature-requested",
});

if (!transaction.fees) {
throw new FeeNotLoaded();
if (!transaction.fees) {
throw new FeeNotLoaded();
}

// Ensure amount is filled when useAllAmount
const transactionToSign = {
...transaction,
amount: calculateAmount({
a: account,
t: transaction,
}),
};
const { unsigned, registry } = await buildTransaction(
account,
transactionToSign,
true
);
const payload = registry
.createType("ExtrinsicPayload", unsigned, {
version: unsigned.version,
})
.toU8a({
method: true,
});
// Sign by device
const polkadot = new Polkadot(transport);
// FIXME: the type of payload Uint8Array is not compatible with the signature of sign which accept a string
const r = await polkadot.sign(
account.freshAddressPath,
payload as any
);
const signed = await signExtrinsic(unsigned, r.signature, registry);
o.next({
type: "device-signature-granted",
});
const operation = buildOptimisticOperation(
account,
transactionToSign,
transactionToSign.fees ?? new BigNumber(0)
);
o.next({
type: "signed",
signedOperation: {
operation,
signature: signed,
expirationDate: null,
},
});
}

// Ensure amount is filled when useAllAmount
const transactionToSign = {
...transaction,
amount: calculateAmount({
a: account,
t: transaction,
}),
};
const { unsigned, registry } = await buildTransaction(
account,
transactionToSign,
true
);
const payload = registry
.createType("ExtrinsicPayload", unsigned, {
version: unsigned.version,
})
.toU8a({
method: true,
});
// Sign by device
const polkadot = new Polkadot(transport);
// FIXME: the type of payload Uint8Array is not compatible with the signature of sign which accept a string
const r = await polkadot.sign(account.freshAddressPath, payload as any);
const signed = await signExtrinsic(unsigned, r.signature, registry);
o.next({
type: "device-signature-granted",
});
const operation = buildOptimisticOperation(
account,
transactionToSign,
transactionToSign.fees ?? new BigNumber(0)
main().then(
() => o.complete(),
(e) => o.error(e)
);
o.next({
type: "signed",
signedOperation: {
operation,
signature: signed,
expirationDate: null,
},
});
} finally {
close(transport, deviceId);
}
}

main().then(
() => o.complete(),
(e) => o.error(e)
);
});
})
);

export default signOperation;
Loading

0 comments on commit 3a60120

Please sign in to comment.