Skip to content

Commit

Permalink
fix: remove circular dependencies from wallet-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsan-javaid authored and reedrosenbluth committed Nov 19, 2021
1 parent 792c2ae commit e9e626d
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 85 deletions.
4 changes: 2 additions & 2 deletions packages/wallet-sdk/src/derive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { ECPair } from 'bitcoinjs-lib';
import { createSha2Hash, ecPairToHexString } from '@stacks/encryption';

import { assertIsTruthy } from './utils';
import { Account } from './models/account';
import { WalletKeys } from './models/wallet';
import { Account } from './models/common';
import { WalletKeys } from './models/common';

const DATA_DERIVATION_PATH = `m/888'/0'`;
const WALLET_CONFIG_PATH = `m/44/5757'/0'/1`;
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet-sdk/src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { generateMnemonic, mnemonicToSeed } from 'bip39';
import { fromSeed } from 'bip32';
import randomBytes from 'randombytes';
import { Wallet, getRootNode } from './models/wallet';
import { Wallet, getRootNode } from './models/common';
import { encrypt } from './encryption';
import { deriveAccount, deriveWalletKeys } from './derive';

Expand Down
1 change: 1 addition & 0 deletions packages/wallet-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export * from './generate';
export * from './derive';
export * from './models/wallet';
export * from './models/account';
export * from './models/common';
export * from './models/wallet-config';
export * from './encryption';
26 changes: 1 addition & 25 deletions packages/wallet-sdk/src/models/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
getPublicKeyFromPrivate,
hashCode,
hashSha256Sync,
publicKeyToAddress,
} from '@stacks/encryption';
import { makeAuthResponse as _makeAuthResponse } from '@stacks/auth';
import { TransactionVersion, getAddressFromPrivateKey } from '@stacks/transactions';
Expand All @@ -12,30 +11,13 @@ import {
DEFAULT_PROFILE,
fetchAccountProfileUrl,
fetchProfileFromUrl,
Profile,
signAndUploadProfile,
} from './profile';
import { Account } from './common';
import { ECPair } from 'bitcoinjs-lib';
import { connectToGaiaHubWithConfig, getHubInfo, makeGaiaAssociationToken } from '../utils';
import { Buffer } from '@stacks/common';

export interface Account {
/** The private key used for STX payments */
stxPrivateKey: string;
/** The private key used in Stacks 1.0 to register BNS names */
dataPrivateKey: string;
/** The salt is the same as the wallet-level salt. Used for app-specific keys */
salt: string;
/** A single username registered via BNS for this account */
username?: string;
/** A profile object that is publicly associated with this account's username */
profile?: Profile;
/** The root of the keychain used to generate app-specific keys */
appsKey: string;
/** The index of this account in the user's wallet */
index: number;
}

