Skip to content

Commit 65b3f79

Browse files
committed
Add ExecutingSigner class
1 parent 25a2f94 commit 65b3f79

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

.changeset/all-ways-lie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@mysten/sui': minor
3+
---
4+
5+
Add ExecutingSigner class

packages/typescript/src/cryptography/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export {
2828
type ParsedKeypair,
2929
type SignatureWithBytes,
3030
Signer,
31+
ExecutingSigner,
3132
Keypair,
3233
decodeSuiPrivateKey,
3334
encodeSuiPrivateKey,

packages/typescript/src/cryptography/keypair.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export abstract class Signer {
8484
transaction,
8585
client,
8686
}: SignAndExecuteOptions): Promise<Experimental_SuiClientTypes.TransactionResponse> {
87+
transaction.setSenderIfNotSet(this.toSuiAddress());
8788
const bytes = await transaction.build({ client });
8889
const { signature } = await this.signTransaction(bytes);
8990
const response = await client.core.executeTransaction({
@@ -109,6 +110,42 @@ export abstract class Signer {
109110
abstract getPublicKey(): PublicKey;
110111
}
111112

113+
export class ExecutingSigner extends Signer {
114+
#client: ClientWithCoreApi;
115+
#signer: Signer;
116+
117+
constructor({ signer, client }: { signer: Signer; client: ClientWithCoreApi }) {
118+
super();
119+
120+
this.#client = client;
121+
this.#signer = signer;
122+
}
123+
124+
sign(bytes: Uint8Array) {
125+
return this.#signer.sign(bytes);
126+
}
127+
128+
getKeyScheme() {
129+
return this.#signer.getKeyScheme();
130+
}
131+
132+
getPublicKey() {
133+
return this.#signer.getPublicKey();
134+
}
135+
136+
async signAndExecuteTransaction({
137+
transaction,
138+
}: Omit<
139+
SignAndExecuteOptions,
140+
'client'
141+
>): Promise<Experimental_SuiClientTypes.TransactionResponse> {
142+
return super.signAndExecuteTransaction({
143+
transaction,
144+
client: this.#client,
145+
});
146+
}
147+
}
148+
112149
export abstract class Keypair extends Signer {
113150
/**
114151
* This returns the Bech32 secret key string for this keypair.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) Mysten Labs, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { beforeEach, describe, expect, it } from 'vitest';
5+
6+
import { Transaction } from '../../src/transactions';
7+
import { normalizeSuiObjectId } from '../../src/utils';
8+
import { setup, TestToolbox } from './utils/setup';
9+
import { ExecutingSigner } from '../../src/cryptography';
10+
11+
export const SUI_CLOCK_OBJECT_ID = normalizeSuiObjectId('0x6');
12+
13+
describe('ExecutingSigner', () => {
14+
let toolbox: TestToolbox;
15+
16+
beforeEach(async () => {
17+
toolbox = await setup();
18+
});
19+
20+
it('executes without a client', async () => {
21+
const tx = new Transaction();
22+
23+
tx.transferObjects(tx.splitCoins(tx.gas, [1]), toolbox.address());
24+
25+
const signer = new ExecutingSigner({
26+
signer: toolbox.keypair,
27+
client: toolbox.client,
28+
});
29+
30+
const result = await signer.signAndExecuteTransaction({
31+
transaction: tx,
32+
});
33+
34+
expect(result.effects.status.success).toBe(true);
35+
36+
expect(
37+
await toolbox.keypair
38+
.getPublicKey()
39+
.verifyTransaction(await tx.build(), result.signatures[0]),
40+
).toBe(true);
41+
});
42+
});

0 commit comments

Comments
 (0)