Skip to content

Commit

Permalink
feat: add fetch middleware for api keys and request init
Browse files Browse the repository at this point in the history
docs: optimize ts docs
  • Loading branch information
janniks committed May 19, 2022
1 parent 32c7d76 commit ef45632
Show file tree
Hide file tree
Showing 44 changed files with 8,743 additions and 9,199 deletions.
16,347 changes: 7,737 additions & 8,610 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@
"rollup-plugin-node-globals": "^1.4.0",
"rollup-plugin-terser": "^7.0.2",
"stream-http": "^3.2.0",
"typedoc": "^0.22.13",
"typedoc": "^0.22.15",
"typescript": "^4.2.4",
"vite-compatible-readable-stream": "^3.6.0"
},
"scripts": {
"bootstrap": "lerna bootstrap",
"build": "lerna run build",
"build:docs": "rimraf docs && typedoc --tsconfig tsconfig.typedoc.json packages/**/src/index.ts --release-version $(node -p \"require('./lerna.json').version\")",
"build:docs": "rimraf docs && RELEASE_VERSION=$(node -p \"require('./lerna.json').version\") && typedoc --tsconfig tsconfig.typedoc.json packages/**/src/index.ts --release-version $RELEASE_VERSION",
"clean": "lerna clean",
"lerna": "lerna",
"lint": "npm run lint:eslint && npm run lint:prettier",
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@stacks/encryption": "^4.0.2",
"@stacks/network": "^4.0.2",
"@stacks/profile": "^4.0.2",
"cross-fetch": "^3.1.4",
"cross-fetch": "^3.1.5",
"jsontokens": "^3.0.0",
"query-string": "^6.13.1"
},
Expand Down
13 changes: 5 additions & 8 deletions packages/auth/src/messages.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { Buffer } from '@stacks/common';
import 'cross-fetch/polyfill';

import { TokenSigner, SECP256K1Client } from 'jsontokens';
import { makeUUID4, nextMonth, getGlobalObject } from '@stacks/common';
import { makeDIDFromAddress } from './dids';
import { Buffer, getGlobalObject, makeUUID4, nextMonth } from '@stacks/common';
import {
encryptECIES,
decryptECIES,
encryptECIES,
makeECPrivateKey,
publicKeyToAddress,
} from '@stacks/encryption';
import { DEFAULT_SCOPE, AuthScope } from './constants';
import { SECP256K1Client, TokenSigner } from 'jsontokens';
import { AuthScope, DEFAULT_SCOPE } from './constants';
import { makeDIDFromAddress } from './dids';

const VERSION = '1.4.0';

Expand Down
5 changes: 2 additions & 3 deletions packages/auth/src/profile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { resolveZoneFileToProfile } from '@stacks/profile';
import { fetchPrivate } from '@stacks/common';
import { StacksMainnet, StacksNetwork, StacksNetworkName } from '@stacks/network';

