Skip to content

Commit

Permalink
refactor(experimental): add signers package (#1710)
Browse files Browse the repository at this point in the history
This PR adds the `@solana/signers` package **with the first part of its content** that focuses on adding the five signer interfaces with their type guards as well as a concrete implementation of these signers via the `KeyPairSigner` type.

I made sure to document the README immediately so you can have the full picture of what's available in this PR and how to use it.

Below is a copy/paste of that README:

---

You can think of signers as an abstract way to sign messages and transactions. This could be using a Crypto KeyPair, a wallet adapter in the browser, a Noop signer for testing purposes, or anything you want. Here's an example using a `CryptoKeyPair` signer:

```ts
import { pipe } from '@solana/functional';
import { generateKeyPairSigner } from '@solana/signers';
import { createTransaction } from '@solana/transactions';

// Generate a key pair signer.
const mySigner = await generateKeyPairSigner();
mySigner.address; // Address;

// Sign one or multiple messages.
const myMessage = createSignableMessage('Hello world!');
const [messageSignatures] = await mySigner.signMessages([myMessage]);

// Sign one or multiple transactions.
const myTransaction = pipe(
    createTransaction({ version: 0 })
    // Add instructions, fee payer, lifetime, etc.
);
const [transactionSignatures] = await mySigner.signTransactions([myTransaction]);
```

As you can see, this provides a consistent API regardless of how things are being signed behind the scenes. If tomorrow we need to use a browser wallet instead, we'd simply need to swap the `generateKeyPairSigner` function with the signer factory of our choice.

This package offers a total of five different types of signers that may be used in combination when applicable. Three of them allow us to sign transactions whereas the other two are used for regular message signing.

They are separated into three categories:

-   **Partial signers**: Given a message or transaction, provide one or more signatures for it. These signers are not able to modify the given data which allows us to run many of them in parallel.
-   **Modifying signers**: Can choose to modify a message or transaction before signing it with zero or more private keys. Because modifying a message or transaction invalidates any pre-existing signatures over it, modifying signers must do their work before any other signer.
-   **Sending signers**: Given a transaction, signs it and sends it immediately to the blockchain. When applicable, the signer may also decide to modify the provided transaction before signing it. This interface accommodates wallets that simply cannot sign a transaction without sending it at the same time. This category of signers does not apply to regular messages.

Thus, we end up with the following interfaces.

|                     | Partial signers            | Modifying signers            | Sending signers            |
| ------------------- | -------------------------- | ---------------------------- | -------------------------- |
| `TransactionSigner` | `TransactionPartialSigner` | `TransactionModifyingSigner` | `TransactionSendingSigner` |
| `MessageSigner`     | `MessagePartialSigner`     | `MessageModifyingSigner`     | N/A                        |

We will go through each of these five signer interfaces and their respective characteristics in the documentation below.

This package also provides the following concrete signer implementations:

-   The `KeyPairSigner` which uses a `CryptoKeyPair` to sign messages and transactions.
-   The Noop signer which does not sign anything and is mostly useful for testing purposes or for indicating that an account will be signed in a different environment (e.g. sending a transaction to your server so it can sign it).

Additionally, this package allows transaction signers to be stored inside the account meta of an instruction. This allows us to create instructions by passing around signers instead of addresses when applicable which, in turn, allows us to sign an entire transaction automatically without having to scan through its instructions to find the required signers.

In the sections below, we'll go through all the provided signers in more detail before diving into storing signers inside instruction account metas and how to benefit from it.

## Signing messages

### Types

#### `SignableMessage`

Defines a message with any of the signatures that might have already been provided by other signers. This interface allows modifying signers to decide on whether or not they should modify the provided message depending on whether or not signatures already exist for such message. It also helps create a more consistent API by providing a structure analogous to transactions which also keep track of their signature dictionary.

```ts
type SignableMessage = {
    content: Uint8Array;
    signatures: SignatureDictionary; // Record<Address, SignatureBytes>
};
```

#### `MessagePartialSigner<TAddress>`

An interface that signs an array of `SignableMessages` without modifying their content. It defines a `signMessages` function that returns a `SignatureDictionary` for each provided message. Such signature dictionaries are expected to be merged with the existing ones if any.

```ts
const myMessagePartialSigner: MessagePartialSigner<'1234..5678'> = {
    address: address('1234..5678'),
    signMessages: async (messages: SignableMessage[]): Promise<SignatureDictionary[]> => {
        // My custom signing logic.
    },
};
```

**Characteristics**:

-   **Parallel**. When multiple signers sign the same message, we can perform this operation in parallel to obtain all their signatures.
-   **Flexible order**. The order in which we use these signers for a given message doesn’t matter.

#### `MessageModifyingSigner<TAddress>`

An interface that potentially modifies the content of the provided `SignableMessages` before signing them. E.g. this enables wallets to prefix or suffix nonces to the messages they sign. For each message, instead of returning a `SignatureDirectory`, its `modifyAndSignMessages` function returns its updated `SignableMessage` with a potentially modified content and signature dictionary.

```ts
const myMessageModifyingSigner: MessageModifyingSigner<'1234..5678'> = {
    address: address('1234..5678'),
    modifyAndSignMessages: async (messages: SignableMessage[]): Promise<SignableMessage[]> => {
        // My custom signing logic.
    },
};
```

**Characteristics**:

-   **Sequential**. Contrary to partial signers, these cannot be executed in parallel as each call can modify the content of the message.
-   **First signers**. For a given message, a modifying signer must always be used before a partial signer as the former will likely modify the message and thus impact the outcome of the latter.
-   **Potential conflicts**. If more than one modifying signer is provided, the second signer may invalidate the signature of the first one. However, modifying signers may decide not to modify a message based on the existence of signatures for that message.

#### `MessageSigner<TAddress>`

Union interface that uses any of the available message signers.

```ts
type MessageSigner<TAddress extends string = string> =
    | MessagePartialSigner<TAddress>
    | MessageModifyingSigner<TAddress>;
```

### Functions

#### `createSignableMessage(content, signatures?)`

Creates a `SignableMessage` from a `Uint8Array` or a UTF-8 string. It optionally accepts a signature dictionary if the message already contains signatures.

```ts
const myMessage = createSignableMessage(new Uint8Array([1, 2, 3]));
const myMessageFromText = createSignableMessage('Hello world!');
const myMessageWithSignatures = createSignableMessage('Hello world!', {
    '1234..5678': new Uint8Array([1, 2, 3]),
});
```

#### Type guards

Each of the message interfaces described above comes with two type guards that allow us to check whether or not a given value is a message signer of the requested type. One that returns a boolean and one that asserts by throwing an error if the provided value is not of the expected interface.

```ts
const myAddress = address('1234..5678');

isMessagePartialSigner({ address: myAddress, signMessages: async () => {} }); // ✅ true
isMessagePartialSigner({ address: myAddress }); // ❌ false
assertIsMessagePartialSigner({ address: myAddress, signMessages: async () => {} }); // ✅ void
assertIsMessagePartialSigner({ address: myAddress }); // ❌ Throws an error.

isMessageModifyingSigner({ address: myAddress, modifyAndSignMessages: async () => {} }); // ✅ true
isMessageModifyingSigner({ address: myAddress }); // ❌ false
assertIsMessageModifyingSigner({ address: myAddress, modifyAndSignMessages: async () => {} }); // ✅ void
assertIsMessageModifyingSigner({ address: myAddress }); // ❌ Throws an error.

isMessageSigner({ address: myAddress, signMessages: async () => {} }); // ✅ true
isMessageSigner({ address: myAddress, modifyAndSignMessages: async () => {} }); // ✅ true
assertIsMessageSigner({ address: myAddress, signMessages: async () => {} }); // ✅ void
assertIsMessageSigner({ address: myAddress, modifyAndSignMessages: async () => {} }); // ✅ void
```

## Signing transactions

### Types

#### `TransactionPartialSigner<TAddress>`

An interface that signs an array of `CompilableTransactions` without modifying their content. It defines a `signTransactions` function that returns a `SignatureDictionary` for each provided transaction. Such signature dictionaries are expected to be merged with the existing ones if any.

```ts
const myTransactionPartialSigner: TransactionPartialSigner<'1234..5678'> = {
    address: address('1234..5678'),
    signTransactions: async (transactions: CompilableTransaction[]): Promise<SignatureDictionary[]> => {
        // My custom signing logic.
    },
};
```

**Characteristics**:

-   **Parallel**. It returns a signature directory for each provided transaction without modifying them, making it possible for multiple partial signers to sign the same transaction in parallel.
-   **Flexible order**. The order in which we use these signers for a given transaction doesn’t matter.

#### `TransactionModifyingSigner<TAddress>`

An interface that potentially modifies the provided `CompilableTransactions` before signing them. E.g. this enables wallets to inject additional instructions into the transaction before signing them. For each transaction, instead of returning a `SignatureDirectory`, its `modifyAndSignTransactions` function returns an updated `CompilableTransaction` with a potentially modified set of instructions and signature dictionary.

```ts
const myTransactionModifyingSigner: TransactionModifyingSigner<'1234..5678'> = {
    address: address('1234..5678'),
    modifyAndSignTransactions: async <T extends CompilableTransaction>(transactions: T[]): Promise<T[]> => {
        // My custom signing logic.
    },
};
```

**Characteristics**:

-   **Sequential**. Contrary to partial signers, these cannot be executed in parallel as each call can modify the provided transactions.
-   **First signers**. For a given transaction, a modifying signer must always be used before a partial signer as the former will likely modify the transaction and thus impact the outcome of the latter.
-   **Potential conflicts**. If more than one modifying signer is provided, the second signer may invalidate the signature of the first one. However, modifying signers may decide not to modify a transaction based on the existence of signatures for that transaction.

#### `TransactionSendingSigner<TAddress>`

An interface that signs one or multiple transactions before sending them immediately to the blockchain. It defines a `signAndSendTransactions` function that returns the transaction signature (i.e. its identifier) for each provided `CompilableTransaction`. This interface is required for PDA wallets and other types of wallets that don't provide an interface for signing transactions without sending them.

Note that it is also possible for such signers to modify the provided transactions before signing and sending them. This enables use cases where the modified transactions cannot be shared with the app and thus must be sent directly.

```ts
const myTransactionSendingSigner: TransactionSendingSigner<'1234..5678'> = {
    address: address('1234..5678'),
    signAndSendTransactions: async (transactions: CompilableTransaction[]): Promise<SignatureBytes[]> => {
        // My custom signing logic.
    },
};
```

**Characteristics**:

-   **Single signer**. Since this signer also sends the provided transactions, we can only use a single `TransactionSendingSigner` for a given set of transactions.
-   **Last signer**. Trivially, that signer must also be the last one used.
-   **Potential conflicts**. Since signers may decide to modify the given transactions before sending them, they may invalidate previous signatures. However, signers may decide not to modify a transaction based on the existence of signatures for that transaction.
-   **Potential confirmation**. Whilst this is not required by this interface, it is also worth noting that most wallets will also wait for the transaction to be confirmed (typically with a `confirmed` commitment) before notifying the app that they are done.

#### `TransactionSigner<TAddress>`

Union interface that uses any of the available transaction signers.

```ts
type TransactionSigner<TAddress extends string = string> =
    | TransactionPartialSigner<TAddress>
    | TransactionModifyingSigner<TAddress>
    | TransactionSendingSigner<TAddress>;
```

### Functions

#### Type guards

Each of the transaction interfaces described above comes with two type guards that allow us to check whether or not a given value is a transaction signer of the requested type. One that returns a boolean and one that asserts by throwing an error if the provided value is not of the expected interface.

```ts
const myAddress = address('1234..5678');

isTransactionPartialSigner({ address: myAddress, signTransactions: async () => {} }); // ✅ true
isTransactionPartialSigner({ address: myAddress }); // ❌ false
assertIsTransactionPartialSigner({ address: myAddress, signTransactions: async () => {} }); // ✅ void
assertIsTransactionPartialSigner({ address: myAddress }); // ❌ Throws an error.

isTransactionModifyingSigner({ address: myAddress, modifyAndSignTransactions: async () => {} }); // ✅ true
isTransactionModifyingSigner({ address: myAddress }); // ❌ false
assertIsTransactionModifyingSigner({ address: myAddress, modifyAndSignTransactions: async () => {} }); // ✅ void
assertIsTransactionModifyingSigner({ address: myAddress }); // ❌ Throws an error.

isTransactionSendingSigner({ address: myAddress, signAndSignTransaction: async () => {} }); // ✅ true
isTransactionSendingSigner({ address: myAddress }); // ❌ false
assertIsTransactionSendingSigner({ address: myAddress, signAndSignTransaction: async () => {} }); // ✅ void
assertIsTransactionSendingSigner({ address: myAddress }); // ❌ Throws an error.

isTransactionSigner({ address: myAddress, signTransactions: async () => {} }); // ✅ true
isTransactionSigner({ address: myAddress, modifyAndSignTransactions: async () => {} }); // ✅ true
isTransactionSigner({ address: myAddress, signAndSignTransaction: async () => {} }); // ✅ true
assertIsTransactionSigner({ address: myAddress, signTransactions: async () => {} }); // ✅ void
assertIsTransactionSigner({ address: myAddress, modifyAndSignTransactions: async () => {} }); // ✅ void
assertIsTransactionSigner({ address: myAddress, signAndSignTransaction: async () => {} }); // ✅ void
```

## Creating and generating KeyPair signers

### Types

#### `KeyPairSigner<TAddress>`

Defines a signer that uses a `CryptoKeyPair` to sign messages and transactions. It implements both the `MessagePartialSigner` and `TransactionPartialSigner` interfaces and keeps track of the `CryptoKeyPair` instance used to sign messages and transactions.

```ts
import { generateKeyPairSigner } from '@solana/signers';

const myKeyPairSigner = generateKeyPairSigner();
myKeyPairSigner.address; // Address;
myKeyPairSigner.keyPair; // CryptoKeyPair;
const [myMessageSignatures] = await myKeyPairSigner.signMessages([myMessage]);
const [myTransactionSignatures] = await myKeyPairSigner.signTransactions([myTransaction]);
```

### Functions

#### `createSignerFromKeyPair()`

Creates a `KeyPairSigner` from a provided Crypto KeyPair. The `signMessages` and `signTransactions` functions of the returned signer will use the private key of the provided key pair to sign messages and transactions. Note that both the `signMessages` and `signTransactions` implementations are parallelized, meaning that they will sign all provided messages and transactions in parallel.

```ts
import { generateKeyPair } from '@solana/keys';
import { createSignerFromKeyPair, KeyPairSigner } from '@solana/signers';

const myKeyPair: CryptoKeyPair = await generateKeyPair();
const myKeyPairSigner: KeyPairSigner = await createSignerFromKeyPair(myKeyPair);
```

#### `generateKeyPairSigner()`

A convenience function that generates a new Crypto KeyPair and immediately creates a `KeyPairSigner` from it.

```ts
import { generateKeyPairSigner } from '@solana/signers';

const myKeyPairSigner = await generateKeyPairSigner();
```

#### `isKeyPairSigner()`

A type guard that returns `true` if the provided value is a `KeyPairSigner`.

```ts
const myKeyPairSigner = await generateKeyPairSigner();
isKeyPairSigner(myKeyPairSigner); // ✅ true
isKeyPairSigner({ address: address('1234..5678') }); // ❌ false
```

#### `assertIsKeyPairSigner()`

A type guard that throws an error if the provided value is not a `KeyPairSigner`.

```ts
const myKeyPairSigner = await generateKeyPairSigner();
assertIsKeyPairSigner(myKeyPairSigner); // ✅ void
assertIsKeyPairSigner({ address: address('1234..5678') }); // ❌ Throws an error.
```

## Creating Noop signers

### Functions

#### `createNoopSigner()`

_Coming soon..._

Creates a Noop (No-Operation) signer from a given address. It will return an implementation of both the `MessagePartialSigner` and `TransactionPartialSigner` interfaces that do not sign anything. Namely, signing a transaction or a message will return an empty `SignatureDictionary`.

This signer may be useful:

-   For testing purposes.
-   For indicating that a given account is a signer and taking the responsibility to provide the signature for that account ourselves. For instance, if we need to send the transaction to a server that will sign it and send it for us.

```ts
import { createNoopSigner } from '@solana/signers';

const myAddress = address('1234..5678');
const myNoopSigner = createNoopSigner(myAddress);
// ^ MessagePartialSigner<'1234..5678'> & TransactionPartialSigner<'1234..5678'>
```

## Storing transaction signers inside instruction account metas

### Types

_Coming soon..._

### Functions

_Coming soon..._
  • Loading branch information
lorisleiva committed Nov 15, 2023
1 parent 26136d9 commit 7c29a1e
Show file tree
Hide file tree
Showing 38 changed files with 1,729 additions and 67 deletions.
1 change: 1 addition & 0 deletions packages/signers/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
1 change: 1 addition & 0 deletions packages/signers/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
1 change: 1 addition & 0 deletions packages/signers/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
20 changes: 20 additions & 0 deletions packages/signers/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2018 Solana Labs, Inc

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
368 changes: 368 additions & 0 deletions packages/signers/README.md

Large diffs are not rendered by default.

104 changes: 104 additions & 0 deletions packages/signers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"name": "@solana/signers",
"version": "2.0.0-development",
"description": "An abstraction layer over signing messages and transactions in Solana",
"exports": {
"browser": {
"import": "./dist/index.browser.js",
"require": "./dist/index.browser.cjs"
},
"node": {
"import": "./dist/index.node.js",
"require": "./dist/index.node.cjs"
},
"react-native": "./dist/index.native.js",
"types": "./dist/types/index.d.ts"
},
"browser": {
"./dist/index.node.cjs": "./dist/index.browser.cjs",
"./dist/index.node.js": "./dist/index.browser.js"
},
"main": "./dist/index.node.cjs",
"module": "./dist/index.node.js",
"react-native": "./dist/index.native.js",
"types": "./dist/types/index.d.ts",
"type": "module",
"files": [
"./dist/"
],
"sideEffects": false,
"keywords": [
"blockchain",
"solana",
"web3"
],
"scripts": {
"compile:js": "tsup --config build-scripts/tsup.config.library.ts",
"compile:typedefs": "tsc -p ./tsconfig.declarations.json",
"dev": "jest -c node_modules/test-config/jest-dev.config.ts --rootDir . --watch",
"prepublishOnly": "version-from-git --no-git-tag-version --template experimental.short",
"publish-packages": "pnpm publish --tag experimental --access public --no-git-checks",
"style:fix": "pnpm eslint --fix src/* && pnpm prettier -w src/*",
"test:lint": "jest -c node_modules/test-config/jest-lint.config.ts --rootDir . --silent",
"test:prettier": "jest -c node_modules/test-config/jest-prettier.config.ts --rootDir . --silent",
"test:treeshakability:browser": "agadoo dist/index.browser.js",
"test:treeshakability:native": "agadoo dist/index.native.js",
"test:treeshakability:node": "agadoo dist/index.node.js",
"test:typecheck": "tsc --noEmit",
"test:unit:browser": "jest -c node_modules/test-config/jest-unit.config.browser.ts --rootDir . --silent",
"test:unit:node": "jest -c node_modules/test-config/jest-unit.config.node.ts --rootDir . --silent"
},
"author": "Solana Labs Maintainers <maintainers@solanalabs.com>",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/solana-labs/solana-web3.js"
},
"bugs": {
"url": "http://github.com/solana-labs/solana-web3.js/issues"
},
"browserslist": [
"supports bigint and not dead",
"maintained node versions"
],
"engine": {
"node": ">=17.4"
},
"dependencies": {
"@solana/addresses": "workspace:*",
"@solana/keys": "workspace:*",
"@solana/transactions": "workspace:*"
},
"devDependencies": {
"@solana/eslint-config-solana": "^1.0.2",
"@swc/jest": "^0.2.29",
"@types/jest": "^29.5.6",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.3.0",
"agadoo": "^3.0.0",
"build-scripts": "workspace:*",
"eslint": "^8.45.0",
"eslint-plugin-jest": "^27.4.2",
"eslint-plugin-sort-keys-fix": "^1.1.2",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jest-runner-eslint": "^2.1.2",
"jest-runner-prettier": "^1.0.0",
"prettier": "^2.8",
"test-config": "workspace:*",
"test-matchers": "workspace:*",
"text-encoding-impl": "workspace:*",
"tsconfig": "workspace:*",
"tsup": "7.2.0",
"typescript": "^5.2.2",
"version-from-git": "^1.1.1"
},
"bundlewatch": {
"defaultCompression": "gzip",
"files": [
{
"path": "./dist/index*.js"
}
]
}
}
210 changes: 210 additions & 0 deletions packages/signers/src/__tests__/keypair-signer-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
import 'test-matchers/toBeFrozenObject';

