@aztec/accounts 0.0.0-test.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -0
- package/artifacts/EcdsaKAccount.d.json.ts +3 -0
- package/artifacts/EcdsaKAccount.json +6257 -0
- package/artifacts/EcdsaRAccount.d.json.ts +3 -0
- package/artifacts/EcdsaRAccount.json +6257 -0
- package/artifacts/SchnorrAccount.d.json.ts +3 -0
- package/artifacts/SchnorrAccount.json +6354 -0
- package/artifacts/SchnorrSingleKeyAccount.d.json.ts +3 -0
- package/artifacts/SchnorrSingleKeyAccount.json +3937 -0
- package/dest/dapp/dapp_interface.d.ts +10 -0
- package/dest/dapp/dapp_interface.d.ts.map +1 -0
- package/dest/dapp/dapp_interface.js +16 -0
- package/dest/dapp/index.d.ts +2 -0
- package/dest/dapp/index.d.ts.map +1 -0
- package/dest/dapp/index.js +1 -0
- package/dest/defaults/account_contract.d.ts +15 -0
- package/dest/defaults/account_contract.d.ts.map +1 -0
- package/dest/defaults/account_contract.js +10 -0
- package/dest/defaults/account_interface.d.ts +27 -0
- package/dest/defaults/account_interface.d.ts.map +1 -0
- package/dest/defaults/account_interface.js +37 -0
- package/dest/defaults/index.d.ts +10 -0
- package/dest/defaults/index.d.ts.map +1 -0
- package/dest/defaults/index.js +8 -0
- package/dest/ecdsa/ecdsa_k/account_contract.d.ts +18 -0
- package/dest/ecdsa/ecdsa_k/account_contract.d.ts.map +1 -0
- package/dest/ecdsa/ecdsa_k/account_contract.js +38 -0
- package/dest/ecdsa/ecdsa_k/index.d.ts +43 -0
- package/dest/ecdsa/ecdsa_k/index.d.ts.map +1 -0
- package/dest/ecdsa/ecdsa_k/index.js +44 -0
- package/dest/ecdsa/ecdsa_k/lazy.d.ts +47 -0
- package/dest/ecdsa/ecdsa_k/lazy.d.ts.map +1 -0
- package/dest/ecdsa/ecdsa_k/lazy.js +52 -0
- package/dest/ecdsa/index.d.ts +3 -0
- package/dest/ecdsa/index.d.ts.map +1 -0
- package/dest/ecdsa/index.js +2 -0
- package/dest/ecdsa/lazy.d.ts +3 -0
- package/dest/ecdsa/lazy.d.ts.map +1 -0
- package/dest/ecdsa/lazy.js +2 -0
- package/dest/ecdsa/ssh_ecdsa_r/account_contract.d.ts +21 -0
- package/dest/ecdsa/ssh_ecdsa_r/account_contract.d.ts.map +1 -0
- package/dest/ecdsa/ssh_ecdsa_r/account_contract.js +80 -0
- package/dest/ecdsa/ssh_ecdsa_r/index.d.ts +46 -0
- package/dest/ecdsa/ssh_ecdsa_r/index.d.ts.map +1 -0
- package/dest/ecdsa/ssh_ecdsa_r/index.js +47 -0
- package/dest/ecdsa/ssh_ecdsa_r/lazy.d.ts +49 -0
- package/dest/ecdsa/ssh_ecdsa_r/lazy.d.ts.map +1 -0
- package/dest/ecdsa/ssh_ecdsa_r/lazy.js +57 -0
- package/dest/schnorr/account_contract.d.ts +24 -0
- package/dest/schnorr/account_contract.d.ts.map +1 -0
- package/dest/schnorr/account_contract.js +37 -0
- package/dest/schnorr/index.d.ts +57 -0
- package/dest/schnorr/index.d.ts.map +1 -0
- package/dest/schnorr/index.js +66 -0
- package/dest/schnorr/lazy.d.ts +61 -0
- package/dest/schnorr/lazy.d.ts.map +1 -0
- package/dest/schnorr/lazy.js +74 -0
- package/dest/single_key/account_contract.d.ts +17 -0
- package/dest/single_key/account_contract.d.ts.map +1 -0
- package/dest/single_key/account_contract.js +42 -0
- package/dest/single_key/index.d.ts +39 -0
- package/dest/single_key/index.d.ts.map +1 -0
- package/dest/single_key/index.js +45 -0
- package/dest/single_key/lazy.d.ts +43 -0
- package/dest/single_key/lazy.d.ts.map +1 -0
- package/dest/single_key/lazy.js +53 -0
- package/dest/testing/configuration.d.ts +28 -0
- package/dest/testing/configuration.d.ts.map +1 -0
- package/dest/testing/configuration.js +15 -0
- package/dest/testing/create_account.d.ts +43 -0
- package/dest/testing/create_account.d.ts.map +1 -0
- package/dest/testing/create_account.js +58 -0
- package/dest/testing/index.d.ts +35 -0
- package/dest/testing/index.d.ts.map +1 -0
- package/dest/testing/index.js +47 -0
- package/dest/testing/lazy.d.ts +34 -0
- package/dest/testing/lazy.d.ts.map +1 -0
- package/dest/testing/lazy.js +46 -0
- package/dest/utils/index.d.ts +2 -0
- package/dest/utils/index.d.ts.map +1 -0
- package/dest/utils/index.js +1 -0
- package/dest/utils/ssh_agent.d.ts +36 -0
- package/dest/utils/ssh_agent.d.ts.map +1 -0
- package/dest/utils/ssh_agent.js +153 -0
- package/package.json +106 -0
- package/src/dapp/dapp_interface.ts +38 -0
- package/src/dapp/index.ts +1 -0
- package/src/defaults/account_contract.ts +21 -0
- package/src/defaults/account_interface.ts +59 -0
- package/src/defaults/index.ts +10 -0
- package/src/ecdsa/ecdsa_k/account_contract.ts +39 -0
- package/src/ecdsa/ecdsa_k/index.ts +63 -0
- package/src/ecdsa/ecdsa_k/lazy.ts +71 -0
- package/src/ecdsa/index.ts +2 -0
- package/src/ecdsa/lazy.ts +2 -0
- package/src/ecdsa/ssh_ecdsa_r/account_contract.ts +89 -0
- package/src/ecdsa/ssh_ecdsa_r/index.ts +67 -0
- package/src/ecdsa/ssh_ecdsa_r/lazy.ts +78 -0
- package/src/schnorr/account_contract.ts +39 -0
- package/src/schnorr/index.ts +97 -0
- package/src/schnorr/lazy.ts +107 -0
- package/src/single_key/account_contract.ts +43 -0
- package/src/single_key/index.ts +63 -0
- package/src/single_key/lazy.ts +71 -0
- package/src/testing/configuration.ts +39 -0
- package/src/testing/create_account.ts +93 -0
- package/src/testing/index.ts +92 -0
- package/src/testing/lazy.ts +86 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/ssh_agent.ts +136 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { type AccountManager, FeeJuicePaymentMethod, type PXE, type WaitOpts } from '@aztec/aztec.js';
|
|
2
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
3
|
+
import { deriveSigningKey } from '@aztec/stdlib/keys';
|
|
4
|
+
|
|
5
|
+
import { getSchnorrAccount, getSchnorrAccountContractAddress } from '../schnorr/index.js';
|
|
6
|
+
import type { InitialAccountData } from './configuration.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Generate a fixed amount of random schnorr account contract instance.
|
|
10
|
+
*/
|
|
11
|
+
export async function generateSchnorrAccounts(numberOfAccounts: number) {
|
|
12
|
+
const secrets = Array.from({ length: numberOfAccounts }, () => Fr.random());
|
|
13
|
+
return await Promise.all(
|
|
14
|
+
secrets.map(async secret => {
|
|
15
|
+
const salt = Fr.random();
|
|
16
|
+
return {
|
|
17
|
+
secret,
|
|
18
|
+
signingKey: deriveSigningKey(secret),
|
|
19
|
+
salt,
|
|
20
|
+
address: await getSchnorrAccountContractAddress(secret, salt),
|
|
21
|
+
};
|
|
22
|
+
}),
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Data for deploying funded account.
|
|
28
|
+
*/
|
|
29
|
+
type DeployAccountData = Pick<InitialAccountData, 'secret' | 'salt'> & {
|
|
30
|
+
/**
|
|
31
|
+
* An optional signingKey if it's not derived from the secret.
|
|
32
|
+
*/
|
|
33
|
+
signingKey?: InitialAccountData['signingKey'];
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Deploy schnorr account contract.
|
|
38
|
+
* It will pay for the fee for the deployment itself. So it must be funded with the prefilled public data.
|
|
39
|
+
*/
|
|
40
|
+
export async function deployFundedSchnorrAccount(
|
|
41
|
+
pxe: PXE,
|
|
42
|
+
account: DeployAccountData,
|
|
43
|
+
opts: WaitOpts & {
|
|
44
|
+
/**
|
|
45
|
+
* Whether or not to skip registering contract class.
|
|
46
|
+
*/
|
|
47
|
+
skipClassRegistration?: boolean;
|
|
48
|
+
} = { interval: 0.1, skipClassRegistration: false },
|
|
49
|
+
): Promise<AccountManager> {
|
|
50
|
+
const signingKey = account.signingKey ?? deriveSigningKey(account.secret);
|
|
51
|
+
const accountManager = await getSchnorrAccount(pxe, account.secret, signingKey, account.salt);
|
|
52
|
+
|
|
53
|
+
// Pay the fee by the account itself.
|
|
54
|
+
// This only works when the world state is prefilled with the balance for the account in test environment.
|
|
55
|
+
const paymentMethod = new FeeJuicePaymentMethod(accountManager.getAddress());
|
|
56
|
+
|
|
57
|
+
await accountManager
|
|
58
|
+
.deploy({
|
|
59
|
+
skipClassRegistration: opts.skipClassRegistration,
|
|
60
|
+
skipPublicDeployment: true,
|
|
61
|
+
fee: { paymentMethod },
|
|
62
|
+
})
|
|
63
|
+
.wait(opts);
|
|
64
|
+
|
|
65
|
+
return accountManager;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Deploy schnorr account contracts.
|
|
70
|
+
* They will pay for the fees for the deployment themselves. So they must be funded with the prefilled public data.
|
|
71
|
+
*/
|
|
72
|
+
export async function deployFundedSchnorrAccounts(
|
|
73
|
+
pxe: PXE,
|
|
74
|
+
accounts: DeployAccountData[],
|
|
75
|
+
opts: WaitOpts & {
|
|
76
|
+
/**
|
|
77
|
+
* Whether or not to skip registering contract class.
|
|
78
|
+
*/
|
|
79
|
+
skipClassRegistration?: boolean;
|
|
80
|
+
} = { interval: 0.1, skipClassRegistration: false },
|
|
81
|
+
): Promise<AccountManager[]> {
|
|
82
|
+
const accountManagers: AccountManager[] = [];
|
|
83
|
+
// Serial due to https://github.com/AztecProtocol/aztec-packages/issues/12045
|
|
84
|
+
for (let i = 0; i < accounts.length; i++) {
|
|
85
|
+
accountManagers.push(
|
|
86
|
+
await deployFundedSchnorrAccount(pxe, accounts[i], {
|
|
87
|
+
...opts,
|
|
88
|
+
skipClassRegistration: i !== 0 || opts.skipClassRegistration, // Register the contract class at most once.
|
|
89
|
+
}),
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
return accountManagers;
|
|
93
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `@aztec/accounts/testing` export provides utility methods for testing, in particular in a Sandbox environment.
|
|
3
|
+
*
|
|
4
|
+
* Use {@link getInitialTestAccountsWallets} to obtain a list of wallets for the Sandbox pre-seeded accounts.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
import type { PXE } from '@aztec/aztec.js';
|
|
9
|
+
import type { AccountWalletWithSecretKey } from '@aztec/aztec.js/wallet';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
getSchnorrAccount,
|
|
13
|
+
getSchnorrAccountContractAddress,
|
|
14
|
+
getSchnorrWalletWithSecretKey,
|
|
15
|
+
} from '../schnorr/index.js';
|
|
16
|
+
import type { InitialAccountData } from './configuration.js';
|
|
17
|
+
import {
|
|
18
|
+
INITIAL_TEST_ACCOUNT_SALTS,
|
|
19
|
+
INITIAL_TEST_ENCRYPTION_KEYS,
|
|
20
|
+
INITIAL_TEST_SECRET_KEYS,
|
|
21
|
+
INITIAL_TEST_SIGNING_KEYS,
|
|
22
|
+
} from './configuration.js';
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
type InitialAccountData,
|
|
26
|
+
INITIAL_TEST_ACCOUNT_SALTS,
|
|
27
|
+
INITIAL_TEST_ENCRYPTION_KEYS,
|
|
28
|
+
INITIAL_TEST_SECRET_KEYS,
|
|
29
|
+
INITIAL_TEST_SIGNING_KEYS,
|
|
30
|
+
} from './configuration.js';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Gets the basic information for initial test accounts.
|
|
34
|
+
*/
|
|
35
|
+
export function getInitialTestAccounts(): Promise<InitialAccountData[]> {
|
|
36
|
+
return Promise.all(
|
|
37
|
+
INITIAL_TEST_SECRET_KEYS.map(async (secret, i) => ({
|
|
38
|
+
secret,
|
|
39
|
+
signingKey: INITIAL_TEST_ENCRYPTION_KEYS[i],
|
|
40
|
+
salt: INITIAL_TEST_ACCOUNT_SALTS[i],
|
|
41
|
+
address: await getSchnorrAccountContractAddress(
|
|
42
|
+
secret,
|
|
43
|
+
INITIAL_TEST_ACCOUNT_SALTS[i],
|
|
44
|
+
INITIAL_TEST_SIGNING_KEYS[i],
|
|
45
|
+
),
|
|
46
|
+
})),
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Gets a collection of wallets for the Aztec accounts that are initially stored in the test environment.
|
|
52
|
+
* @param pxe - PXE instance.
|
|
53
|
+
* @returns A set of AccountWallet implementations for each of the initial accounts.
|
|
54
|
+
*/
|
|
55
|
+
export function getInitialTestAccountsWallets(pxe: PXE): Promise<AccountWalletWithSecretKey[]> {
|
|
56
|
+
return Promise.all(
|
|
57
|
+
INITIAL_TEST_SECRET_KEYS.map(async (encryptionKey, i) => {
|
|
58
|
+
const account = await getSchnorrAccount(
|
|
59
|
+
pxe,
|
|
60
|
+
encryptionKey!,
|
|
61
|
+
INITIAL_TEST_SIGNING_KEYS[i]!,
|
|
62
|
+
INITIAL_TEST_ACCOUNT_SALTS[i],
|
|
63
|
+
);
|
|
64
|
+
return account.getWallet();
|
|
65
|
+
}),
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Queries a PXE for it's registered accounts.
|
|
71
|
+
* @param pxe - PXE instance.
|
|
72
|
+
* @returns A set of key data for each of the initial accounts.
|
|
73
|
+
*/
|
|
74
|
+
export async function getDeployedTestAccounts(pxe: PXE): Promise<InitialAccountData[]> {
|
|
75
|
+
const registeredAccounts = await pxe.getRegisteredAccounts();
|
|
76
|
+
const testAccounts = await getInitialTestAccounts();
|
|
77
|
+
return testAccounts.filter(t => registeredAccounts.some(r => r.address.equals(t.address)));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Queries a PXE for it's registered accounts and returns wallets for those accounts using keys in the initial test accounts.
|
|
82
|
+
* @param pxe - PXE instance.
|
|
83
|
+
* @returns A set of AccountWallet implementations for each of the initial accounts.
|
|
84
|
+
*/
|
|
85
|
+
export async function getDeployedTestAccountsWallets(pxe: PXE): Promise<AccountWalletWithSecretKey[]> {
|
|
86
|
+
const testAccounts = await getDeployedTestAccounts(pxe);
|
|
87
|
+
return Promise.all(
|
|
88
|
+
testAccounts.map(({ secret, signingKey, salt }) => getSchnorrWalletWithSecretKey(pxe, secret, signingKey, salt)),
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export { deployFundedSchnorrAccount, deployFundedSchnorrAccounts, generateSchnorrAccounts } from './create_account.js';
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `@aztec/accounts/testing/lazy` export provides utility methods for testing, in particular in a Sandbox environment.
|
|
3
|
+
*
|
|
4
|
+
* Use {@link getInitialTestAccountsWallets} to obtain a list of wallets for the Sandbox pre-seeded accounts.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
import type { PXE } from '@aztec/aztec.js';
|
|
9
|
+
import type { AccountWalletWithSecretKey } from '@aztec/aztec.js/wallet';
|
|
10
|
+
|
|
11
|
+
import { getSchnorrAccount, getSchnorrAccountContractAddress, getSchnorrWalletWithSecretKey } from '../schnorr/lazy.js';
|
|
12
|
+
import {
|
|
13
|
+
INITIAL_TEST_ACCOUNT_SALTS,
|
|
14
|
+
INITIAL_TEST_ENCRYPTION_KEYS,
|
|
15
|
+
INITIAL_TEST_SECRET_KEYS,
|
|
16
|
+
INITIAL_TEST_SIGNING_KEYS,
|
|
17
|
+
type InitialAccountData,
|
|
18
|
+
} from './configuration.js';
|
|
19
|
+
|
|
20
|
+
export {
|
|
21
|
+
type InitialAccountData,
|
|
22
|
+
INITIAL_TEST_ACCOUNT_SALTS,
|
|
23
|
+
INITIAL_TEST_ENCRYPTION_KEYS,
|
|
24
|
+
INITIAL_TEST_SECRET_KEYS,
|
|
25
|
+
INITIAL_TEST_SIGNING_KEYS,
|
|
26
|
+
} from './configuration.js';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Gets the basic information for initial test accounts.
|
|
30
|
+
*/
|
|
31
|
+
export function getInitialTestAccounts(): Promise<InitialAccountData[]> {
|
|
32
|
+
return Promise.all(
|
|
33
|
+
INITIAL_TEST_SECRET_KEYS.map(async (secret, i) => ({
|
|
34
|
+
secret,
|
|
35
|
+
signingKey: INITIAL_TEST_ENCRYPTION_KEYS[i],
|
|
36
|
+
salt: INITIAL_TEST_ACCOUNT_SALTS[i],
|
|
37
|
+
address: await getSchnorrAccountContractAddress(
|
|
38
|
+
secret,
|
|
39
|
+
INITIAL_TEST_ACCOUNT_SALTS[i],
|
|
40
|
+
INITIAL_TEST_SIGNING_KEYS[i],
|
|
41
|
+
),
|
|
42
|
+
})),
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Gets a collection of wallets for the Aztec accounts that are initially stored in the test environment.
|
|
48
|
+
* @param pxe - PXE instance.
|
|
49
|
+
* @returns A set of AccountWallet implementations for each of the initial accounts.
|
|
50
|
+
*/
|
|
51
|
+
export function getInitialTestAccountsWallets(pxe: PXE): Promise<AccountWalletWithSecretKey[]> {
|
|
52
|
+
return Promise.all(
|
|
53
|
+
INITIAL_TEST_SECRET_KEYS.map(async (encryptionKey, i) => {
|
|
54
|
+
const account = await getSchnorrAccount(
|
|
55
|
+
pxe,
|
|
56
|
+
encryptionKey!,
|
|
57
|
+
INITIAL_TEST_SIGNING_KEYS[i]!,
|
|
58
|
+
INITIAL_TEST_ACCOUNT_SALTS[i],
|
|
59
|
+
);
|
|
60
|
+
return account.getWallet();
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Queries a PXE for it's registered accounts.
|
|
67
|
+
* @param pxe - PXE instance.
|
|
68
|
+
* @returns A set of key data for each of the initial accounts.
|
|
69
|
+
*/
|
|
70
|
+
export async function getDeployedTestAccounts(pxe: PXE): Promise<InitialAccountData[]> {
|
|
71
|
+
const registeredAccounts = await pxe.getRegisteredAccounts();
|
|
72
|
+
const testAccounts = await getInitialTestAccounts();
|
|
73
|
+
return testAccounts.filter(t => registeredAccounts.some(r => r.address.equals(t.address)));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Queries a PXE for it's registered accounts and returns wallets for those accounts using keys in the initial test accounts.
|
|
78
|
+
* @param pxe - PXE instance.
|
|
79
|
+
* @returns A set of AccountWallet implementations for each of the initial accounts.
|
|
80
|
+
*/
|
|
81
|
+
export async function getDeployedTestAccountsWallets(pxe: PXE): Promise<AccountWalletWithSecretKey[]> {
|
|
82
|
+
const testAccounts = await getDeployedTestAccounts(pxe);
|
|
83
|
+
return Promise.all(
|
|
84
|
+
testAccounts.map(({ secret, signingKey, salt }) => getSchnorrWalletWithSecretKey(pxe, secret, signingKey, salt)),
|
|
85
|
+
);
|
|
86
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ssh_agent.js';
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { Buffer } from 'buffer';
|
|
2
|
+
import net from 'net';
|
|
3
|
+
|
|
4
|
+
const SSH_AGENT_IDENTITIES_REQUEST = 11;
|
|
5
|
+
const SSH_AGENT_IDENTITIES_RESPONSE = 12;
|
|
6
|
+
const SSH_AGENT_SIGN_REQUEST = 13;
|
|
7
|
+
const SSH_AGENT_SIGN_RESPONSE = 14;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Connect to the SSH agent via a TCP socket using the standard env variable
|
|
11
|
+
*/
|
|
12
|
+
export function connectToAgent() {
|
|
13
|
+
const socketPath = process.env.SSH_AUTH_SOCK;
|
|
14
|
+
if (!socketPath) {
|
|
15
|
+
throw new Error('SSH_AUTH_SOCK is not set');
|
|
16
|
+
}
|
|
17
|
+
return net.connect(socketPath);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Type representing a stored key in the SSH agent.
|
|
22
|
+
*/
|
|
23
|
+
type StoredKey = {
|
|
24
|
+
/**
|
|
25
|
+
* Type of the key.
|
|
26
|
+
*/
|
|
27
|
+
type: string;
|
|
28
|
+
/**
|
|
29
|
+
* Public key in base64 encoding.
|
|
30
|
+
*/
|
|
31
|
+
publicKey: string;
|
|
32
|
+
/**
|
|
33
|
+
* Comment associated with the key.
|
|
34
|
+
*/
|
|
35
|
+
comment: string;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Retrieve the identities stored in the SSH agent.
|
|
40
|
+
*/
|
|
41
|
+
export function getIdentities(): Promise<StoredKey[]> {
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
const stream = connectToAgent();
|
|
44
|
+
stream.on('connect', () => {
|
|
45
|
+
const request = Buffer.concat([
|
|
46
|
+
Buffer.from([0, 0, 0, 5 + 4]), // length
|
|
47
|
+
Buffer.from([SSH_AGENT_IDENTITIES_REQUEST]),
|
|
48
|
+
Buffer.from([0, 0, 0, 0]), // flags
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
stream.write(request);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
stream.on('data', data => {
|
|
55
|
+
const responseType = data[4];
|
|
56
|
+
if (responseType === SSH_AGENT_IDENTITIES_RESPONSE) {
|
|
57
|
+
let offset = 5;
|
|
58
|
+
const numKeys = data.readUInt32BE(offset);
|
|
59
|
+
offset += 4;
|
|
60
|
+
|
|
61
|
+
const keys = [];
|
|
62
|
+
for (let i = 0; i < numKeys; i++) {
|
|
63
|
+
const keyLength = data.readUInt32BE(offset);
|
|
64
|
+
offset += 4;
|
|
65
|
+
const key = data.subarray(offset, offset + keyLength);
|
|
66
|
+
offset += keyLength;
|
|
67
|
+
const commentLength = data.readUInt32BE(offset);
|
|
68
|
+
offset += 4;
|
|
69
|
+
const comment = data.subarray(offset, offset + commentLength);
|
|
70
|
+
offset += commentLength;
|
|
71
|
+
|
|
72
|
+
let keyOffset = 0;
|
|
73
|
+
const typeLen = key.readUInt32BE(keyOffset);
|
|
74
|
+
keyOffset += 4;
|
|
75
|
+
const type = key.subarray(keyOffset, keyOffset + typeLen);
|
|
76
|
+
|
|
77
|
+
keys.push({
|
|
78
|
+
type: type.toString('ascii'),
|
|
79
|
+
publicKey: key.toString('base64'),
|
|
80
|
+
comment: comment.toString('utf8'),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
stream.end();
|
|
84
|
+
resolve(keys);
|
|
85
|
+
} else {
|
|
86
|
+
stream.end();
|
|
87
|
+
reject(`Unexpected response type: ${responseType}`);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Sign data using a key stored in the SSH agent. The private signing key is identified by its corresponding public key.
|
|
95
|
+
*/
|
|
96
|
+
export function signWithAgent(keyType: Buffer, curveName: Buffer, publicKey: Buffer, data: Buffer) {
|
|
97
|
+
return new Promise<Buffer>((resolve, reject) => {
|
|
98
|
+
const stream = connectToAgent();
|
|
99
|
+
stream.on('connect', () => {
|
|
100
|
+
// Construct the key blob
|
|
101
|
+
const keyBlob = Buffer.concat([
|
|
102
|
+
Buffer.from([0, 0, 0, keyType.length]),
|
|
103
|
+
keyType,
|
|
104
|
+
Buffer.from([0, 0, 0, curveName.length]),
|
|
105
|
+
curveName,
|
|
106
|
+
Buffer.from([0, 0, 0, publicKey.length + 1, 4]),
|
|
107
|
+
publicKey,
|
|
108
|
+
]);
|
|
109
|
+
const request = Buffer.concat([
|
|
110
|
+
Buffer.from([0, 0, 0, 5 + keyBlob.length + 4 + data.length + 4]), // length
|
|
111
|
+
Buffer.from([SSH_AGENT_SIGN_REQUEST]),
|
|
112
|
+
Buffer.from([0, 0, 0, keyBlob.length]), // key blob length
|
|
113
|
+
keyBlob,
|
|
114
|
+
Buffer.from([0, 0, 0, data.length]), // data length
|
|
115
|
+
data,
|
|
116
|
+
Buffer.from([0, 0, 0, 0]), // flags
|
|
117
|
+
]);
|
|
118
|
+
|
|
119
|
+
stream.write(request);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
stream.on('data', data => {
|
|
123
|
+
const type = data[4];
|
|
124
|
+
|
|
125
|
+
if (type === SSH_AGENT_SIGN_RESPONSE) {
|
|
126
|
+
const signatureLength = data.readUInt32BE(5);
|
|
127
|
+
const signature = data.subarray(9, 9 + signatureLength);
|
|
128
|
+
stream.end();
|
|
129
|
+
resolve(signature);
|
|
130
|
+
} else {
|
|
131
|
+
stream.end();
|
|
132
|
+
reject(`Unexpected response type: ${type}`);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
}
|