export interface ProfileLookupOptions {
Expand Down Expand Up @@ -31,13 +30,13 @@ export function lookupProfile(lookupOptions: ProfileLookupOptions): Promise<Reco
let lookupPromise;
if (options.zoneFileLookupURL) {
const url = `${options.zoneFileLookupURL.replace(/\/$/, '')}/${options.username}`;
lookupPromise = fetchPrivate(url).then(response => response.json());
lookupPromise = network.fetchFn(url).then(response => response.json());
} else {
lookupPromise = network.getNameInfo(options.username);
}
return lookupPromise.then((responseJSON: any) => {
if (responseJSON.hasOwnProperty('zonefile') && responseJSON.hasOwnProperty('address')) {
return resolveZoneFileToProfile(responseJSON.zonefile, responseJSON.address);
return resolveZoneFileToProfile(responseJSON.zonefile, responseJSON.address, network.fetchFn);
} else {
throw new Error(
'Invalid zonefile lookup response: did not contain `address`' + ' or `zonefile` field'
Expand Down
10 changes: 7 additions & 3 deletions packages/auth/src/provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as queryString from 'query-string';
import { decodeToken } from 'jsontokens';
import { BLOCKSTACK_HANDLER, getGlobalObject, fetchPrivate } from '@stacks/common';
import { BLOCKSTACK_HANDLER, getGlobalObject } from '@stacks/common';
import { createFetchFn, FetchFn } from '@stacks/network';

/**
* Retrieves the authentication request from the query string
Expand Down Expand Up @@ -36,7 +37,10 @@ export function getAuthRequestFromURL() {
* @private
* @ignore
*/
export async function fetchAppManifest(authRequest: string): Promise<any> {
export async function fetchAppManifest(
authRequest: string,
fetchFn: FetchFn = createFetchFn()
): Promise<any> {
if (!authRequest) {
throw new Error('Invalid auth request');
}
Expand All @@ -47,7 +51,7 @@ export async function fetchAppManifest(authRequest: string): Promise<any> {
const manifestURI = payload.manifest_uri as string;
try {
// Logger.debug(`Fetching manifest from ${manifestURI}`)
const response = await fetchPrivate(manifestURI);
const response = await fetchFn(manifestURI);
const responseText = await response.text();
const responseJSON = JSON.parse(responseText);
return { ...responseJSON, manifestURI };
Expand Down
8 changes: 4 additions & 4 deletions packages/auth/src/userSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
import { getAddressFromDID } from './dids';
import {
BLOCKSTACK_DEFAULT_GAIA_HUB_URL,
fetchPrivate,
getGlobalObject,
InvalidStateError,
isLaterVersion,
Expand All @@ -28,7 +27,7 @@ import { extractProfile } from '@stacks/profile';
import { AuthScope, DEFAULT_PROFILE } from './constants';
import * as queryString from 'query-string';
import { UserData } from './userData';
import { StacksMainnet } from '@stacks/network';
import { createFetchFn, FetchFn, StacksMainnet } from '@stacks/network';
import { protocolEchoReplyDetection } from './protocolEchoDetection';

/**
Expand Down Expand Up @@ -214,7 +213,8 @@ export class UserSession {
* if handling the sign in request fails or there was no pending sign in request.
*/
async handlePendingSignIn(
authResponseToken: string = this.getAuthResponseToken()
authResponseToken: string = this.getAuthResponseToken(),
fetchFn: FetchFn = createFetchFn()
): Promise<UserData> {
const sessionData = this.store.getSessionData();

Expand Down Expand Up @@ -312,7 +312,7 @@ export class UserSession {
};
const profileURL = tokenPayload.profile_url as string;
if (!userData.profile && profileURL) {
const response = await fetchPrivate(profileURL);
const response = await fetchFn(profileURL);
if (!response.ok) {
// return blank profile if we fail to fetch
userData.profile = Object.assign({}, DEFAULT_PROFILE);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"bitcoinjs-lib": "^5.2.0",
"c32check": "^1.1.3",
"cors": "^2.8.4",
"cross-fetch": "^3.1.4",
"cross-fetch": "^3.1.5",
"express": "^4.17.1",
"express-winston": "^3.1.0",
"inquirer": "^7.1.0",
Expand Down
25 changes: 14 additions & 11 deletions packages/cli/src/network.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import blockstack from 'blockstack';
import { createFetchFn, FetchFn } from '@stacks/network';
import * as bitcoin from 'bitcoinjs-lib';
import fetch from 'node-fetch';

import { CLI_CONFIG_TYPE } from './argparse';

import blockstack from 'blockstack';
import { BlockstackNetwork } from 'blockstack/lib/network';
import { CLI_CONFIG_TYPE } from './argparse';

export interface CLI_NETWORK_OPTS {
consensusHash: string | null;
Expand Down Expand Up @@ -186,15 +184,16 @@ export class CLINetworkAdapter {
getNamespaceBurnAddress(
namespace: string,
useCLI: boolean = true,
receiveFeesPeriod: number = -1
receiveFeesPeriod: number = -1,
fetchFn: FetchFn = createFetchFn()
): Promise<string> {
// override with CLI option
if (this.namespaceBurnAddress && useCLI) {
return new Promise((resolve: any) => resolve(this.namespaceBurnAddress));
}

return Promise.all([
fetch(`${this.legacyNetwork.blockstackAPIUrl}/v1/namespaces/${namespace}`),
fetchFn(`${this.legacyNetwork.blockstackAPIUrl}/v1/namespaces/${namespace}`),
this.legacyNetwork.getBlockHeight(),
])
.then(([resp, blockHeight]: [any, number]) => {
Expand Down Expand Up @@ -244,10 +243,10 @@ export class CLINetworkAdapter {
});
}

getBlockchainNameRecord(name: string): Promise<any> {
getBlockchainNameRecord(name: string, fetchFn: FetchFn = createFetchFn()): Promise<any> {
// TODO: send to blockstack.js
const url = `${this.legacyNetwork.blockstackAPIUrl}/v1/blockchains/bitcoin/names/${name}`;
return fetch(url)
return fetchFn(url)
.then(resp => {
if (resp.status !== 200) {
throw new Error(`Bad response status: ${resp.status}`);
Expand All @@ -267,10 +266,14 @@ export class CLINetworkAdapter {
});
}

getNameHistory(name: string, page: number): Promise<Record<string, any[]>> {
getNameHistory(
name: string,
page: number,
fetchFn: FetchFn = createFetchFn()
): Promise<Record<string, any[]>> {
// TODO: send to blockstack.js
const url = `${this.legacyNetwork.blockstackAPIUrl}/v1/names/${name}/history?page=${page}`;
return fetch(url)
return fetchFn(url)
.then(resp => {
if (resp.status !== 200) {
throw new Error(`Bad response status: ${resp.status}`);
Expand Down
3 changes: 1 addition & 2 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
"dependencies": {
"@types/bn.js": "^5.1.0",
"@types/node": "^14.14.43",
"buffer": "^6.0.3",
"cross-fetch": "^3.1.4"
"buffer": "^6.0.3"
},
"devDependencies": {
"@rollup/plugin-typescript": "^8.3.0",
Expand Down
38 changes: 0 additions & 38 deletions packages/common/src/fetchUtil.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from './config';
export * from './errors';
export * from './fetchUtil';
export * from './logger';
export * from './utils';
export * from './constants';
Expand Down
6 changes: 3 additions & 3 deletions packages/keychain/src/identity.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { makeAuthResponse } from '@stacks/auth';
import { getPublicKeyFromPrivate, publicKeyToAddress } from '@stacks/encryption';
import { createFetchFn, FetchFn } from '@stacks/network';
import { bip32 } from 'bitcoinjs-lib';
import { Identity as IdentifyInterface, Profile } from './common';
import IdentityAddressOwnerNode from './nodes/identity-address-owner-node';
import { DEFAULT_PROFILE, fetchProfile, signAndUploadProfile } from './profiles';
import { getProfileURLFromZoneFile, IdentityKeyPair } from './utils';
import { fetchPrivate } from '@stacks/common';
import {
connectToGaiaHubWithConfig,
DEFAULT_GAIA_HUB,
Expand Down Expand Up @@ -130,9 +130,9 @@ export class Identity implements IdentifyInterface {
return `${gaiaUrl}${this.address}/profile.json`;
}

async fetchNames() {
async fetchNames(fetchFn: FetchFn = createFetchFn()) {
const getNamesUrl = `https://stacks-node-api.stacks.co/v1/addresses/bitcoin/${this.address}`;
const res = await fetchPrivate(getNamesUrl);
const res = await fetchFn(getNamesUrl);
const data = await res.json();
const { names }: { names: string[] } = data;
return names;
Expand Down
15 changes: 9 additions & 6 deletions packages/keychain/src/profiles.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { connectToGaiaHub } from '@stacks/storage';
import { signProfileToken, wrapProfileToken, makeProfileZoneFile } from '@stacks/profile';
import { createFetchFn, FetchFn } from '@stacks/network';
import { makeProfileZoneFile, signProfileToken, wrapProfileToken } from '@stacks/profile';
import { connectToGaiaHub, GaiaHubConfig } from '@stacks/storage';
import { Identity, Profile } from './common';
import { IdentityKeyPair } from './utils';
import { uploadToGaiaHub } from './utils/gaia';
import { GaiaHubConfig } from '@stacks/storage';
import { fetchPrivate } from '@stacks/common';

export const DEFAULT_PROFILE: Profile = {
'@type': 'Person',
Expand Down Expand Up @@ -61,13 +60,15 @@ interface SendToRegistrarParams {
subdomain: Subdomains;
zoneFile: string;
identity: Identity;
fetchFn?: FetchFn;
}

const sendUsernameToRegistrar = async ({
username,
subdomain,
zoneFile,
identity,
fetchFn = createFetchFn(),
}: SendToRegistrarParams) => {
const { registerUrl } = registrars[subdomain];

Expand All @@ -82,7 +83,7 @@ const sendUsernameToRegistrar = async ({
'Content-Type': 'application/json',
};

const response = await fetchPrivate(registerUrl, {
const response = await fetchFn(registerUrl, {
method: 'POST',
headers: requestHeaders,
body: registrationRequestBody,
Expand Down Expand Up @@ -151,13 +152,15 @@ export const signAndUploadProfile = async ({
export const fetchProfile = async ({
identity,
gaiaUrl,
fetchFn = createFetchFn(),
}: {
identity: Identity;
gaiaUrl: string;
fetchFn?: FetchFn;
}) => {
try {
const url = await identity.profileUrl(gaiaUrl);
const res = await fetchPrivate(url);
const res = await fetchFn(url);
if (res.ok) {
const json = await res.json();
const { decodedToken } = json[0];
Expand Down

0 comments on commit ef45632

Please sign in to comment.