export const getStxAddress = ({
account,
transactionVersion = TransactionVersion.Testnet,
Expand All @@ -62,12 +44,6 @@ export const getAccountDisplayName = (account: Account) => {
return `Account ${account.index + 1}`;
};

export const getGaiaAddress = (account: Account) => {
const publicKey = getPublicKeyFromPrivate(account.dataPrivateKey);
const address = publicKeyToAddress(publicKey);
return address;
};

export const getAppPrivateKey = ({
account,
appDomain,
Expand Down
76 changes: 76 additions & 0 deletions packages/wallet-sdk/src/models/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { getPublicKeyFromPrivate, publicKeyToAddress } from '@stacks/encryption';
import { fromBase58 } from 'bip32';

export interface Account {
/** The private key used for STX payments */
stxPrivateKey: string;
/** The private key used in Stacks 1.0 to register BNS names */
dataPrivateKey: string;
/** The salt is the same as the wallet-level salt. Used for app-specific keys */
salt: string;
/** A single username registered via BNS for this account */
username?: string;
/** A profile object that is publicly associated with this account's username */
profile?: Profile;
/** The root of the keychain used to generate app-specific keys */
appsKey: string;
/** The index of this account in the user's wallet */
index: number;
}

const PERSON_TYPE = 'Person';
const CONTEXT = 'http://schema.org';
const IMAGE_TYPE = 'ImageObject';

export interface ProfileImage {
'@type': typeof IMAGE_TYPE;
name: string;
contentUrl: string;
}

export interface Profile {
'@type': typeof PERSON_TYPE;
'@context': typeof CONTEXT;
apps?: {
[origin: string]: string;
};
appsMeta?: {
[origin: string]: {
publicKey: string;
storage: string;
};
};
name?: string;
image?: ProfileImage[];
[key: string]: any;
}

/**
* This object represents the keys that were derived from the root-level
* keychain of a wallet.
*/
export interface WalletKeys {
/** Used when generating app private keys, which encrypt app-specific data */
salt: string;
/** The private key associated with the root of a BIP39 keychain */
rootKey: string;
/** A private key used to encrypt configuration data */
configPrivateKey: string;
}

export interface Wallet extends WalletKeys {
/** The encrypted secret key */
encryptedSecretKey: string;
/** A list of accounts generated by this wallet */
accounts: Account[];
}

export const getGaiaAddress = (account: Account) => {
const publicKey = getPublicKeyFromPrivate(account.dataPrivateKey);
const address = publicKeyToAddress(publicKey);
return address;
};

export const getRootNode = (wallet: Wallet) => {
return fromBase58(wallet.rootKey);
};
2 changes: 1 addition & 1 deletion packages/wallet-sdk/src/models/legacy-wallet-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { decryptContent } from '@stacks/encryption';
import { GaiaHubConfig } from '@stacks/storage';
import { fetchPrivate } from '@stacks/transactions';
import { deriveLegacyConfigPrivateKey } from '../derive';
import { getRootNode, Wallet } from './wallet';
import { Wallet, getRootNode } from './common';

export interface LegacyConfigApp {
origin: string;
Expand Down
29 changes: 1 addition & 28 deletions packages/wallet-sdk/src/models/profile.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,9 @@
import { getProfileURLFromZoneFile } from '../utils';
import { Account, getGaiaAddress } from './account';
import { signProfileToken, wrapProfileToken } from '@stacks/profile';
import { connectToGaiaHub, GaiaHubConfig, uploadToGaiaHub } from '@stacks/storage';
import { getPublicKeyFromPrivate } from '@stacks/encryption';
import { fetchPrivate } from '@stacks/common';

const PERSON_TYPE = 'Person';
const CONTEXT = 'http://schema.org';
const IMAGE_TYPE = 'ImageObject';

export interface ProfileImage {
'@type': typeof IMAGE_TYPE;
name: string;
contentUrl: string;
}

export interface Profile {
'@type': typeof PERSON_TYPE;
'@context': typeof CONTEXT;
apps?: {
[origin: string]: string;
};
appsMeta?: {
[origin: string]: {
publicKey: string;
storage: string;
};
};
name?: string;
image?: ProfileImage[];
[key: string]: any;
}
import { Account, Profile, getGaiaAddress } from './common';

export const DEFAULT_PROFILE: Profile = {
'@type': 'Person',
Expand Down
3 changes: 1 addition & 2 deletions packages/wallet-sdk/src/models/wallet-config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Wallet } from './wallet';
import { Account } from './account';
import { Account, Wallet } from './common';
import { GaiaHubConfig, connectToGaiaHub, uploadToGaiaHub } from '@stacks/storage';
import { decryptContent, encryptContent, getPublicKeyFromPrivate } from '@stacks/encryption';
import { fetchPrivate } from '@stacks/common';
Expand Down
27 changes: 1 addition & 26 deletions packages/wallet-sdk/src/models/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,13 @@
import { fromBase58 } from 'bip32';
import { deriveAccount, deriveLegacyConfigPrivateKey } from '../derive';
import { connectToGaiaHubWithConfig, getHubInfo } from '../utils';
import { Account } from './account';
import { Wallet, getRootNode } from './common';
import { fetchLegacyWalletConfig } from './legacy-wallet-config';
import { fetchWalletConfig, updateWalletConfig, WalletConfig } from './wallet-config';

/**
* This object represents the keys that were derived from the root-level
* keychain of a wallet.
*/
export interface WalletKeys {
/** Used when generating app private keys, which encrypt app-specific data */
salt: string;
/** The private key associated with the root of a BIP39 keychain */
rootKey: string;
/** A private key used to encrypt configuration data */
configPrivateKey: string;
}

export interface Wallet extends WalletKeys {
/** The encrypted secret key */
encryptedSecretKey: string;
/** A list of accounts generated by this wallet */
accounts: Account[];
}

export interface LockedWallet {
encryptedSecretKey: string;
}

export const getRootNode = (wallet: Wallet) => {
return fromBase58(wallet.rootKey);
};

/**
* Restore wallet accounts by checking for encrypted WalletConfig files,
* stored in Gaia.
Expand Down

1 comment on commit e9e626d

@vercel
Copy link

@vercel vercel bot commented on e9e626d Nov 19, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.