Skip to content

Commit

Permalink
fix: remove blockstack.js and legacy transaction lib dependency from …
Browse files Browse the repository at this point in the history
…keychain
  • Loading branch information
yknl committed Oct 30, 2020
1 parent c335bb0 commit c9c4f77
Show file tree
Hide file tree
Showing 23 changed files with 81 additions and 44 deletions.
2 changes: 1 addition & 1 deletion packages/auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { AppConfig } from './appConfig';
export { makeAuthRequest, makeAuthResponse } from './messages';
export { makeAuthRequest, makeAuthResponse, decryptPrivateKey } from './messages';
export { getAuthRequestFromURL, fetchAppManifest } from './provider';
export {
verifyAuthRequest,
Expand Down
2 changes: 2 additions & 0 deletions packages/encryption/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export * from './cryptoRandom';
export * from './sha2Hash';

export * from './encryption';

export * from './utils';
15 changes: 15 additions & 0 deletions packages/encryption/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,18 @@ export function getBase64OutputLength(inputByteLength: number) {
const encodedLength = Math.ceil(inputByteLength / 3) * 4;
return encodedLength;
}

/**
*
* @ignore
*/
export function hashCode(string: string) {
let hash = 0
if (string.length === 0) return hash
for (let i = 0; i < string.length; i++) {
const character = string.charCodeAt(i)
hash = (hash << 5) - hash + character
hash &= hash
}
return hash & 0x7fffffff
}
6 changes: 4 additions & 2 deletions packages/keychain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@
},
"dependencies": {
"@blockstack/rpc-client": "^0.3.0-alpha.11",
"@blockstack/stacks-transactions": "0.5.1",
"@stacks/transactions": "^1.0.0-beta.8",
"@stacks/encryption": "^1.0.0-beta.8",
"@stacks/storage": "^1.0.0-beta.8",
"@stacks/common": "^1.0.0-beta.8",
"bip39": "^3.0.2",
"bitcoinjs-lib": "^5.1.6",
"blockstack": "21.0.0",
"bn.js": "^5.1.1",
"c32check": "^1.0.1",
"jsontokens": "^3.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/keychain/src/address-derivation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {
ChainID,
getAddressFromPrivateKey,
TransactionVersion,
} from '@blockstack/stacks-transactions';
} from '@stacks/transactions';
import { BIP32Interface, ECPair } from 'bitcoinjs-lib';
import { ecPairToHexString } from 'blockstack';
import { ecPairToHexString } from '@stacks/encryption';

const networkDerivationPath = `m/44'/5757'/0'/0/0`;

Expand Down
2 changes: 1 addition & 1 deletion packages/keychain/src/encryption/decrypt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { decrypt as triplesecDecrypt } from 'triplesec';
import { decryptMnemonic } from 'blockstack/lib/encryption/wallet';
import { decryptMnemonic } from '@stacks/encryption';

