Skip to content

Commit

Permalink
feat: add sign actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesposito committed Sep 4, 2023
1 parent 44713eb commit 73ad7a1
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 2 deletions.
117 changes: 116 additions & 1 deletion packages/keyring-controller/src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1904,6 +1904,116 @@ describe('KeyringController', () => {
});
});
});

describe('actions', () => {
describe('signMessage', () => {
it('should sign message', async () => {
await withController(async ({ messenger, initialState }) => {
const messageParams = {
from: initialState.keyrings[0].accounts[0],
data: '0x1234',
};

const signature = await messenger.call(
'KeyringController:signMessage',
messageParams,
);

expect(signature).not.toBe('');
});
});
});

describe('signPersonalMessage', () => {
it('should sign personal message', async () => {
await withController(async ({ messenger, initialState }) => {
const messageParams = {
from: initialState.keyrings[0].accounts[0],
data: '0x1234',
};

const signature = await messenger.call(
'KeyringController:signPersonalMessage',
messageParams,
);

expect(signature).not.toBe('');
});
});
});

describe('signTypedMessage', () => {
it('should call signTypedMessage', async () => {
await withController(async ({ messenger, initialState }) => {
const msgParams = {
domain: {
chainId: 1,
name: 'Ether Mail',
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
version: '1',
},
message: {
contents: 'Hello, Bob!',
from: {
name: 'Cow',
wallets: [
'0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
'0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF',
],
},
to: [
{
name: 'Bob',
wallets: [
'0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
'0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57',
'0xB0B0b0b0b0b0B000000000000000000000000000',
],
},
],
},
primaryType: 'Mail' as const,
types: {
EIP712Domain: [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' },
{ name: 'chainId', type: 'uint256' },
{ name: 'verifyingContract', type: 'address' },
],
Group: [
{ name: 'name', type: 'string' },
{ name: 'members', type: 'Person[]' },
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person[]' },
{ name: 'contents', type: 'string' },
],
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallets', type: 'address[]' },
],
},
};

const signer = initialState.keyrings[0].accounts[0];
const signature = await messenger.call(
'KeyringController:signTypedMessage',
{ data: JSON.stringify(msgParams), from: signer },
SignTypedDataVersion.V4,
);
const recovered = recoverTypedSignature({
data: msgParams,
signature,
version: SignTypedDataVersion.V4,
});

expect(signer).toBe(recovered);
expect(signature).not.toBe('');
});
});
});
});
});

type WithControllerCallback<ReturnValue> = ({
Expand Down Expand Up @@ -1954,7 +2064,12 @@ function buildMessenger() {
function buildKeyringControllerMessenger(messenger = buildMessenger()) {
return messenger.getRestricted({
name: 'KeyringController',
allowedActions: [],
allowedActions: [
'KeyringController:getState',
'KeyringController:signMessage',
'KeyringController:signPersonalMessage',
'KeyringController:signTypedMessage',
],
allowedEvents: [
'KeyringController:stateChange',
'KeyringController:lock',
Expand Down
44 changes: 43 additions & 1 deletion packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ export type KeyringControllerGetStateAction = {
handler: () => KeyringControllerState;
};

export type KeyringControllerSignMessageAction = {
type: `${typeof name}:signMessage`;
handler: KeyringController['signMessage'];
};

export type KeyringControllerSignPersonalMessageAction = {
type: `${typeof name}:signPersonalMessage`;
handler: KeyringController['signPersonalMessage'];
};

export type KeyringControllerSignTypedMessageAction = {
type: `${typeof name}:signTypedMessage`;
handler: KeyringController['signTypedMessage'];
};

export type KeyringControllerStateChangeEvent = {
type: `${typeof name}:stateChange`;
payload: [KeyringControllerState, Patch[]];
Expand All @@ -85,7 +100,11 @@ export type KeyringControllerUnlockEvent = {
payload: [];
};

export type KeyringControllerActions = KeyringControllerGetStateAction;
export type KeyringControllerActions =
| KeyringControllerGetStateAction
| KeyringControllerSignMessageAction
| KeyringControllerSignPersonalMessageAction
| KeyringControllerSignTypedMessageAction;

export type KeyringControllerEvents =
| KeyringControllerStateChangeEvent
Expand Down Expand Up @@ -256,6 +275,8 @@ export class KeyringController extends BaseControllerV2<
this.updateIdentities = updateIdentities;
this.setSelectedAddress = setSelectedAddress;
this.setAccountLabel = setAccountLabel;

this.#registerMessageHandlers();
}

/**
Expand Down Expand Up @@ -905,6 +926,27 @@ export class KeyringController extends BaseControllerV2<
await this.#keyring.persistAllKeyrings();
}

/**
* Constructor helper for registering this controller's messaging system
* actions.
*/
#registerMessageHandlers() {
this.messagingSystem.registerActionHandler(
`${name}:signMessage`,
this.signMessage.bind(this),
);

this.messagingSystem.registerActionHandler(
`${name}:signPersonalMessage`,
this.signPersonalMessage.bind(this),
);

this.messagingSystem.registerActionHandler(
`${name}:signTypedMessage`,
this.signTypedMessage.bind(this),
);
}

/**
* Add qr hardware keyring.
*
Expand Down

0 comments on commit 73ad7a1

Please sign in to comment.