Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/get public address keytype #170

Merged
merged 7 commits into from
Feb 27, 2025

Conversation

ieow
Copy link
Contributor

@ieow ieow commented Jan 7, 2025

Motivation and Context

To support multicurve tss (mpc core-kit) -
Allow getPublicAddress to retrieve different keyType based on input params

Jira Link:
https://toruslabs.atlassian.net/browse/IRT-1680

Description

How has this been tested?

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project. (run lint)
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • My code requires a db migration.

@ieow ieow marked this pull request as ready for review January 7, 2025 05:31
* @returns \{EC\} The elliptic curve.
*
*/
export const getEcCurve = (keyType: KeyType): EC => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a similar function getKeyCurve that already exists in src/helpers/common.ts

Copy link
Contributor

@huggingbot huggingbot Feb 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getKeyCurve can also be optimised to be similar to getEcCurve, i.e. to use constant instances:

const secp256k1EC = new EC("secp256k1");
const ed25519EC = new EC("ed25519");

based on the elliptic's docs, it's safe to initialise the EC instance once and reuse it:

// Create and initialize EC context
// (better do it once and reuse it)
var ec = new EC('secp256k1');

Copy link
Contributor

@huggingbot huggingbot Feb 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Torus class constructor and tests should use getKeyCurve to retrieve the EC instance for consistency

// current implementation
this.ec = new EC(this.keyType);

// proposed implementation
this.ec = getEcCurve(this.keyType);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

src/torus.ts Outdated
enableOneKey: boolean
): Promise<TorusPublicKey> {
const localKeyType = keyType ?? this.keyType;
const localEc = getEcCurve(localKeyType);
Copy link
Contributor

@huggingbot huggingbot Feb 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before retrieving an EC instance, we should probably do this check that is similar in the constructor:

(keyType === KEY_TYPE.ED25519 && LEGACY_NETWORKS_ROUTE_MAP[network as TORUS_LEGACY_NETWORK_TYPE])

otherwise, we might pass the incorrect key type into formatLegacyPublicKeyData

would be nice to bake this check into a function that can be reused here and in the constructor

Copy link
Contributor

@huggingbot huggingbot Feb 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could make more sense to do this check in formatLegacyPublicKeyData, or simply apply the same check wherever getEcCurve is used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not going to check for the instance's keyType
this feature is to Allow getPublicAddress to retrieve different keyType based on input params regardless of the instances's keyType

Copy link
Contributor

@huggingbot huggingbot Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assume that this.keyType is secp256k1 and this.network is a legacy network when the Torus class is initialised.

In getPublicAddress, if keyType is passed in as ed25519, then it won't be compatible with the legacy network. We need to guard against this potential bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added check on prior GetPubKeyOrKeyAssign

redundance function
@@ -16,11 +16,14 @@ export const generatePrivateKey = (ecCurve: EC, buf: typeof Buffer): Buffer => {
return ecCurve.genKeyPair().getPrivate().toArrayLike(buf);
};

const secp256k1EC = new EC("secp256k1");
const ed25519EC = new EC("ed25519");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was intentionally not left as a global variable to reduce the build size

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would create EC object every time increase memory usage and latency?

src/torus.ts Outdated
): Promise<TorusPublicKey> {
log.info(torusNodePubs, { verifier, verifierId, extendedVerifierId });
return this.getNewPublicAddress(endpoints, { verifier, verifierId, extendedVerifierId }, this.enableOneKey);
return this.getNewPublicAddress(endpoints, { verifier, verifierId, extendedVerifierId, keyType }, this.enableOneKey);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we pass keyType here as const localKeyType = keyType ?? this.keyType for the sake of consistency with other public functions

@@ -240,6 +240,37 @@ describe("torus utils sapphire devnet", function () {
});
});

it("should should fetch public address with keyType", async function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use existing test case and user for testcase to validate nothing changes on passing keyType for existing test user

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[email protected] is existing testcase's email in the sapphire_devnet_ed25519.test.ts

Copy link
Member

@himanshuchawla009 himanshuchawla009 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@himanshuchawla009 himanshuchawla009 merged commit 169265f into master Feb 27, 2025
3 checks passed
@himanshuchawla009 himanshuchawla009 deleted the feat/getPublicAddress-keytype branch February 27, 2025 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants