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: export normalizePrivateKey #10

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@
return u.ensureBytes('', typeof hex === 'string' ? hex0xToBytes(hex) : hex);
}

function normPrivKey(privKey: Hex): string {
export function normalizePrivateKey(privKey: Hex): string {
return u.bytesToHex(ensureBytes(privKey)).padStart(64, '0');
}
export function getPublicKey(privKey: Hex, isCompressed = false): Uint8Array {
return curve.getPublicKey(normPrivKey(privKey), isCompressed);
return curve.getPublicKey(normalizePrivateKey(privKey), isCompressed);
}
export function getSharedSecret(privKeyA: Hex, pubKeyB: Hex): Uint8Array {
return curve.getSharedSecret(normPrivKey(privKeyA), pubKeyB);
return curve.getSharedSecret(normalizePrivateKey(privKeyA), pubKeyB);
}

function checkSignature(signature: SignatureType) {
Expand All @@ -93,7 +93,7 @@
}

export function sign(msgHash: Hex, privKey: Hex, opts?: any): SignatureType {
const sig = curve.sign(checkMessage(msgHash), normPrivKey(privKey), opts);
const sig = curve.sign(checkMessage(msgHash), normalizePrivateKey(privKey), opts);
checkSignature(sig);
return sig;
}
Expand Down Expand Up @@ -374,7 +374,7 @@
for (let j = 0; j < rate; j++) {
const item = padded[i + j];
if (typeof item === 'undefined') throw new Error('invalid index');
state[j] += item;

Check failure on line 377 in index.ts

View workflow job for this annotation

GitHub Actions / v18 @ ubuntu-latest

Object is possibly 'undefined'.

Check failure on line 377 in index.ts

View workflow job for this annotation

GitHub Actions / v20 @ ubuntu-latest

Object is possibly 'undefined'.
}
state = fn(state);
}
Expand Down
13 changes: 13 additions & 0 deletions test/stark.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,19 @@ describe('starknet', () => {
);
});

should('Private stark key normalization', () => {
const ethSignature =
'0x21fbf0696d5e0aa2ef41a2b4ffb623bcaf070461d61cf7251c74161f82fec3a43' +
'70854bc0a34b3ab487c1bc021cd318c734c51ae29374f2beb0e6f2dd49b4bf41c';
const privateKey = starknet.ethSigToPrivate(ethSignature);
const normalizedPrivateKey = starknet.normalizePrivateKey(privateKey);
deepStrictEqual(privateKey, '766f11e90cd7c7b43085b56da35c781f8c067ac0d578eabdceebc4886435bda');
deepStrictEqual(
normalizedPrivateKey,
'0766f11e90cd7c7b43085b56da35c781f8c067ac0d578eabdceebc4886435bda'
);
});

should('Key derivation', () => {
const layer = 'starkex';
const application = 'starkdeployement';
Expand Down
Loading