Skip to content

Commit

Permalink
starknet-tee
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroRosalba committed Jan 8, 2025
1 parent ea9d1c0 commit 7b161ec
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 94 deletions.
1 change: 1 addition & 0 deletions packages/plugin-tee/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"bs58": "6.0.0",
"node-cache": "5.1.2",
"pumpdotfun-sdk": "1.3.2",
"starknet": "6.18.0",
"tsup": "8.3.5"
},
"scripts": {
Expand Down
47 changes: 47 additions & 0 deletions packages/plugin-tee/src/providers/deriveKeyProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { privateKeyToAccount } from "viem/accounts";
import { PrivateKeyAccount, keccak256 } from "viem";
import { RemoteAttestationProvider } from "./remoteAttestationProvider";
import { TEEMode, RemoteAttestationQuote } from "../types/tee";
import * as starknet from "starknet";

interface DeriveKeyAttestationData {
agentId: string;
Expand Down Expand Up @@ -156,6 +157,50 @@ class DeriveKeyProvider {
throw error;
}
}

async deriveSnKeypair(
path: string,
subject: string,
agentId: string
) : Promise<{
keypair: {privateKey: string, publicKey: string};
attestation: RemoteAttestationQuote;
}> {
try {
if (!path || !subject) {
console.error(
"Path and Subject are required for key derivation"
);
}
console.log("Deriving Starknet Key in TEE...");
const derivedKey = await this.client.deriveKey(path, subject);
const uint8Parsed = derivedKey.asUint8Array();
const stringParsed = uint8Parsed.toString();

const privateKey = starknet.starknetKeccak(stringParsed).toString();
const publicKey = starknet.ec.starkCurve.getStarkKey(privateKey);

const argentXaccountClassHash = '0x1a736d6ed154502257f02b1ccdf4d9d1089f80811cd6acad48e6b6a9d1f2003';
const AXConstructorCallData = starknet.CallData.compile({
owner: publicKey,
guardian: '0',
});
const AXcontractAddress = starknet.hash.calculateContractAddressFromHash(
publicKey,
argentXaccountClassHash,
AXConstructorCallData,
0
);
const attestation = await this.generateDeriveKeyAttestation(
agentId,
AXcontractAddress
);
return { keypair: {privateKey: privateKey, publicKey: publicKey} , attestation: attestation}
} catch (error) {
console.error("Error deriving Starknet key:", error);
throw error;
}
}
}

const deriveKeyProvider: Provider = {
Expand Down Expand Up @@ -185,6 +230,8 @@ const deriveKeyProvider: Provider = {
secretSalt,
agentId
);
const snKeypair = await provider

return JSON.stringify({
solana: solanaKeypair.keypair.publicKey,
evm: evmKeypair.keypair.address,
Expand Down
Loading

0 comments on commit 7b161ec

Please sign in to comment.