import { address, getAddressFromPublicKey } from '@solana/addresses';
import { generateKeyPair, SignatureBytes, signBytes } from '@solana/keys';
import { CompilableTransaction, signTransaction } from '@solana/transactions';

import {
assertIsKeyPairSigner,
createSignerFromKeyPair,
generateKeyPairSigner,
isKeyPairSigner,
KeyPairSigner,
} from '../keypair-signer';
import { createSignableMessage } from '../signable-message';

const getMockCryptoKeyPair = () => ({ privateKey: {}, publicKey: {} } as CryptoKeyPair);

// Partial mocks.
jest.mock('@solana/addresses', () => ({
...jest.requireActual('@solana/addresses'),
getAddressFromPublicKey: jest.fn(),
}));
jest.mock('@solana/keys', () => ({
...jest.requireActual('@solana/keys'),
generateKeyPair: jest.fn(),
signBytes: jest.fn(),
}));
jest.mock('@solana/transactions', () => ({
...jest.requireActual('@solana/transactions'),
signTransaction: jest.fn(),
}));

describe('isKeyPairSigner', () => {
it('checks whether a given value is a KeyPairSigner', () => {
const myAddress = address('Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy');
const mySigner = {
address: myAddress,
keyPair: getMockCryptoKeyPair(),
signMessages: async () => [],
signTransactions: async () => [],
} satisfies KeyPairSigner<'Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy'>;

expect(isKeyPairSigner(mySigner)).toBe(true);
expect(isKeyPairSigner({ address: myAddress })).toBe(false);
expect(isKeyPairSigner({ ...mySigner, signMessages: 42 })).toBe(false);
expect(isKeyPairSigner({ ...mySigner, signTransactions: 42 })).toBe(false);
expect(isKeyPairSigner({ ...mySigner, keyPair: 42 })).toBe(false);
});
});

