Skip to content

Commit

Permalink
fix: use stacks package
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Aug 12, 2024
1 parent 85db0cc commit 937c0e2
Show file tree
Hide file tree
Showing 16 changed files with 1,847 additions and 1,239 deletions.
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,15 @@
"@coinbase/cbpay-js": "2.1.0",
"@fungible-systems/zone-file": "2.0.0",
"@hirosystems/token-metadata-api-client": "1.2.0",
"@leather.io/bitcoin": "0.10.0",
"@leather.io/bitcoin": "0.10.3",
"@leather.io/constants": "0.8.2",
"@leather.io/crypto": "1.1.0",
"@leather.io/crypto": "1.3.0",
"@leather.io/models": "0.11.0",
"@leather.io/query": "2.4.0",
"@leather.io/query": "2.4.2",
"@leather.io/stacks": "0.0.1",
"@leather.io/tokens": "0.7.0",
"@leather.io/ui": "1.10.0",
"@leather.io/utils": "0.11.1",
"@leather.io/ui": "1.10.1",
"@leather.io/utils": "0.12.0",
"@ledgerhq/hw-transport-webusb": "6.27.19",
"@noble/hashes": "1.4.0",
"@noble/secp256k1": "2.1.0",
Expand Down
2,972 changes: 1,814 additions & 1,158 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/app/common/validation/forms/recipient-validators.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as yup from 'yup';

import type { NetworkConfiguration } from '@leather.io/models';
import { stacksChainIdToCoreNetworkMode } from '@leather.io/stacks';

import { stacksChainIdToCoreNetworkMode } from '@shared/crypto/stacks/stacks.utils';
import { FormErrorMessages } from '@shared/error-messages';

import {
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/no-fees-warning-row.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChainID } from '@stacks/transactions';
import { HStack, styled } from 'leather-styles/jsx';

import { stacksChainIdToCoreNetworkMode } from '@shared/crypto/stacks/stacks.utils';
import { stacksChainIdToCoreNetworkMode } from '@leather.io/stacks';

import { capitalize } from '@app/common/utils';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { bytesToHex } from '@stacks/common';
import StacksApp from '@zondax/ledger-stacks';
import ecdsaFormat from 'ecdsa-sig-formatter';

import { getIdentityDerivationPath } from '@shared/crypto/stacks/stacks.utils';
import { getIdentityDerivationPath } from '@shared/crypto/stacks/stacks-address-gen';

