Skip to content

Commit

Permalink
[privatekey] Fix AIP-80 strictness warnings (#588)
Browse files Browse the repository at this point in the history
* Fix AIP-80 strictness warnings

* Update changelog

---------

Co-authored-by: Greg Nazario <greg@gnazar.io>
  • Loading branch information
GhostWalker562 and gregnazario authored Nov 26, 2024
1 parent 7d9785b commit 3d1fe1b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to the Aptos TypeScript SDK will be captured in this file. T
# Unreleased

- Add `gasProfile` function to `Move` class to allow for gas profiling of Aptos Move functions
- `PrivateKey.formatPrivateKey` now supports formatting AIP-80 strings
- Removed strictness warnings for bytes AIP-80 private key parsing formatting.

# 1.33.0 (2024-11-13)

Expand Down
17 changes: 9 additions & 8 deletions src/core/crypto/privateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ export class PrivateKey {
*/
public static formatPrivateKey(privateKey: HexInput, type: PrivateKeyVariants): string {
const aip80Prefix = PrivateKey.AIP80_PREFIXES[type];
return `${aip80Prefix}${Hex.fromHexInput(privateKey).toString()}`;

// Remove the prefix if it exists
let formattedPrivateKey = privateKey;
if (typeof formattedPrivateKey === "string" && formattedPrivateKey.startsWith(aip80Prefix)) {
// eslint-disable-next-line prefer-destructuring
formattedPrivateKey = formattedPrivateKey.split("-")[2];
}

return `${aip80Prefix}${Hex.fromHexInput(formattedPrivateKey).toString()}`;
}

/**
Expand Down Expand Up @@ -91,13 +99,6 @@ export class PrivateKey {
} else {
// The value is an Uint8Array
data = Hex.fromHexInput(value);
// If the strictness is false, the user has opted into non-AIP-80 compliant private keys.
if (strict !== false) {
// eslint-disable-next-line no-console
console.warn(
"[Aptos SDK] It is recommended that private keys are parsed as AIP-80 compliant strings instead of Uint8Array (https://github.com/aptos-foundation/AIPs/blob/main/aips/aip-80.md). You can fix the private key by formatting it with `PrivateKey.formatPrivateKey(privateKey: Uint8Array, type: 'ed25519' | 'secp256k1'): string`.",
);
}
}

return data;
Expand Down

0 comments on commit 3d1fe1b

Please sign in to comment.