Skip to content

Commit

Permalink
refactor: Fix build breaking refactor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Oct 11, 2024
1 parent 350e1fa commit e188e15
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 62 deletions.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"typecheck:watch": "npm run typecheck -- --watch"
},
"dependencies": {
"@noble/secp256k1": "1.7.1",
"@stacks/common": "^6.16.0",
"@stacks/encryption": "^6.16.1",
"@stacks/network": "^6.16.0",
Expand Down
10 changes: 3 additions & 7 deletions packages/auth/src/userSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ import { InstanceDataStore, LocalStorageStore, SessionDataStore } from './sessio
import { decodeToken } from 'jsontokens';
import { verifyAuthResponse } from './verification';
import * as authMessages from './messages';
import {
decryptContent,
encryptContent,
EncryptContentOptions,
isValidPrivateKey,
} from '@stacks/encryption';
import { utils } from '@noble/secp256k1';
import { decryptContent, encryptContent, EncryptContentOptions } from '@stacks/encryption';
import { getAddressFromDID } from './dids';
import {
createFetchFn,
Expand Down Expand Up @@ -254,7 +250,7 @@ export class UserSession {
)) as string;
} catch (e) {
Logger.warn('Failed decryption of appPrivateKey, will try to use as given');
if (!isValidPrivateKey(tokenPayload.private_key as string)) {
if (!utils.isValidPrivateKey(tokenPayload.private_key as string)) {
throw new LoginFailedError(
'Failed decrypting appPrivateKey. Usually means' +
' that the transit key has changed during login.'
Expand Down
7 changes: 2 additions & 5 deletions packages/cli/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ const c32check = require('c32check');
import { HDKey } from '@scure/bip32';
import * as scureBip39 from '@scure/bip39';

import {
compressPrivateKey,
getPublicKeyFromPrivate,
publicKeyToBtcAddress,
} from '@stacks/encryption';
import { getPublicKeyFromPrivate, publicKeyToBtcAddress } from '@stacks/encryption';
import { DerivationType, deriveAccount, generateWallet, getRootNode } from '@stacks/wallet-sdk';
import * as bip32 from 'bip32';
import * as bip39 from 'bip39';
Expand All @@ -20,6 +16,7 @@ import * as wif from 'wif';

import { getMaxIDSearchIndex, getPrivateKeyAddress } from './common';
import { CLINetworkAdapter } from './network';
import { compressPrivateKey } from '@stacks/transactions';

const BITCOIN_PUBKEYHASH = 0;
const BITCOIN_PUBKEYHASH_TESTNET = 111;
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ function nthBit(value: bigint, n: bigint) {
return value & (BigInt(1) << n);
}

/** @internal */
/** @ignore */
export function bytesToTwosBigInt(bytes: Uint8Array): bigint {
return fromTwos(BigInt(`0x${bytesToHex(bytes)}`), BigInt(bytes.byteLength * 8));
}
Expand Down
20 changes: 0 additions & 20 deletions packages/encryption/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { hmac } from '@noble/hashes/hmac';
import { sha256 } from '@noble/hashes/sha256';
import { getPublicKey as nobleGetPublicKey, signSync, utils } from '@noble/secp256k1';
import {
PRIVATE_KEY_BYTES_COMPRESSED,
PrivateKey,
bytesToHex,
concatBytes,
Expand All @@ -13,7 +12,6 @@ import {
import base58 from 'bs58';
import { hashRipemd160 } from './hashRipemd160';
import { hashSha256Sync } from './sha2Hash';
import { privateKeyToHex } from '../../transactions/src';

const BITCOIN_PUBKEYHASH = 0x00;

Expand Down Expand Up @@ -112,21 +110,3 @@ export function ecSign(messageHash: Uint8Array, privateKey: PrivateKey) {
der: false,
});
}

/**
* @ignore
*/
export function isValidPrivateKey(privateKey: PrivateKey): boolean {
return utils.isValidPrivateKey(privateKeyToBytes(privateKey));
}

/**
* @ignore
*/
export function compressPrivateKey(privateKey: PrivateKey): string {
privateKey = privateKeyToHex(privateKey);

return privateKey.length == PRIVATE_KEY_BYTES_COMPRESSED * 2
? privateKey // leave compressed
: `${privateKey}01`; // compress
}
20 changes: 1 addition & 19 deletions packages/encryption/tests/keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import {
hexToBytes,
utf8ToBytes,
} from '@stacks/common';
import { address, ECPair, networks } from 'bitcoinjs-lib';
import { ECPair, address, networks } from 'bitcoinjs-lib';
import bs58check from 'bs58check';
import { SECP256K1Client } from 'jsontokens';
import {
base58Encode,
compressPrivateKey,
ecSign,
getPublicKeyFromPrivate,
hashSha256Sync,
Expand Down Expand Up @@ -109,20 +108,3 @@ test('ecSign', () => {

expect(bytesToHex(signature)).toEqual(signatureHex);
});

describe(compressPrivateKey, () => {
it('does not change already compressed key', () => {
const privateKeyCompressed =
'00cdce6b5f87d38f2a830cae0da82162e1b487f07c5affa8130f01fe1a2a25fb01';

expect(compressPrivateKey(privateKeyCompressed)).toEqual(privateKeyCompressed);
});

it('compresses uncompressed key', () => {
const privateKey = '00cdce6b5f87d38f2a830cae0da82162e1b487f07c5affa8130f01fe1a2a25fb';
const privateKeyCompressed =
'00cdce6b5f87d38f2a830cae0da82162e1b487f07c5affa8130f01fe1a2a25fb01';

expect(compressPrivateKey(privateKey)).toEqual(privateKeyCompressed);
});
});
1 change: 1 addition & 0 deletions packages/internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"devDependencies": {
"@stacks/api": "^6.9.0",
"@stacks/stacking": "^6.9.0",
"@stacks/blockchain-api-client": "^7.12.0",
"@stacks/network": "^6.16.0",
"jest-fetch-mock": "^3.0.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/internal/src/apiMockingHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Configuration, TransactionsApi } from '@stacks/blockchain-api-client';
import { STACKS_TESTNET } from '@stacks/network';
import { MockResponseInitFunction } from 'jest-fetch-mock';
import { StackingClient } from '../../stacking/src';
import { StackingClient } from '@stacks/stacking';
import { StacksNodeApi } from '@stacks/api';

// NOTES
Expand Down
8 changes: 8 additions & 0 deletions packages/internal/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@
"tsBuildInfoFile": "./tsconfig.build.tsbuildinfo",
"composite": true
},
"references": [
{
"path": "../api/tsconfig.build.json"
},
{
"path": "../stacking/tsconfig.build.json"
}
],
"include": ["src/**/*"]
}
3 changes: 0 additions & 3 deletions packages/stacking/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
{
"path": "../encryption/tsconfig.build.json"
},
{
"path": "../internal/tsconfig.build.json"
},
{
"path": "../network/tsconfig.build.json"
},
Expand Down
6 changes: 4 additions & 2 deletions packages/storage/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import {
NotEnoughProofError,
PayloadTooLargeError,
PreconditionFailedError,
PRIVATE_KEY_BYTES_COMPRESSED,
utf8ToBytes,
ValidationError,
} from '@stacks/common';
import {
compressPrivateKey,
ecSign,
getPublicKeyFromPrivate,
hashSha256Sync,
Expand Down Expand Up @@ -151,7 +151,9 @@ function makeLegacyAuthToken(challengeText: string, signerKeyHex: string): strin
}
if (parsedChallenge[0] === 'gaiahub' && parsedChallenge[3] === 'blockstack_storage_please_sign') {
const digest = hashSha256Sync(utf8ToBytes(challengeText));
const signatureBytes = ecSign(digest, compressPrivateKey(signerKeyHex));
const compressedPrivateKey =
signerKeyHex.length === PRIVATE_KEY_BYTES_COMPRESSED * 2 ? signerKeyHex : `${signerKeyHex}01`;
const signatureBytes = ecSign(digest, compressedPrivateKey);
// We only want the DER encoding so use toDERHex provided by @noble/secp256k1
const signature = Signature.fromCompact(bytesToHex(signatureBytes)).toDERHex();

Expand Down
11 changes: 11 additions & 0 deletions packages/transactions/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,17 @@ export function signMessageHashRsv({
return signatureVrsToRsv(signWithKey(privateKey, messageHash));
}

/**
* @ignore
*/
export function compressPrivateKey(privateKey: PrivateKey): string {
privateKey = privateKeyToHex(privateKey);

return privateKey.length == PRIVATE_KEY_BYTES_COMPRESSED * 2
? privateKey // leave compressed
: `${privateKey}01`; // compress
}

/**
* Convert a private key to a single-sig address.
* @returns A Stacks address string (encoded with c32check)
Expand Down
4 changes: 2 additions & 2 deletions packages/transactions/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import {
Hex,
IntegerType,
PrivateKey,
PublicKey,
bytesToHex,
concatArray,
hexToBytes,
intToBigInt,
writeUInt32BE,
} from '@stacks/common/src';
} from '@stacks/common';
import {
ChainId,
DEFAULT_CHAIN_ID,
Expand Down Expand Up @@ -59,7 +60,6 @@ import {
deserializePayloadBytes,
serializeLPListBytes,
} from './wire';
import { PublicKey } from '@stacks/common';

export class StacksTransaction {
version: TransactionVersion;
Expand Down
18 changes: 18 additions & 0 deletions packages/transactions/tests/keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ec as EC } from 'elliptic';
import {
PubKeyEncoding,
StacksWireType,
compressPrivateKey,
compressPublicKey,
createStacksPublicKey,
encodeStructuredData,
Expand Down Expand Up @@ -343,3 +344,20 @@ describe(privateKeyToAddress.name, () => {
expect(addressTestnet).toBe('ST10J81WVGVB3M4PHQN4Q4G0R8586TBJH94CGRESQ');
});
});

describe(compressPrivateKey, () => {
it('does not change already compressed key', () => {
const privateKeyCompressed =
'00cdce6b5f87d38f2a830cae0da82162e1b487f07c5affa8130f01fe1a2a25fb01';

expect(compressPrivateKey(privateKeyCompressed)).toEqual(privateKeyCompressed);
});

it('compresses uncompressed key', () => {
const privateKey = '00cdce6b5f87d38f2a830cae0da82162e1b487f07c5affa8130f01fe1a2a25fb';
const privateKeyCompressed =
'00cdce6b5f87d38f2a830cae0da82162e1b487f07c5affa8130f01fe1a2a25fb01';

expect(compressPrivateKey(privateKey)).toEqual(privateKeyCompressed);
});
});
4 changes: 2 additions & 2 deletions packages/wallet-sdk/src/derive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import { HDKey } from '@scure/bip32';
import { getNameInfo } from '@stacks/auth';
import { ApiParam, bytesToHex, defaultApiLike, utf8ToBytes } from '@stacks/common';
import { compressPrivateKey, createSha2Hash } from '@stacks/encryption';
import { createSha2Hash } from '@stacks/encryption';
import {
STACKS_MAINNET,
StacksNetwork,
StacksNetworkName,
deriveDefaultUrl,
networkFrom,
} from '@stacks/network';
import { getAddressFromPrivateKey } from '@stacks/transactions';
import { compressPrivateKey, getAddressFromPrivateKey } from '@stacks/transactions';
import { Account, HARDENED_OFFSET, WalletKeys } from './models/common';
import { fetchFirstName } from './usernames';
import { assertIsTruthy } from './utils';
Expand Down

0 comments on commit e188e15

Please sign in to comment.