diff --git a/src/helpers/keyUtils.ts b/src/helpers/keyUtils.ts index 6080f79..6f40635 100644 --- a/src/helpers/keyUtils.ts +++ b/src/helpers/keyUtils.ts @@ -118,14 +118,15 @@ export const generateEd25519KeyData = async (ed25519Seed: BN): Promise { - 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(`${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 { - return getOrSetNonce(legacyMetadataHost, ecCurve, serverTimeOffset, X, Y, privKey, true); -} - export function generateNonceMetadataParams( serverTimeOffset: number, operation: string, @@ -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 { + // 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(`${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(`${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(`${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 { + 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"); diff --git a/src/interfaces.ts b/src/interfaces.ts index 0767444..32c1470 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -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 { diff --git a/test/sapphire_devnet_ed25519.test.ts b/test/sapphire_devnet_ed25519.test.ts index 72832d2..ab451c5 100644 --- a/test/sapphire_devnet_ed25519.test.ts +++ b/test/sapphire_devnet_ed25519.test.ts @@ -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