describe('assertIsKeyPairSigner', () => {
it('asserts that a given value is a KeyPairSigner', () => {
const myAddress = address('Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy');
const mySigner = {
address: myAddress,
keyPair: getMockCryptoKeyPair(),
signMessages: async () => [],
signTransactions: async () => [],
} satisfies KeyPairSigner<'Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy'>;

const expectedMessage = 'The provided value does not implement the KeyPairSigner interface';
expect(() => assertIsKeyPairSigner(mySigner)).not.toThrow();
expect(() => assertIsKeyPairSigner({ address: myAddress })).toThrow(expectedMessage);
expect(() => assertIsKeyPairSigner({ ...mySigner, signMessages: 42 })).toThrow(expectedMessage);
expect(() => assertIsKeyPairSigner({ ...mySigner, signTransactions: 42 })).toThrow(expectedMessage);
expect(() => assertIsKeyPairSigner({ ...mySigner, keyPair: 42 })).toThrow(expectedMessage);
});
});

describe('createSignerFromKeyPair', () => {
it('creates a KeyPairSigner from a given CryptoKeypair', async () => {
expect.assertions(5);

// Given a mock CryptoKeyPair returning a mock address.
const myKeyPair = getMockCryptoKeyPair();
const myAddress = address('Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy');
jest.mocked(getAddressFromPublicKey).mockResolvedValueOnce(myAddress);

// When we create a Signer from that CryptoKeyPair.
const mySigner = await createSignerFromKeyPair(myKeyPair);
mySigner satisfies KeyPairSigner;

// Then the created signer kept track of the address and key pair.
expect(jest.mocked(getAddressFromPublicKey)).toHaveBeenCalledTimes(1);
expect(mySigner.address).toBe(myAddress);
expect(mySigner.keyPair).toBe(myKeyPair);

// And provided functions to sign messages and transactions.
expect(typeof mySigner.signMessages).toBe('function');
expect(typeof mySigner.signTransactions).toBe('function');
});

it('freezes the created signer', async () => {
expect.assertions(1);
const mySigner = await createSignerFromKeyPair(getMockCryptoKeyPair());
expect(mySigner).toBeFrozenObject();
});

it('signs messages using the signBytes function', async () => {
expect.assertions(7);

// Given a KeyPairSigner created from a mock CryptoKeyPair.
const myKeyPair = getMockCryptoKeyPair();
const myAddress = address('Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy');
jest.mocked(getAddressFromPublicKey).mockResolvedValueOnce(myAddress);
const mySigner = await createSignerFromKeyPair(myKeyPair);

// And given we mock the next two signatures of the signBytes function.
const mockSignatures = [new Uint8Array([101, 101, 101]), new Uint8Array([201, 201, 201])] as SignatureBytes[];
jest.mocked(signBytes).mockResolvedValueOnce(mockSignatures[0]);
jest.mocked(signBytes).mockResolvedValueOnce(mockSignatures[1]);

// When we sign two messages using that signer.
const messages = [
createSignableMessage(new Uint8Array([1, 1, 1])),
createSignableMessage(new Uint8Array([2, 2, 2])),
];
const signatureDictionaries = await mySigner.signMessages(messages);

// Then the signature directories contain the expected signatures.
expect(signatureDictionaries[0]).toStrictEqual({ [myAddress]: mockSignatures[0] });
expect(signatureDictionaries[1]).toStrictEqual({ [myAddress]: mockSignatures[1] });

// And the signature directories are frozen.
expect(signatureDictionaries[0]).toBeFrozenObject();
expect(signatureDictionaries[1]).toBeFrozenObject();

// And signBytes was called twice with the expected parameters.
expect(jest.mocked(signBytes)).toHaveBeenCalledTimes(2);
expect(jest.mocked(signBytes)).toHaveBeenNthCalledWith(1, myKeyPair.privateKey, messages[0].content);
expect(jest.mocked(signBytes)).toHaveBeenNthCalledWith(2, myKeyPair.privateKey, messages[1].content);
});

it('signs transactions using the signTransactions function', async () => {
expect.assertions(7);

// Given a KeyPairSigner created from a mock CryptoKeyPair.
const myKeyPair = getMockCryptoKeyPair();
const myAddress = address('Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy');
jest.mocked(getAddressFromPublicKey).mockResolvedValueOnce(myAddress);
const mySigner = await createSignerFromKeyPair(myKeyPair);

// And given we have a couple of mock transactions to sign.
const mockTransactions = [{} as CompilableTransaction, {} as CompilableTransaction];

// And given we mock the next two calls of the signTransactions function.
const mockSignatures = [new Uint8Array([101, 101, 101]), new Uint8Array([201, 201, 201])] as SignatureBytes[];
jest.mocked(signTransaction).mockResolvedValueOnce({
...mockTransactions[0],
signatures: { [myAddress]: mockSignatures[0] },
});
jest.mocked(signTransaction).mockResolvedValueOnce({
...mockTransactions[1],
signatures: { [myAddress]: mockSignatures[1] },
});

// When we sign both transactions using that signer.
const signatureDictionaries = await mySigner.signTransactions(mockTransactions);

// Then the signature directories contain the expected signatures.
expect(signatureDictionaries[0]).toStrictEqual({ [myAddress]: mockSignatures[0] });
expect(signatureDictionaries[1]).toStrictEqual({ [myAddress]: mockSignatures[1] });

// And the signature directories are frozen.
expect(signatureDictionaries[0]).toBeFrozenObject();
expect(signatureDictionaries[1]).toBeFrozenObject();

// And signTransactions was called twice with the expected parameters.
expect(jest.mocked(signTransaction)).toHaveBeenCalledTimes(2);
expect(jest.mocked(signTransaction)).toHaveBeenNthCalledWith(1, [myKeyPair], mockTransactions[0]);
expect(jest.mocked(signTransaction)).toHaveBeenNthCalledWith(2, [myKeyPair], mockTransactions[1]);
});
});

