@aztec/accounts 0.47.1 → 0.48.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.
Files changed (50) hide show
  1. package/artifacts/EcdsaKAccount.json +1 -0
  2. package/artifacts/EcdsaRAccount.d.json.ts +3 -0
  3. package/artifacts/EcdsaRAccount.json +1 -0
  4. package/artifacts/SchnorrAccount.json +1 -1
  5. package/artifacts/SchnorrSingleKeyAccount.json +1 -1
  6. package/dest/ecdsa/{account_contract.d.ts → ecdsa_k/account_contract.d.ts} +2 -2
  7. package/dest/ecdsa/ecdsa_k/account_contract.d.ts.map +1 -0
  8. package/dest/ecdsa/ecdsa_k/account_contract.js +33 -0
  9. package/dest/ecdsa/ecdsa_k/artifact.d.ts +2 -0
  10. package/dest/ecdsa/ecdsa_k/artifact.d.ts.map +1 -0
  11. package/dest/ecdsa/ecdsa_k/artifact.js +4 -0
  12. package/dest/ecdsa/ecdsa_k/index.d.ts +31 -0
  13. package/dest/ecdsa/ecdsa_k/index.d.ts.map +1 -0
  14. package/dest/ecdsa/ecdsa_k/index.js +32 -0
  15. package/dest/ecdsa/index.d.ts +2 -30
  16. package/dest/ecdsa/index.d.ts.map +1 -1
  17. package/dest/ecdsa/index.js +3 -32
  18. package/dest/ecdsa/ssh_ecdsa_r/account_contract.d.ts +18 -0
  19. package/dest/ecdsa/ssh_ecdsa_r/account_contract.d.ts.map +1 -0
  20. package/dest/ecdsa/ssh_ecdsa_r/account_contract.js +76 -0
  21. package/dest/ecdsa/ssh_ecdsa_r/artifact.d.ts +2 -0
  22. package/dest/ecdsa/ssh_ecdsa_r/artifact.d.ts.map +1 -0
  23. package/dest/ecdsa/ssh_ecdsa_r/artifact.js +4 -0
  24. package/dest/ecdsa/ssh_ecdsa_r/index.d.ts +31 -0
  25. package/dest/ecdsa/ssh_ecdsa_r/index.d.ts.map +1 -0
  26. package/dest/ecdsa/ssh_ecdsa_r/index.js +32 -0
  27. package/dest/utils/index.d.ts +2 -0
  28. package/dest/utils/index.d.ts.map +1 -0
  29. package/dest/utils/index.js +2 -0
  30. package/dest/utils/ssh_agent.d.ts +35 -0
  31. package/dest/utils/ssh_agent.d.ts.map +1 -0
  32. package/dest/utils/ssh_agent.js +109 -0
  33. package/package.json +10 -9
  34. package/src/ecdsa/{account_contract.ts → ecdsa_k/account_contract.ts} +6 -6
  35. package/src/ecdsa/ecdsa_k/artifact.ts +5 -0
  36. package/src/ecdsa/ecdsa_k/index.ts +37 -0
  37. package/src/ecdsa/index.ts +2 -37
  38. package/src/ecdsa/ssh_ecdsa_r/account_contract.ts +88 -0
  39. package/src/ecdsa/ssh_ecdsa_r/artifact.ts +5 -0
  40. package/src/ecdsa/ssh_ecdsa_r/index.ts +37 -0
  41. package/src/utils/index.ts +1 -0
  42. package/src/utils/ssh_agent.ts +136 -0
  43. package/artifacts/EcdsaAccount.json +0 -1
  44. package/dest/ecdsa/account_contract.d.ts.map +0 -1
  45. package/dest/ecdsa/account_contract.js +0 -33
  46. package/dest/ecdsa/artifact.d.ts +0 -2
  47. package/dest/ecdsa/artifact.d.ts.map +0 -1
  48. package/dest/ecdsa/artifact.js +0 -4
  49. package/src/ecdsa/artifact.ts +0 -5
  50. /package/artifacts/{EcdsaAccount.d.json.ts → EcdsaKAccount.d.json.ts} +0 -0
@@ -4,16 +4,16 @@ import { Ecdsa } from '@aztec/circuits.js/barretenberg';
4
4
  import { type ContractArtifact } from '@aztec/foundation/abi';
5
5
  import { type Fr } from '@aztec/foundation/fields';
6
6
 
7
- import { DefaultAccountContract } from '../defaults/account_contract.js';
8
- import { EcdsaAccountContractArtifact } from './artifact.js';
7
+ import { DefaultAccountContract } from '../../defaults/account_contract.js';
8
+ import { EcdsaKAccountContractArtifact } from './artifact.js';
9
9
 
