Skip to content

Commit

Permalink
fix metadata signing key for ed25519
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshu committed Apr 4, 2024
1 parent 802f9fc commit 04c151c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 40 deletions.
7 changes: 4 additions & 3 deletions src/helpers/keyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,15 @@ export const generateEd25519KeyData = async (ed25519Seed: BN): Promise<PrivateKe
const metadataPrivNonce = ed25519Curve.genKeyPair().getPrivate();
const oauthKey = finalEd25519Key.scalar.sub(metadataPrivNonce).umod(ed25519Curve.n);
const oauthKeyPair = ed25519Curve.keyFromPrivate(oauthKey.toArrayLike(Buffer));
const metadataSigningKey = getSecpKeyFromEd25519(oauthKeyPair.getPrivate());
return {
oAuthKeyScalar: oauthKeyPair.getPrivate(),
oAuthPubX: oauthKeyPair.getPublic().getX(),
oAuthPubY: oauthKeyPair.getPublic().getY(),
SigningPubX: encryptionKey.point.getX(),
SigningPubY: encryptionKey.point.getY(),
SigningPubX: metadataSigningKey.point.getX(),
SigningPubY: metadataSigningKey.point.getY(),
metadataNonce: metadataPrivNonce,
metadataSigningKey: encryptionKey.scalar,
metadataSigningKey: metadataSigningKey.scalar,
encryptedSeed: encDataBase64,
finalUserPubKeyPoint: finalEd25519Key.point,
};
Expand Down
98 changes: 64 additions & 34 deletions src/helpers/metadataUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,40 +57,6 @@ export async function getMetadata(
}
}

export async function getOrSetNonce(
legacyMetadataHost: string,
ecCurve: ec,
serverTimeOffset: number,
X: string,
Y: string,
privKey?: BN,
getOnly = false
): Promise<GetOrSetNonceResult> {
let data: Data;
const msg = getOnly ? "getNonce" : "getOrSetNonce";
if (privKey) {
data = generateMetadataParams(ecCurve, serverTimeOffset, msg, privKey);
} else {
data = {
pub_key_X: X,
pub_key_Y: Y,
set_data: { data: msg },
};
}
return post<GetOrSetNonceResult>(`${legacyMetadataHost}/get_or_set_nonce`, data, undefined, { useAPIKey: true });
}

export async function getNonce(
legacyMetadataHost: string,
ecCurve: ec,
serverTimeOffset: number,
X: string,
Y: string,
privKey?: BN
): Promise<GetOrSetNonceResult> {
return getOrSetNonce(legacyMetadataHost, ecCurve, serverTimeOffset, X, Y, privKey, true);
}

export function generateNonceMetadataParams(
serverTimeOffset: number,
operation: string,
Expand Down Expand Up @@ -126,6 +92,70 @@ export function generateNonceMetadataParams(
};
}

export async function getOrSetNonce(
metadataHost: string,
ecCurve: ec,
serverTimeOffset: number,
X: string,
Y: string,
privKey?: BN,
getOnly = false,
isLegacyMetadata = true,
nonce = new BN(0),
keyType: KeyType = "secp256k1",
seed = ""
): Promise<GetOrSetNonceResult> {
// for legacy metadata
if (isLegacyMetadata) {
let data: Data;
const msg = getOnly ? "getNonce" : "getOrSetNonce";
if (privKey) {
data = generateMetadataParams(ecCurve, serverTimeOffset, msg, privKey);
} else {
data = {
pub_key_X: X,
pub_key_Y: Y,
set_data: { data: msg },
};
}
return post<GetOrSetNonceResult>(`${metadataHost}/get_or_set_nonce`, data, undefined, { useAPIKey: true });
}

// for sapphire metadata
const operation = getOnly ? "getNonce" : "getOrSetNonce";
if (operation === "getOrSetNonce") {
if (!privKey) {
throw new Error("privKey is required while `getOrSetNonce` for non legacy metadata");
}
if (nonce.cmp(new BN(0)) === 0) {
throw new Error("nonce is required while `getOrSetNonce` for non legacy metadata");
}
if (keyType === "ed25519" && !seed) {
throw new Error("seed is required while `getOrSetNonce` for non legacy metadata for ed25519 key type");
}
const data = generateNonceMetadataParams(serverTimeOffset, operation, privKey, keyType, nonce, seed);

return post<GetOrSetNonceResult>(`${metadataHost}/get_or_set_nonce`, data, undefined, { useAPIKey: true });
}
const data = {
pub_key_X: X,
pub_key_Y: Y,
set_data: { operation: "getNonce" },
key_type: keyType,
};
return post<GetOrSetNonceResult>(`${metadataHost}/get_or_set_nonce`, data, undefined, { useAPIKey: true });
}
export async function getNonce(
legacyMetadataHost: string,
ecCurve: ec,
serverTimeOffset: number,
X: string,
Y: string,
privKey?: BN
): Promise<GetOrSetNonceResult> {
return getOrSetNonce(legacyMetadataHost, ecCurve, serverTimeOffset, X, Y, privKey, true);
}

export const decryptSeedData = async (seedBase64: string, finalUserKey: BN) => {
const decryptionKey = getSecpKeyFromEd25519(finalUserKey);
const seedUtf8 = Buffer.from(seedBase64, "base64").toString("utf-8");
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,9 @@ export interface PrivateKeyData {
SigningPubX: BN;
SigningPubY: BN;
metadataNonce: BN;
encryptedSeed?: string;
metadataSigningKey: BN;
finalUserPubKeyPoint: curve.base.BasePoint;
metadataSigningKey?: BN;
encryptedSeed?: string;
}

export interface EncryptedSeed {
Expand Down
2 changes: 1 addition & 1 deletion test/sapphire_devnet_ed25519.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe("torus utils ed25519 sapphire devnet", function () {
});

it("should be able to import a key for a new user", async function () {
const email = "Willa_Funk12@gmail.com";
const email = "Willa_Funk1289@gmail.com";
const token = generateIdToken(email, "ES256");
// const privKeyBuffer = new BN(generatePrivateKey(ec, Buffer));
// key exported from phantom wallet
Expand Down

0 comments on commit 04c151c

Please sign in to comment.