Skip to content

Add eth_getPlumeSignature RPC handler #198

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

Open
wants to merge 4 commits into
base: main
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
91 changes: 91 additions & 0 deletions src/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,94 @@ describe('wallet', () => {
});
});
});

describe('getPlumeSignature', () => {
it('should sign with a valid address', async () => {
const { engine } = createTestSetup();
const getAccounts = async () => testAddresses.slice();
const witnessedMsgParams: MessageParams[] = [];
const processPlumeSignature = async (msgParams: MessageParams) => {
witnessedMsgParams.push(msgParams);
return testMsgSig;
};

engine.push(createWalletMiddleware({ getAccounts, processPlumeSignature }));
const message = [
{
type: 'string',
name: 'message',
value: 'Hi, Alice!',
},
];

const payload = {
method: 'eth_getPlumeSignature',
params: [message, testAddresses[0]],
};
const signMsgResponse = await pify(engine.handle).call(engine, payload);
const signMsgResult = signMsgResponse.result;

expect(signMsgResult).toBeDefined();
expect(signMsgResult).toStrictEqual(testMsgSig);
expect(witnessedMsgParams).toHaveLength(1);
expect(witnessedMsgParams[0]).toStrictEqual({
from: testAddresses[0],
data: message,
});
});

it('should throw with invalid address', async () => {
const { engine } = createTestSetup();
const getAccounts = async () => testAddresses.slice();
const witnessedMsgParams: MessageParams[] = [];
const processPlumeSignature = async (msgParams: MessageParams) => {
witnessedMsgParams.push(msgParams);
return testMsgSig;
};

engine.push(createWalletMiddleware({ getAccounts, processPlumeSignature }));
const message = [
{
type: 'string',
name: 'message',
value: 'Hi, Alice!',
},
];

const payload = {
method: 'eth_getPlumeSignature',
params: [message, '0x3d'],
};
await expect(pify(engine.handle).call(engine, payload)).rejects.toThrow(
new Error('Invalid parameters: must provide an Ethereum address.'),
);
});

it('should throw with unknown address', async () => {
const { engine } = createTestSetup();
const getAccounts = async () => testAddresses.slice();
const witnessedMsgParams: MessageParams[] = [];
const processPlumeSignature = async (msgParams: MessageParams) => {
witnessedMsgParams.push(msgParams);
return testMsgSig;
};

engine.push(createWalletMiddleware({ getAccounts, processPlumeSignature }));
const message = [
{
type: 'string',
name: 'message',
value: 'Hi, Alice!',
},
];

const payload = {
method: 'eth_getPlumeSignature',
params: [message, testUnkownAddress],
};
const promise = pify(engine.handle).call(engine, payload);
await expect(promise).rejects.toThrow(
'The requested account and/or method has not been authorized by the user.',
);
});
});
30 changes: 30 additions & 0 deletions src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export interface WalletMiddlewareOptions {
req: JsonRpcRequest<unknown>,
version: string,
) => Promise<string>;
processPlumeSignature?: (
msgParams: MessageParams,
req: JsonRpcRequest<unknown>,
) => Promise<string>;
}

export function createWalletMiddleware({
Expand All @@ -78,6 +82,7 @@ export function createWalletMiddleware({
processTypedMessage,
processTypedMessageV3,
processTypedMessageV4,
processPlumeSignature,
}: WalletMiddlewareOptions): JsonRpcMiddleware<string, Block> {
if (!getAccounts) {
throw new Error('opts.getAccounts is required');
Expand All @@ -99,6 +104,7 @@ export function createWalletMiddleware({
eth_getEncryptionPublicKey: createAsyncMiddleware(encryptionPublicKey),
eth_decrypt: createAsyncMiddleware(decryptMessage),
personal_ecRecover: createAsyncMiddleware(personalRecover),
eth_getPlumeSignature: createAsyncMiddleware(plumeSignature),
});

//
Expand Down Expand Up @@ -353,6 +359,30 @@ export function createWalletMiddleware({
res.result = await processDecryptMessage(msgParams, req);
}

async function plumeSignature(
req: JsonRpcRequest<unknown>,
res: PendingJsonRpcResponse<unknown>,
): Promise<void> {
if (!processPlumeSignature) {
throw ethErrors.rpc.methodNotSupported();
}

const message: string = (req.params as string[])[0];
const address: string = await validateAndNormalizeKeyholder(
(req.params as string[])[1],
req,
);
const extraParams: Record<string, unknown> =
(req.params as Record<string, unknown>[])[2] || {};
const msgParams: MessageParams = {
...extraParams,
from: address,
data: message,
};

res.result = await processPlumeSignature(msgParams, req);
}

//
// utility
//
Expand Down