describe('generateKeyPairSigner', () => {
it('generates a new KeyPairSigner using the generateKeyPair function', async () => {
expect.assertions(3);

// Given we mock the return value of generateKeyPair.
const mockKeypair = getMockCryptoKeyPair();
jest.mocked(generateKeyPair).mockResolvedValueOnce(mockKeypair);

// And we mock the return value of getAddressFromPublicKey.
const mockAddress = address('Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy');
jest.mocked(getAddressFromPublicKey).mockResolvedValueOnce(mockAddress);

// When we generate a new KeyPairSigner from scratch.
const mySigner = await generateKeyPairSigner();
mySigner satisfies KeyPairSigner;

// Then the signer was created using the generated key pair and the mock address.
expect(mySigner.keyPair).toBe(mockKeypair);
expect(mySigner.address).toBe(mockAddress);

// And generateKeyPair was called once.
expect(jest.mocked(generateKeyPair)).toHaveBeenCalledTimes(1);
});

it('freezes the generated signer', async () => {
expect.assertions(1);

// Given we mock the return value of generateKeyPair.
const mockKeypair = getMockCryptoKeyPair();
jest.mocked(generateKeyPair).mockResolvedValueOnce(mockKeypair);

// Then the generated signer is frozen.
const mySigner = await generateKeyPairSigner();
expect(mySigner).toBeFrozenObject();
});
});
38 changes: 38 additions & 0 deletions packages/signers/src/__tests__/message-modifying-signer-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { address } from '@solana/addresses';

