Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate-lookups-file-flow-to-ts #82

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
254dc5f
feat: migrate-lookups-file-flow-to-ts
Akirtovskis Mar 15, 2022
14923cf
fix: upd package json lock
Akirtovskis Mar 15, 2022
4af2459
fix: test removing the lock file
Akirtovskis Mar 15, 2022
ad20a4a
Revert "fix: test removing the lock file"
Akirtovskis Mar 15, 2022
5ab3fe1
Merge branch 'master' into feature/migrate-flow-to-ts
Akirtovskis Mar 15, 2022
1b98458
fix: add type of owner
Akirtovskis Mar 15, 2022
e61d6be
Merge branch 'feature/migrate-flow-to-ts' of https://github.com/Akirt…
Akirtovskis Mar 15, 2022
4d11dcb
fix: remove old file + fix import cheerio
Akirtovskis Mar 21, 2022
540550d
fix: upd with the new file
Akirtovskis Mar 21, 2022
c135d11
fix: add ts config to babel
Akirtovskis Mar 21, 2022
3501ae9
fix: add babel ts
Akirtovskis Mar 21, 2022
4525102
fix: try upd flow config
Akirtovskis Mar 21, 2022
63445ef
fix: add ts config to babel
Akirtovskis Mar 22, 2022
41772e0
fix: upd import
Akirtovskis Mar 22, 2022
933dac3
fix: enable tests
Akirtovskis Mar 22, 2022
1a9b238
fix: feedback updates
Akirtovskis Mar 23, 2022
3a9fdef
fix: upd tsconfig
Akirtovskis Mar 23, 2022
901db61
fix: minor
Akirtovskis Mar 23, 2022
2a57403
fix: minor
Akirtovskis Mar 23, 2022
e2b2d47
fix: upd package json
Akirtovskis Mar 23, 2022
63e7be3
fix: removing cheerio types
Akirtovskis Mar 23, 2022
be95a7e
fix: feedback fixes
Akirtovskis Mar 24, 2022
b5b9909
fix: remove cheerio
Akirtovskis Mar 24, 2022
6539d2e
fix: add cheerio
Akirtovskis Mar 24, 2022
8cea79a
fix: add cheerio
Akirtovskis Mar 24, 2022
d405ba7
fix: remove cheerio
Akirtovskis Mar 24, 2022
784dc8a
fix: remove unused import
Akirtovskis Mar 24, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
{
"presets": [
[ "@babel/preset-env",
{ "targets": { "node": 12 } } ],
{
"targets": {
"node": 12
}
}],
"@babel/preset-flow"],
"overrides": [
{
"test": ["./src/**/*.ts"],
"presets": [
"@babel/preset-typescript",
[
"@babel/preset-env",
{
"targets": {
"node": 12,
}
}
],
]
}
],
"plugins": ["istanbul"]
}
}


7 changes: 1 addition & 6 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@
.*/node_modules/.staging/*
.*/node_modules/npm/*

[include]

[libs]

[options]

[lints]
module.file_ext=.ts
867 changes: 547 additions & 320 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
"devDependencies": {
"@babel/cli": "^7.6.3",
"@babel/core": "^7.6.3",
"@babel/plugin-transform-typescript": "^7.16.8",
"@babel/preset-env": "^7.6.3",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-typescript": "^7.16.7",
"@babel/register": "^7.7.4",
"@commitlint/cli": "^12.1.1",
"@commitlint/config-conventional": "^12.1.1",
"@istanbuljs/nyc-config-babel": "^2.1.1",
"@types/node-fetch": "^2.6.1",
"@types/sqlite3": "^3.1.8",
"babel-eslint": "^10.0.3",
"babel-plugin-istanbul": "^5.2.0",
"dotenv": "^8.2.0",
Expand All @@ -38,15 +42,16 @@
"husky": "^6.0.0",
"nock": "^9.1.6",
"nyc": "^14.1.1",
"tape": "^4.6.3"
"tape": "^4.6.3",
"typescript": "^4.6.2"
},
"bin": {
"blockstack-subdomain-registrar": "lib/index.js"
},
"scripts": {
"start": "npm run build && node lib/index.js",
"start:regtest": "BSK_SUBDOMAIN_CONFIG=config-develop.json BSK_SUBDOMAIN_REGTEST=true npm run start",
"build": "babel src -d lib",
"build": "tsc && babel src -d lib",
"flow": "flow",
"lint": "eslint src",
"lint:fix": "eslint src --fix",
Expand Down Expand Up @@ -102,4 +107,4 @@
"@semantic-release/git"
]
}
}
}
76 changes: 0 additions & 76 deletions src/lookups.js

This file was deleted.

87 changes: 87 additions & 0 deletions src/lookups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {
Akirtovskis marked this conversation as resolved.
Show resolved Hide resolved
config as bskConfig,
validateProofs,
resolveZoneFileToProfile,
} from "blockstack";
import { validateStacksAddress } from "@stacks/transactions";
import fetch from "node-fetch";
import logger from "winston";

export async function isSubdomainRegistered(fullyQualifiedAddress: string) {
try {
const nameInfoUrl =
bskConfig.network.blockstackAPIUrl + "/v1/names/" + fullyQualifiedAddress;
const nameInfoRequest = await fetch(nameInfoUrl, {});
const { status } = nameInfoRequest;
const nameInfo = await nameInfoRequest.json();
if (status == 200) {
return nameInfo.status === "registered_subdomain";
} else {
return false;
}
} catch (err) {
if (err.message === "Name not found") {
return false;
} else if (err.message === "Bad response status: 500") {
return false; // currently, the blockstack api returns 500 on subdomain lookup errors.
} else {
throw err;
}
}
}

export function validlySignedUpdate() {
throw new Error("Not implemented");
}

export async function checkProofs(owner: string, zonefile: any) {
const profile = await resolveZoneFileToProfile(zonefile, owner);
// ts-ignore used below to not modify the code and not add the cheerio module that is in the validate proofs fn type
// @ts-ignore
const proofs = await validateProofs(profile, owner);
return proofs.filter((x) => x.valid);
}

export async function isRegistrationValid(
subdomainName: string,
domainName: string,
owner: string,
sequenceNumber: number,
checkCore: boolean
) {
// currently, only support *new* subdomains
if (sequenceNumber !== 0) {
logger.debug(`seqn: ${sequenceNumber} failed validation`);
return false;
}

// owner should be a stacks address
if (!validateStacksAddress(owner)) {
logger.debug(`owner: ${owner} failed validation`);
return false;
}

// subdomain name should be a legal name
const subdomainRegex = /^[a-z0-9\-_+]{1,37}$/;
if (!subdomainRegex.test(subdomainName)) {
logger.debug(`subdomainName: ${subdomainName} failed validation`);
return false;
}
if (!checkCore) {
return true;
}

// shouldn't already exist
try {
const isRegistered = await isSubdomainRegistered(
`${subdomainName}.${domainName}`
);
if (isRegistered) {
logger.warn(`${subdomainName}.${domainName} already exists`);
}
return !isRegistered;
} catch (e) {
logger.error(e);
return false;
}
}
1 change: 1 addition & 0 deletions src/types/winston.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "winston";
27 changes: 27 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"moduleResolution": "node",
"declaration": false,
"strict": true,
"sourceMap": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": false,
"useUnknownInCatchVariables": false,
"resolveJsonModule": true,
"skipLibCheck": true,
"outDir": "lib",
"noEmit": true,
"baseUrl": ".",
"allowJs": true
},
"include": [
"src/lookups.ts",
"src/types",
],
"exclude": [
"lib",
"node_modules"
]
}