function reformatDerSignatureToJose(derSignature: Uint8Array) {
// Stacks authentication uses `ES256k`, however `ecdsa-sig-formatter` doesn't
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { bytesToHex } from '@noble/hashes/utils';
import * as secp from '@noble/secp256k1';
import StacksApp from '@zondax/ledger-stacks';

import { makeStxDerivationPath } from '@leather.io/stacks';
import { delay } from '@leather.io/utils';

import {
getIdentityDerivationPath,
getStxDerivationPath,
} from '@shared/crypto/stacks/stacks.utils';
import { getIdentityDerivationPath } from '@shared/crypto/stacks/stacks-address-gen';

import { defaultNumberOfKeysToPullFromLedgerDevice } from '../../generic-flows/request-keys/use-request-ledger-keys';
import {
Expand Down Expand Up @@ -57,7 +55,7 @@ export function pullStacksKeysFromLedgerDevice(stacksApp: StacksApp) {
if (!dataPublicKeyResp.publicKey) return { status: 'failure', ...dataPublicKeyResp };

publicKeys.push({
path: getStxDerivationPath(index),
path: makeStxDerivationPath(index),
stxPublicKey: stxPublicKeyResp.publicKey.toString('hex'),
// We return a decompressed public key, to match the behaviour of
// @stacks/wallet-sdk. I'm not sure why we return an uncompressed key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
encodeStructuredData,
} from '@stacks/transactions';

import { stacksChainIdToCoreNetworkMode } from '@shared/crypto/stacks/stacks.utils';
import { stacksChainIdToCoreNetworkMode } from '@leather.io/stacks';

import { UnsignedMessageStructured } from '@shared/signature/signature-types';

import { capitalize } from '@app/common/utils';
Expand Down
8 changes: 4 additions & 4 deletions src/app/features/ledger/utils/stacks-ledger-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import StacksApp, { LedgerError, ResponseSign, ResponseVersion } from '@zondax/ledger-stacks';
import { compare } from 'compare-versions';

import { getStxDerivationPath, stxDerivationWithAccount } from '@shared/crypto/stacks/stacks.utils';
import { makeStxDerivationPath, stxDerivationWithAccount } from '@leather.io/stacks';

import {
LEDGER_APPS_MAP,
Expand All @@ -22,7 +22,7 @@ import {
export function requestPublicKeyForStxAccount(app: StacksApp) {
return async (index: number) =>
app.getAddressAndPubKey(
getStxDerivationPath(index),
makeStxDerivationPath(index),
// We pass mainnet as it expects something, however this is so it can return a formatted address
// We only need the public key, and can derive the address later in any network format
AddressVersion.MainnetSingleSig
Expand Down Expand Up @@ -66,12 +66,12 @@ export function signLedgerStacksTransaction(app: StacksApp) {

export function signLedgerStacksUtf8Message(app: StacksApp) {
return async (payload: string, accountIndex: number): Promise<ResponseSign> =>
app.sign_msg(getStxDerivationPath(accountIndex), payload);
app.sign_msg(makeStxDerivationPath(accountIndex), payload);
}

export function signLedgerStacksStructuredMessage(app: StacksApp) {
return async (domain: string, payload: string, accountIndex: number): Promise<ResponseSign> =>
app.sign_structured_msg(getStxDerivationPath(accountIndex), domain, payload);
app.sign_structured_msg(makeStxDerivationPath(accountIndex), domain, payload);
}

export function signStacksTransactionWithSignature(transaction: string, signatureVRS: Buffer) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/pages/home/components/account-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { ChainID } from '@stacks/transactions';
import { HomePageSelectors } from '@tests/selectors/home.selectors';
import { Flex } from 'leather-styles/jsx';

import { whenStacksChainId } from '@leather.io/stacks';
import { CreditCardIcon, IconButton, InboxIcon, SwapIcon } from '@leather.io/ui';

import { whenStacksChainId } from '@shared/crypto/stacks/stacks.utils';
import { RouteUrls } from '@shared/route-urls';

import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import {
import { deriveStxPrivateKey, generateWallet } from '@stacks/wallet-sdk';
import { atom } from 'jotai';

import { whenStacksChainId } from '@leather.io/stacks';
import { createNullArrayOfLength } from '@leather.io/utils';

import { DATA_DERIVATION_PATH, deriveStacksSalt } from '@shared/crypto/stacks/stacks-address-gen';
import { whenStacksChainId } from '@shared/crypto/stacks/stacks.utils';
import { defaultWalletKeyId } from '@shared/utils';

import { derivePublicKey } from '@app/common/keychain/keychain';
Expand Down
3 changes: 1 addition & 2 deletions src/app/store/networks/networks.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { StacksNetwork } from '@stacks/network';
import { ChainID, TransactionVersion } from '@stacks/transactions';

import { HIRO_API_BASE_URL_NAKAMOTO_TESTNET, type NetworkModes } from '@leather.io/models';

import { whenStacksChainId } from '@shared/crypto/stacks/stacks.utils';
import { whenStacksChainId } from '@leather.io/stacks';

import { useAppDispatch } from '@app/store';

Expand Down
2 changes: 1 addition & 1 deletion src/app/store/transactions/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChainID, TransactionVersion } from '@stacks/transactions';
import { atom } from 'jotai';

import { whenStacksChainId } from '@shared/crypto/stacks/stacks.utils';
import { whenStacksChainId } from '@leather.io/stacks';

import { currentNetworkAtom } from '@app/store/networks/networks';

Expand Down
8 changes: 8 additions & 0 deletions src/shared/crypto/stacks/stacks-address-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { sha256 } from '@noble/hashes/sha256';
import { bytesToHex, utf8ToBytes } from '@noble/hashes/utils';
import { HDKey } from '@scure/bip32';

import { makeAccountIndexDerivationPathFactory } from '@leather.io/stacks';

export const DATA_DERIVATION_PATH = `m/888'/0'`;

export function deriveStacksSalt(keychain: HDKey) {
Expand All @@ -10,3 +12,9 @@ export function deriveStacksSalt(keychain: HDKey) {
const publicKeyHex = utf8ToBytes(bytesToHex(keychain.publicKey!));
return bytesToHex(sha256(publicKeyHex));
}

const stxIdentityDerivationWithAccount = `m/888'/0'/{account}'`;

export const getIdentityDerivationPath = makeAccountIndexDerivationPathFactory(
stxIdentityDerivationWithAccount
);
24 changes: 0 additions & 24 deletions src/shared/crypto/stacks/stacks.utils.spec.ts

This file was deleted.

32 changes: 0 additions & 32 deletions src/shared/crypto/stacks/stacks.utils.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/shared/storage/redux-pesist.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PersistConfig, createMigrate, getStoredState } from 'redux-persist';
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';

import { getStxDerivationPath } from '@shared/crypto/stacks/stacks.utils';
import { makeStxDerivationPath } from '@leather.io/stacks';

import { defaultWalletKeyId } from '@shared/utils';

import type { RootState } from '@app/store';
Expand Down Expand Up @@ -103,7 +104,7 @@ async function migrateToUsingNoSerialization() {
function processStacksLedgerKeys(stacksKeys: any) {
const newStacksKeys = stacksKeys.entities.default.publicKeys.reduce(
(acc: { ids: string[]; entities: Record<string, any> }, item: any, index: number) => {
const path = getStxDerivationPath(index);
const path = makeStxDerivationPath(index);
const id = path.replace('m', defaultWalletKeyId);

acc.ids.push(id);
Expand Down

0 comments on commit 937c0e2

Please sign in to comment.