import {
assertIsMessageModifyingSigner,
isMessageModifyingSigner,
MessageModifyingSigner,
} from '../message-modifying-signer';

describe('isMessageModifyingSigner', () => {
it('checks whether a given value is a MessageModifyingSigner', () => {
const myAddress = address('Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy');
const mySigner = {
address: myAddress,
modifyAndSignMessages: async () => [],
} satisfies MessageModifyingSigner<'Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy'>;

expect(isMessageModifyingSigner(mySigner)).toBe(true);
expect(isMessageModifyingSigner({ address: myAddress })).toBe(false);
expect(isMessageModifyingSigner({ address: myAddress, modifyAndSignMessages: 42 })).toBe(false);
});
});

describe('assertIsMessageModifyingSigner', () => {
it('asserts that a given value is a MessageModifyingSigner', () => {
const myAddress = address('Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy');
const mySigner = {
address: myAddress,
modifyAndSignMessages: async () => [],
} satisfies MessageModifyingSigner<'Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy'>;

const expectedMessage = 'The provided value does not implement the MessageModifyingSigner interface';
expect(() => assertIsMessageModifyingSigner(mySigner)).not.toThrow();
expect(() => assertIsMessageModifyingSigner({ address: myAddress })).toThrow(expectedMessage);
expect(() => assertIsMessageModifyingSigner({ address: myAddress, modifyAndSignMessages: 42 })).toThrow(
expectedMessage
);
});
});
Loading

0 comments on commit 7c29a1e

Please sign in to comment.