/**
* Decrypt an encrypted mnemonic phrase with a password.
Expand Down
2 changes: 1 addition & 1 deletion packages/keychain/src/encryption/encrypt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { encryptMnemonic } from 'blockstack/lib/encryption/wallet';
import { encryptMnemonic } from '@stacks/encryption';

/**
* Encrypt a raw mnemonic phrase to be password protected
Expand Down
6 changes: 3 additions & 3 deletions packages/keychain/src/identity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { bip32, ECPair } from 'bitcoinjs-lib';
import { getPublicKeyFromPrivate } from 'blockstack/lib/keys';
import { makeAuthResponse } from 'blockstack/lib/auth/authMessages';
import { getPublicKeyFromPrivate } from '@stacks/encryption';
import { makeAuthResponse } from '@stacks//auth';
import { getProfileURLFromZoneFile } from './utils';

import { IdentityKeyPair } from './utils/index';
Expand All @@ -12,7 +12,7 @@ import {
} from './utils/gaia';
import IdentityAddressOwnerNode from './nodes/identity-address-owner-node';
import { Profile, fetchProfile, DEFAULT_PROFILE, signAndUploadProfile } from './profiles';
import { ecPairToAddress } from 'blockstack';
import { ecPairToAddress } from '@stacks/encryption';

interface IdentityConstructorOptions {
keyPair: IdentityKeyPair;
Expand Down
2 changes: 1 addition & 1 deletion packages/keychain/src/mnemonic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import randomBytes from 'randombytes';
import { bip32 } from 'bitcoinjs-lib';

import { encrypt } from '../encryption/encrypt';
import { encryptMnemonic } from 'blockstack';
import { encryptMnemonic } from '@stacks/encryption';

export type AllowedKeyEntropyBits = 128 | 256;

Expand Down
15 changes: 12 additions & 3 deletions packages/keychain/src/nodes/identity-address-owner-node.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { BIP32Interface } from 'bitcoinjs-lib';
import { getLegacyAppNode } from 'blockstack/lib/wallet';
import { publicKeyToAddress } from 'blockstack/lib/keys';
import {
publicKeyToAddress,
hashSha256Sync,
hashCode
} from '@stacks/encryption';
import { getAddress } from '../utils';
import bip32 from 'bip32';

const APPS_NODE_INDEX = 0;
const SIGNING_NODE_INDEX = 1;
Expand Down Expand Up @@ -58,7 +62,12 @@ export default class IdentityAddressOwnerNode {
}

getAppNode(appDomain: string) {
return getLegacyAppNode(this.hdNode, this.salt, appDomain);

const hashBuffer = hashSha256Sync(Buffer.from(`${appDomain}${this.salt}`))
const hash = hashBuffer.toString('hex')
const appIndex = hashCode(hash)
const appNodeInstance = typeof this.hdNode === 'string' ? bip32.fromBase58(this.hdNode) : this.hdNode
return appNodeInstance.deriveHardened(appIndex)
}

getAppPrivateKey(appDomain: string) {
Expand Down
8 changes: 5 additions & 3 deletions packages/keychain/src/profiles.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import {
connectToGaiaHub,
} from '@stacks/storage';
import {
signProfileToken,
wrapProfileToken,
connectToGaiaHub,
makeProfileZoneFile,
} from 'blockstack';
} from '@stacks/profile';
import { IdentityKeyPair } from './utils';
import { uploadToGaiaHub } from './utils/gaia';
import Identity from './identity';
import { GaiaHubConfig } from 'blockstack/lib/storage/hub';
import { GaiaHubConfig } from '@stacks/storage';

const PERSON_TYPE = 'Person';
const CONTEXT = 'http://schema.org';
Expand Down
9 changes: 6 additions & 3 deletions packages/keychain/src/utils/gaia.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { TokenSigner, Json } from 'jsontokens';
import { getPublicKeyFromPrivate } from 'blockstack/lib/keys';
import {
getPublicKeyFromPrivate,
ecPairToAddress,
hexStringToECPair
} from '@stacks/encryption';
import randomBytes from 'randombytes';
import { ecPairToAddress, hexStringToECPair } from 'blockstack';
import { GaiaHubConfig } from 'blockstack/lib/storage/hub';
import { GaiaHubConfig } from '@stacks/storage';

export const DEFAULT_GAIA_HUB = 'https://gaia.blockstack.org/hub/';

Expand Down
6 changes: 4 additions & 2 deletions packages/keychain/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { BIP32Interface } from 'bitcoinjs-lib';
import IdentityAddressOwnerNode from '../nodes/identity-address-owner-node';
import { createSha2Hash } from 'blockstack/lib/encryption/sha2Hash';
import { publicKeyToAddress } from 'blockstack/lib/keys';
import {
createSha2Hash,
publicKeyToAddress
} from '@stacks/encryption';
import { parseZoneFile } from 'zone-file';
import Identity from '../identity';
import { AssertionError } from 'assert';
Expand Down
13 changes: 8 additions & 5 deletions packages/keychain/src/wallet/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mnemonicToSeed } from 'bip39';
import { bip32, BIP32Interface } from 'bitcoinjs-lib';
import { ChainID } from '@blockstack/stacks-transactions';
import { ChainID } from '@stacks/transactions';

import {
getBlockchainIdentities,
Expand All @@ -12,19 +12,22 @@ import {
import Identity from '../identity';
import { decrypt } from '../encryption/decrypt';
import {
connectToGaiaHub,
GaiaHubConfig,
connectToGaiaHub
} from '@stacks/storage';
import {
encryptContent,
getPublicKeyFromPrivate,
decryptContent,
} from 'blockstack';
getPublicKeyFromPrivate
} from '@stacks/encryption';

import {
AllowedKeyEntropyBits,
generateEncryptedMnemonicRootKeychain,
deriveRootKeychainFromMnemonic,
encryptMnemonicFormatted,
} from '../mnemonic';
import { deriveStxAddressChain } from '../address-derivation';
import { GaiaHubConfig } from 'blockstack/lib/storage/hub';
import { makeReadOnlyGaiaConfig, DEFAULT_GAIA_HUB, uploadToGaiaHub } from '../utils/gaia';
import { WalletSigner } from './signer';

Expand Down
11 changes: 7 additions & 4 deletions packages/keychain/src/wallet/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import {
makeContractDeploy,
TransactionVersion,
ClarityValue,
StacksTestnet,
makeSTXTokenTransfer,
PostConditionMode,
getAddressFromPrivateKey,
PostCondition,
StacksNetwork,
} from '@blockstack/stacks-transactions';
PostCondition
} from '@stacks/transactions';
import {
StacksTestnet,
StacksNetwork
} from '@stacks/network';

import RPCClient from '@blockstack/rpc-client';
import { bip32 } from 'bitcoinjs-lib';
import { assertIsTruthy } from '../utils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChainID } from '@blockstack/stacks-transactions';
import { ChainID } from '@stacks/transactions';
import { BIP32Interface } from 'bitcoinjs-lib';

import { deriveStxAddressChain } from '../../src/address-derivation';
Expand Down
2 changes: 1 addition & 1 deletion packages/keychain/tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Wallet from '../src/wallet';
import { ChainID } from '@blockstack/stacks-transactions';
import { ChainID } from '@stacks/transactions';

const defaultSeed =
'sound idle panel often situate develop unit text design antenna ' +
Expand Down
5 changes: 2 additions & 3 deletions packages/keychain/tests/identity.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import './setup';
import { makeECPrivateKey, getPublicKeyFromPrivate } from 'blockstack/lib/keys';
import { decryptPrivateKey } from 'blockstack/lib/auth/authMessages';
import { makeECPrivateKey, getPublicKeyFromPrivate, ecPairToAddress } from '@stacks/encryption';
import { decryptPrivateKey } from '@stacks/auth';
import { decodeToken } from 'jsontokens';
import { getIdentity, profileResponse, nameInfoResponse } from './helpers';
import { ecPairToAddress } from 'blockstack';
import { ECPair } from 'bitcoinjs-lib';

interface Decoded {
Expand Down
2 changes: 1 addition & 1 deletion packages/keychain/tests/profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../src/profiles';
import { getIdentity, getNewIdentity } from './helpers';
import { decodeToken, TokenVerifier } from 'jsontokens';
import { makeProfileZoneFile } from 'blockstack';
import { makeProfileZoneFile } from '@stacks/profile';

describe('signProfileForUpload', () => {
it('should create a signed JSON string', async () => {
Expand Down
3 changes: 0 additions & 3 deletions packages/keychain/tests/setup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import './global-setup';
import { GlobalWithFetchMock } from 'jest-fetch-mock';
import { config as bskConfig } from 'blockstack';

bskConfig.logLevel = 'none';

const customGlobal: GlobalWithFetchMock = (global as any) as GlobalWithFetchMock;
customGlobal.fetch = require('jest-fetch-mock');
Expand Down
2 changes: 1 addition & 1 deletion packages/keychain/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Subdomains, registrars, Wallet, decrypt } from '../src';
import { mnemonicToSeed } from 'bip39';
import { bip32 } from 'bitcoinjs-lib';
import { profileResponse, nameInfoResponse } from './helpers';
import { ChainID } from '@blockstack/stacks-transactions';
import { ChainID } from '@stacks/transactions';

describe(validateSubdomainFormat.name, () => {
it('returns error state when string less than 8 characters', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/keychain/tests/wallet-signer.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './setup';
import { getWallet } from './helpers';
import { TransactionVersion } from '@blockstack/stacks-transactions';
import { TransactionVersion } from '@stacks/transactions';

const getSigner = async () => {
const wallet = await getWallet();
Expand Down
4 changes: 2 additions & 2 deletions packages/keychain/tests/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import './setup';
import Wallet, { WalletConfig, ConfigApp } from '../src/wallet';
import { decrypt } from '../src/encryption/decrypt';
import { ECPair, bip32 } from 'bitcoinjs-lib';
import { decryptContent, encryptContent, getPublicKeyFromPrivate } from 'blockstack';
import { decryptContent, encryptContent, getPublicKeyFromPrivate } from '@stacks/encryption';
import { DEFAULT_GAIA_HUB } from '../src/utils/gaia';
import { mnemonicToSeed } from 'bip39';
import { ChainID } from '@blockstack/stacks-transactions';
import { ChainID } from '@stacks/transactions';

describe('Restoring a wallet', () => {
test('restores an existing wallet and keychain', async () => {
Expand Down

0 comments on commit c9c4f77

Please sign in to comment.