10
10
  /**
11
11
  * Account contract that authenticates transactions using ECDSA signatures
12
12
  * verified against a secp256k1 public key stored in an immutable encrypted note.
13
13
  */
14
- export class EcdsaAccountContract extends DefaultAccountContract {
14
+ export class EcdsaKAccountContract extends DefaultAccountContract {
15
15
  constructor(private signingPrivateKey: Buffer) {
16
- super(EcdsaAccountContractArtifact as ContractArtifact);
16
+ super(EcdsaKAccountContractArtifact as ContractArtifact);
17
17
  }
18
18
 
19
19
  getDeploymentArgs() {
@@ -22,12 +22,12 @@ export class EcdsaAccountContract extends DefaultAccountContract {
22
22
  }
23
23
 
24
24
  getAuthWitnessProvider(_address: CompleteAddress): AuthWitnessProvider {
25
- return new EcdsaAuthWitnessProvider(this.signingPrivateKey);
25
+ return new EcdsaKAuthWitnessProvider(this.signingPrivateKey);
26
26
  }
27
27
  }
28
28
 
29
29
  /** Creates auth witnesses using ECDSA signatures. */
30
- class EcdsaAuthWitnessProvider implements AuthWitnessProvider {
30
+ class EcdsaKAuthWitnessProvider implements AuthWitnessProvider {
31
31
  constructor(private signingPrivateKey: Buffer) {}
32
32
 
33
33
  createAuthWit(messageHash: Fr): Promise<AuthWitness> {
@@ -0,0 +1,5 @@
1
+ import { type NoirCompiledContract, loadContractArtifact } from '@aztec/aztec.js';
2
+
3
+ import EcdsaKAccountContractJson from '../../../artifacts/EcdsaKAccount.json' assert { type: 'json' };
4
+
5
+ export const EcdsaKAccountContractArtifact = loadContractArtifact(EcdsaKAccountContractJson as NoirCompiledContract);
@@ -0,0 +1,37 @@
1
+ /**
2
+ * The `@aztec/accounts/ecdsa` export provides an ECDSA account contract implementation, that uses an ECDSA private key for authentication, and a Grumpkin key for encryption.
3
+ * Consider using this account type when working with integrations with Ethereum wallets.
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+ import { AccountManager, type Salt } from '@aztec/aztec.js/account';
8
+ import { type AccountWallet, getWallet } from '@aztec/aztec.js/wallet';
9
+ import { type PXE } from '@aztec/circuit-types';
10
+ import { type AztecAddress, type Fr } from '@aztec/circuits.js';
11
+
12
+ import { EcdsaKAccountContract } from './account_contract.js';
13
+
14
+ export { EcdsaKAccountContractArtifact } from './artifact.js';
15
+ export { EcdsaKAccountContract };
16
+
17
+ /**
18
+ * Creates an Account that relies on an ECDSA signing key for authentication.
19
+ * @param pxe - An PXE server instance.
20
+ * @param secretKey - Secret key used to derive all the keystore keys.
21
+ * @param signingPrivateKey - Secp256k1 key used for signing transactions.
22
+ * @param salt - Deployment salt.
23
+ */
24
+ export function getEcdsaKAccount(pxe: PXE, secretKey: Fr, signingPrivateKey: Buffer, salt?: Salt): AccountManager {
25
+ return new AccountManager(pxe, secretKey, new EcdsaKAccountContract(signingPrivateKey), salt);
26
+ }
27
+
28
+ /**
29
+ * Gets a wallet for an already registered account using ECDSA signatures.
30
+ * @param pxe - An PXE server instance.
31
+ * @param address - Address for the account.
32
+ * @param signingPrivateKey - ECDSA key used for signing transactions.
33
+ * @returns A wallet for this account that can be used to interact with a contract instance.
34
+ */
35
+ export function getEcdsaKWallet(pxe: PXE, address: AztecAddress, signingPrivateKey: Buffer): Promise<AccountWallet> {
36
+ return getWallet(pxe, address, new EcdsaKAccountContract(signingPrivateKey));
37
+ }
@@ -1,37 +1,2 @@
1
- /**
2
- * The `@aztec/accounts/ecdsa` export provides an ECDSA account contract implementation, that uses an ECDSA private key for authentication, and a Grumpkin key for encryption.
3
- * Consider using this account type when working with integrations with Ethereum wallets.
4
- *
5
- * @packageDocumentation
6
- */
7
- import { AccountManager, type Salt } from '@aztec/aztec.js/account';
8
- import { type AccountWallet, getWallet } from '@aztec/aztec.js/wallet';
9
- import { type PXE } from '@aztec/circuit-types';
10
- import { type AztecAddress, type Fr } from '@aztec/circuits.js';
11
-
12
- import { EcdsaAccountContract } from './account_contract.js';
13
-
14
- export { EcdsaAccountContractArtifact } from './artifact.js';
15
- export { EcdsaAccountContract };
16
-
17
- /**
18
- * Creates an Account that relies on an ECDSA signing key for authentication.
19
- * @param pxe - An PXE server instance.
20
- * @param secretKey - Secret key used to derive all the keystore keys.
21
- * @param signingPrivateKey - Secp256k1 key used for signing transactions.
22
- * @param salt - Deployment salt.
23
- */
24
- export function getEcdsaAccount(pxe: PXE, secretKey: Fr, signingPrivateKey: Buffer, salt?: Salt): AccountManager {
25
- return new AccountManager(pxe, secretKey, new EcdsaAccountContract(signingPrivateKey), salt);
26
- }
27
-
28
- /**
29
- * Gets a wallet for an already registered account using ECDSA signatures.
30
- * @param pxe - An PXE server instance.
31
- * @param address - Address for the account.
32
- * @param signingPrivateKey - ECDSA key used for signing transactions.
33
- * @returns A wallet for this account that can be used to interact with a contract instance.
34
- */
35
- export function getEcdsaWallet(pxe: PXE, address: AztecAddress, signingPrivateKey: Buffer): Promise<AccountWallet> {
36
- return getWallet(pxe, address, new EcdsaAccountContract(signingPrivateKey));
37
- }
1
+ export * from './ecdsa_k/index.js';
2
+ export * from './ssh_ecdsa_r/index.js';
@@ -0,0 +1,88 @@
1
+ import { type AuthWitnessProvider } from '@aztec/aztec.js/account';
2
+ import { AuthWitness, type CompleteAddress } from '@aztec/circuit-types';
3
+ import { EcdsaSignature } from '@aztec/circuits.js/barretenberg';
4
+ import { type ContractArtifact } from '@aztec/foundation/abi';
5
+ import { type Fr } from '@aztec/foundation/fields';
6
+
7
+ import { DefaultAccountContract } from '../../defaults/account_contract.js';
8
+ import { signWithAgent } from '../../utils/ssh_agent.js';
9
+ import { EcdsaRAccountContractArtifact } from './artifact.js';
10
+
11
+ const secp256r1N = 115792089210356248762697446949407573529996955224135760342422259061068512044369n;
12
+ /**
13
+ * Account contract that authenticates transactions using ECDSA signatures
14
+ * verified against a secp256r1 public key stored in an immutable encrypted note.
15
+ * Since this implementation relays signatures to an SSH agent, we provide the
16
+ * public key here not for signature verification, but to identify actual identity
17
+ * that will be used to sign authwitnesses.
18
+ */
19
+ export class EcdsaRSSHAccountContract extends DefaultAccountContract {
20
+ constructor(private signingPublicKey: Buffer) {
21
+ super(EcdsaRAccountContractArtifact as ContractArtifact);
22
+ }
23
+
24
+ getDeploymentArgs() {
25
+ return [this.signingPublicKey.subarray(0, 32), this.signingPublicKey.subarray(32, 64)];
26
+ }
27
+
28
+ getAuthWitnessProvider(_address: CompleteAddress): AuthWitnessProvider {
29
+ return new SSHEcdsaRAuthWitnessProvider(this.signingPublicKey);
30
+ }
31
+ }
32
+
33
+ /** Creates auth witnesses using ECDSA signatures. */
34
+ class SSHEcdsaRAuthWitnessProvider implements AuthWitnessProvider {
35
+ constructor(private signingPublicKey: Buffer) {}
36
+
37
+ #parseECDSASignature(data: Buffer) {
38
+ // Extract ECDSA signature components
39
+ let offset = 0;
40
+ const sigTypeLen = data.readUInt32BE(offset);
41
+ offset += 4;
42
+ const sigType = data.subarray(offset, offset + sigTypeLen).toString();
43
+ offset += sigTypeLen;
44
+
45
+ if (sigType !== 'ecdsa-sha2-nistp256') {
46
+ throw new Error(`Unexpected signature type: ${sigType}`);
47
+ }
48
+
49
+ offset += 4;
50
+ const rLen = data.readUInt32BE(offset);
51
+ offset += 4;
52
+ let r = data.subarray(offset, offset + rLen);
53
+ offset += rLen;
54
+
55
+ const sLen = data.readUInt32BE(offset);
56
+ offset += 4;
57
+ let s = data.subarray(offset, offset + sLen);
58
+
59
+ // R and S are encoded using ASN.1 DER format, which may include a leading zero byte to avoid interpreting the value as negative
60
+ if (r.length > 32) {
61
+ r = Buffer.from(Uint8Array.prototype.slice.call(r, 1));
62
+ }
63
+
64
+ if (s.length > 32) {
65
+ s = Buffer.from(Uint8Array.prototype.slice.call(s, 1));
66
+ }
67
+
68
+ const maybeHighS = BigInt(`0x${s.toString('hex')}`);
69
+
70
+ // ECDSA signatures must have a low S value so they can be used as a nullifier. BB forces a value of 27 for v, so
71
+ // only one PublicKey can verify the signature (and not its negated counterpart) https://ethereum.stackexchange.com/a/55728
72
+ if (maybeHighS > secp256r1N / 2n + 1n) {
73
+ s = Buffer.from((secp256r1N - maybeHighS).toString(16), 'hex');
74
+ }
75
+
76
+ return new EcdsaSignature(r, s, Buffer.from([0]));
77
+ }
78
+
79
+ async createAuthWit(messageHash: Fr): Promise<AuthWitness> {
80
+ // Key type and curve name
81
+ const keyType = Buffer.from('ecdsa-sha2-nistp256');
82
+ const curveName = Buffer.from('nistp256');
83
+ const data = await signWithAgent(keyType, curveName, this.signingPublicKey, messageHash.toBuffer());
84
+ const signature = this.#parseECDSASignature(data);
85
+
86
+ return new AuthWitness(messageHash, [...signature.r, ...signature.s]);
87
+ }
88
+ }
@@ -0,0 +1,5 @@
1
+ import { type NoirCompiledContract, loadContractArtifact } from '@aztec/aztec.js';
2
+
3
+ import EcdsaRAccountContractJson from '../../../artifacts/EcdsaRAccount.json' assert { type: 'json' };
4
+
5
+ export const EcdsaRAccountContractArtifact = loadContractArtifact(EcdsaRAccountContractJson as NoirCompiledContract);
@@ -0,0 +1,37 @@
1
+ /**
2
+ * The `@aztec/accounts/ecdsa` export provides an ECDSA account contract implementation, that uses an ECDSA private key for authentication, and a Grumpkin key for encryption.
3
+ * Consider using this account type when working with integrations with Ethereum wallets.
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+ import { AccountManager, type Salt } from '@aztec/aztec.js/account';
8
+ import { type AccountWallet, getWallet } from '@aztec/aztec.js/wallet';
9
+ import { type PXE } from '@aztec/circuit-types';
10
+ import { type AztecAddress, type Fr } from '@aztec/circuits.js';
11
+
12
+ import { EcdsaRSSHAccountContract } from './account_contract.js';
13
+
14
+ export { EcdsaRAccountContractArtifact } from './artifact.js';
15
+ export { EcdsaRSSHAccountContract };
16
+
17
+ /**
18
+ * Creates an Account that relies on an ECDSA signing key for authentication.
19
+ * @param pxe - An PXE server instance.
20
+ * @param secretKey - Secret key used to derive all the keystore keys.
21
+ * @param signingPublicKey - Secp2561 key used to identify its corresponding private key in the SSH Agent.
22
+ * @param salt - Deployment salt.
23
+ */
24
+ export function getEcdsaRSSHAccount(pxe: PXE, secretKey: Fr, signingPublicKey: Buffer, salt?: Salt): AccountManager {
25
+ return new AccountManager(pxe, secretKey, new EcdsaRSSHAccountContract(signingPublicKey), salt);
26
+ }
27
+
28
+ /**
29
+ * Gets a wallet for an already registered account using ECDSA signatures.
30
+ * @param pxe - An PXE server instance.
31
+ * @param address - Address for the account.
32
+ * @param signingPrivateKey - ECDSA key used for signing transactions.
33
+ * @returns A wallet for this account that can be used to interact with a contract instance.
34
+ */
35
+ export function getEcdsaRSSHWallet(pxe: PXE, address: AztecAddress, signingPublicKey: Buffer): Promise<AccountWallet> {
36
+ return getWallet(pxe, address, new EcdsaRSSHAccountContract(signingPublicKey));
37
+ }
@@ -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
+ }