Skip to content

Commit

Permalink
fix: missing ACRE signIn handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Justkant committed Oct 29, 2024
1 parent 6510d1e commit 72c31a6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 26 deletions.
2 changes: 1 addition & 1 deletion libs/ledger-live-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"https": false
},
"dependencies": {
"@blooo/hw-app-acre": "^1.0.1",
"@blooo/hw-app-acre": "^1.1.1",
"@cardano-foundation/ledgerjs-hw-app-cardano": "^7.1.2",
"@celo/connect": "^3.0.1",
"@celo/contractkit": "^3.0.1",
Expand Down
33 changes: 32 additions & 1 deletion libs/ledger-live-common/src/families/bitcoin/ACRESetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { GetAddressFn } from "@ledgerhq/coin-framework/bridge/getAddressWrapper"
import { getCurrencyConfiguration } from "../../config";
import { BitcoinConfigInfo } from "@ledgerhq/coin-bitcoin/lib/config";
import { SignMessage } from "../../hw/signMessage/types";
import { AcreMessageWithdraw } from "@ledgerhq/wallet-api-acre-module";
import { AcreMessageSignIn, AcreMessageWithdraw } from "@ledgerhq/wallet-api-acre-module";

const createSigner = (transport: Transport, currency: CryptoCurrency) => {
return new Acre({ transport, currency: currency.id });
Expand Down Expand Up @@ -75,9 +75,40 @@ export function createWithdrawSigner(): SignMessage {
};
}

export function createSignInSigner(): SignMessage {
const signIn =
(signerContext: SignerContext) =>
async (deviceId: string, account: Account, container: AcreMessageSignIn & AnyMessage) => {
const path =
"path" in container && container.path ? container.path : account.freshAddressPath;
const result = (await signerContext(deviceId, account.currency, signer =>
(signer as unknown as Acre).signERC4361Message(
path,
Buffer.from(container.message as string).toString("hex"),
),
)) as BitcoinSignature;
const v = result["v"] + 27 + 4;
const signature = `${v.toString(16)}${result["r"]}${result["s"]}`;
return {
rsv: result,
signature,
};
};

return (transport, account, messageData) => {
const signerContext: SignerContext = (_, crypto, fn) => fn(createSigner(transport, crypto));
return signIn(signerContext)(
"",
account,
messageData as unknown as AcreMessageSignIn & AnyMessage,
);
};
}

const messageSigner = {
signMessage: createMessageSigner(),
signWithdraw: createWithdrawSigner(),
signIn: createSignInSigner(),
};

const resolver: Resolver = (
Expand Down
17 changes: 12 additions & 5 deletions libs/ledger-live-common/src/hw/signMessage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ const signMessage: SignMessage = (transport, account, opts) => {
const { currency } = account;
let signMessage = perFamily[currency.family].signMessage;
if ("type" in opts) {
signMessage =
opts.type === AcreMessageType.Withdraw
? ACREMessageSigner.signWithdraw
: ACREMessageSigner.signMessage;
switch (opts.type) {
case AcreMessageType.Withdraw:
signMessage = ACREMessageSigner.signWithdraw;
break;
case AcreMessageType.SignIn:
signMessage = ACREMessageSigner.signIn;
break;
default:
signMessage = ACREMessageSigner.signMessage;
break;
}
}
invariant(signMessage, `signMessage is not implemented for ${currency.id}`);
return signMessage(transport, account, opts)
Expand Down Expand Up @@ -103,7 +110,7 @@ export const createAction = (
const appState: AppState = createAppAction(connectAppExec).useHook(reduxDevice, {
appName: request.appName,
dependencies: request.dependencies,
account: request.account,
account: request.appName ? undefined : request.account,
});
const { device, opened, inWrongDeviceForAccount, error } = appState;
const [state, setState] = useState<BaseState>({
Expand Down
45 changes: 26 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 72c31a6

Please sign in to comment.