Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ability to signMessage with Ledger adapter #712

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilled-badgers-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@solana/wallet-adapter-ledger': patch
---

Adding ability to signMessage with Ledger adapter
20 changes: 19 additions & 1 deletion packages/wallets/ledger/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import {
WalletPublicKeyError,
WalletReadyState,
WalletSignTransactionError,
WalletSignMessageError,
} from '@solana/wallet-adapter-base';
import type { PublicKey, Transaction } from '@solana/web3.js';
import './polyfills/index.js';
import { getDerivationPath, getPublicKey, signTransaction } from './util.js';
import { getDerivationPath, getPublicKey, signTransaction, signMessage } from './util.js';

export interface LedgerWalletAdapterConfig {
derivationPath?: Buffer;
Expand Down Expand Up @@ -142,6 +143,23 @@ export class LedgerWalletAdapter extends BaseSignerWalletAdapter {
}
}

async signMessage(message: Uint8Array): Promise<Uint8Array> {
try {
try {
const transport = this._transport;
if (!transport) throw new WalletNotConnectedError();

const signature = await signMessage(transport, Buffer.from(message.buffer), this._derivationPath);
return new Uint8Array(signature);
} catch (error: any) {
throw new WalletSignMessageError(error?.message, error);
}
} catch (error: any) {
this.emit('error', error);
throw error;
}
}

private _disconnected = () => {
const transport = this._transport;
if (transport) {
Expand Down
11 changes: 11 additions & 0 deletions packages/wallets/ledger/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function harden(n: number): number {

const INS_GET_PUBKEY = 0x05;
const INS_SIGN_MESSAGE = 0x06;
const INS_SIGN_MESSAGE_OFFCHAIN = 0x07;

const P1_NON_CONFIRM = 0x00;
const P1_CONFIRM = 0x01;
Expand Down Expand Up @@ -62,6 +63,16 @@ export async function signTransaction(
return await send(transport, INS_SIGN_MESSAGE, P1_CONFIRM, data);
}

/** @internal */
export async function signMessage(transport: Transport, message: Buffer, derivationPath: Buffer): Promise<Buffer> {
const paths = Buffer.alloc(1);
paths.writeUInt8(1, 0);

const data = Buffer.concat([paths, derivationPath, message]);

return await send(transport, INS_SIGN_MESSAGE_OFFCHAIN, P1_CONFIRM, data);
}
Comment on lines +66 to +74
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks great. I think we'll need to add some things to the adapter/utils to help apps prepare the right message format to sign, but this is a good start.


async function send(transport: Transport, instruction: number, p1: number, data: Buffer): Promise<Buffer> {
let p2 = 0;
let offset = 0;
Expand Down