Skip to content

Commit

Permalink
feat: clean up legacy code
Browse files Browse the repository at this point in the history
  • Loading branch information
yknl committed Aug 23, 2020
1 parent 5f4a961 commit faa3620
Show file tree
Hide file tree
Showing 55 changed files with 56 additions and 41 deletions.
13 changes: 11 additions & 2 deletions packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"author": "yknl <[email protected]>",
"homepage": "https://blockstack.org",
"license": "GPL-3.0-or-later",
"main": "lib/index.js",
"main": "./dist/index.js",
"umd:main": "./dist/auth.umd.production.js",
"module": "./dist/auth.esm.js",
"directories": {
"lib": "lib",
"test": "__tests__"
Expand All @@ -26,7 +28,12 @@
"url": "git+https://github.com/blockstack/blockstack.js.git"
},
"scripts": {
"build": "rimraf lib && tsc -b tsconfig.build.json",
"build": "cross-env NODE_ENV=production tsdx build --format=cjs,esm,umd",
"build-all": "run-p build:*",
"build:cjs": "tsc --outDir ./lib -m commonjs -t es2017",
"build:esm": "tsc --outDir ./lib-esm -m es6 -t es2017",
"build:cjs:watch": "tsc --outDir ./lib -m commonjs -t es2017 --watch",
"build:esm:watch": "tsc --outDir ./lib-esm -m es6 -t es2017 --watch",
"test": "echo \"Error: run tests from root\" && exit 1"
},
"bugs": {
Expand All @@ -35,6 +42,8 @@
"dependencies": {
"@stacks/common": "^1.0.0",
"@stacks/encryption": "^1.0.0",
"@stacks/network": "^1.0.0",
"@stacks/profile": "^1.0.0",
"cross-fetch": "^3.0.5",
"jsontokens": "^3.0.0",
"query-string": "^6.13.1"
Expand Down
31 changes: 17 additions & 14 deletions packages/auth/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import { decryptPrivateKey } from './messages'
import {
NAME_LOOKUP_PATH
} from './constants'
import { extractProfile } from './legacy/profiles/profileTokens'
import { extractProfile } from '@stacks/profile'
import { UserSession } from './userSession'
import { config } from './legacy/config'
// import { GaiaHubConfig } from './legacy/storage/hub'
import { hexStringToECPair } from '@stacks/encryption'
import { StacksMainnet } from '@stacks/network'


const DEFAULT_PROFILE = {
Expand Down Expand Up @@ -130,23 +129,27 @@ export async function handlePendingSignIn(
if (!nameLookupURL) {
let coreNode = caller.appConfig && caller.appConfig.coreNode
if (!coreNode) {
coreNode = config.network.blockstackAPIUrl
let network = new StacksMainnet()
coreNode = network.coreApiUrl
}

const tokenPayload = decodeToken(authResponseToken).payload
if (typeof tokenPayload === 'string') {
throw new Error('Unexpected token payload type of string')
}
if (isLaterVersion(tokenPayload.version as string, '1.3.0')
&& tokenPayload.blockstackAPIUrl !== null && tokenPayload.blockstackAPIUrl !== undefined) {
// override globally
Logger.info(`Overriding ${config.network.blockstackAPIUrl} `
+ `with ${tokenPayload.blockstackAPIUrl}`)
// TODO: this config is never saved so the user node preference
// is not respected in later sessions..
config.network.blockstackAPIUrl = tokenPayload.blockstackAPIUrl as string
coreNode = tokenPayload.blockstackAPIUrl as string
}

// Section below is removed since the config was never persisted and therefore useless

// if (isLaterVersion(tokenPayload.version as string, '1.3.0')
// && tokenPayload.blockstackAPIUrl !== null && tokenPayload.blockstackAPIUrl !== undefined) {
// // override globally
// Logger.info(`Overriding ${config.network.blockstackAPIUrl} `
// + `with ${tokenPayload.blockstackAPIUrl}`)
// // TODO: this config is never saved so the user node preference
// // is not respected in later sessions..
// config.network.blockstackAPIUrl = tokenPayload.blockstackAPIUrl as string
// coreNode = tokenPayload.blockstackAPIUrl as string
// }

nameLookupURL = `${coreNode}${NAME_LOOKUP_PATH}`
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ec as EllipticCurve } from 'elliptic'
import * as BN from 'bn.js'
import { randomBytes } from './cryptoRandom'
import { FailedDecryptionError } from '../errors'
import { getPublicKeyFromPrivate } from '../keys'
import { getPublicKeyFromPrivate } from '@stacks/encryption'
import { hashSha256Sync, hashSha512Sync } from './sha2Hash'
import { createHmacSha256 } from './hmacSha256'
import { createCipher } from './aesCipher'
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ECPair } from 'bitcoinjs-lib'
import { decodeToken, SECP256K1Client, TokenSigner, TokenVerifier } from 'jsontokens'
import { TokenInterface } from 'jsontokens/lib/decode'
import { nextYear, makeUUID4 } from '@stacks/common'
import { ecPairToAddress } from '../keys'
import { ecPairToAddress } from '@stacks/encryption'

/**
* Signs a profile token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ export {
makeDIDFromAddress, makeDIDFromPublicKey, getDIDType, getAddressFromDID
} from '../dids'

export {
getEntropy, makeECPrivateKey, publicKeyToAddress, getPublicKeyFromPrivate,
hexStringToECPair, ecPairToHexString, ecPairToAddress
} from './keys'

export {
transactions, safety, TransactionSigner,
PubkeyHashSigner, addUTXOsToFund, estimateTXBytes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { encryptMnemonic, decryptMnemonic } from './encryption/wallet'
import { randomBytes, GetRandomBytes } from './encryption/cryptoRandom'
import { hashSha256Sync } from './encryption/sha2Hash'
import { TriplesecDecryptSignature } from './encryption/cryptoUtils'
import { ecPairToHexString } from './keys'
import { ecPairToHexString } from '@stacks/encryption'

const APPS_NODE_INDEX = 0
const IDENTITY_KEYCHAIN = 888
Expand Down
3 changes: 1 addition & 2 deletions packages/auth/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
import 'cross-fetch/polyfill'

import { TokenSigner, SECP256K1Client } from 'jsontokens'
import { makeECPrivateKey, publicKeyToAddress } from './legacy/keys'
import { makeUUID4, nextMonth, getGlobalObject, Logger } from '@stacks/common'
import { makeDIDFromAddress } from './dids'
import { encryptECIES, decryptECIES } from './legacy/encryption/ec'
import { encryptECIES, decryptECIES, makeECPrivateKey, publicKeyToAddress } from '@stacks/encryption'
import { DEFAULT_SCOPE, AuthScope } from './constants'
import { UserSession } from './userSession'

Expand Down
28 changes: 17 additions & 11 deletions packages/auth/src/profile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@

import { resolveZoneFileToProfile } from './legacy/profiles/profileZoneFiles'
import { fetchPrivate } from '@stacks/common'
import { config } from './legacy/config'
import { resolveZoneFileToProfile } from '@stacks/profile'
import { fetchPrivate} from '@stacks/common'
import { StacksNetwork, StacksMainnet } from '@stacks/network'

export interface ProfileLookupOptions {
username: string;
zoneFileLookupURL?: string;
network?: StacksNetwork;
}

/**
* Look up a user profile by blockstack ID
Expand All @@ -12,21 +18,21 @@ import { config } from './legacy/config'
* blockstack.js [[getNameInfo]] function.
* @returns {Promise} that resolves to a profile object
*/
export function lookupProfile(username: string, zoneFileLookupURL?: string):
Promise<Record<string, any>> {
if (!username) {
export function lookupProfile(options: ProfileLookupOptions): Promise<Record<string, any>> {
if (!options.username) {
return Promise.reject()
}
let network: StacksNetwork = options.network ? options.network : new StacksMainnet
let lookupPromise
if (zoneFileLookupURL) {
const url = `${zoneFileLookupURL.replace(/\/$/, '')}/${username}`
if (options.zoneFileLookupURL) {
const url = `${options.zoneFileLookupURL.replace(/\/$/, '')}/${options.username}`
lookupPromise = fetchPrivate(url)
.then(response => response.json())
} else {
lookupPromise = config.network.getNameInfo(username)
lookupPromise = network.getNameInfo(options.username)
}
return lookupPromise
.then((responseJSON) => {
.then((responseJSON: any) => {
if (responseJSON.hasOwnProperty('zonefile')
&& responseJSON.hasOwnProperty('address')) {
return resolveZoneFileToProfile(responseJSON.zonefile, responseJSON.address)
Expand All @@ -35,4 +41,4 @@ export function lookupProfile(username: string, zoneFileLookupURL?: string):
+ ' or `zonefile` field')
}
})
}
}
2 changes: 0 additions & 2 deletions packages/auth/src/sessionData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@

// import { GaiaHubConfig } from './legacy/storage/hub'
import { InvalidStateError } from '@stacks/common'
import { UserData } from './auth'

Expand Down
1 change: 0 additions & 1 deletion packages/auth/src/userSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from './sessionStore'

import * as authMessages from './messages'
// import * as storage from './legacy/storage'

import {
nextHour,
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/verification.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { decodeToken, TokenVerifier } from 'jsontokens'
import { getAddressFromDID } from './dids'
import { publicKeyToAddress } from './legacy/keys'
import { publicKeyToAddress } from '@stacks/encryption'
import { fetchPrivate, isSameOriginAbsoluteUrl } from '@stacks/common'
import { fetchAppManifest } from './provider'

Expand Down
6 changes: 6 additions & 0 deletions packages/auth/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.json",
"include": [
"./src/**/*"
]
}

0 comments on commit faa3620

Please sign in to comment.