@sentio/cli 1.0.0-development
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/build.d.ts +2 -0
- package/lib/build.js +82 -0
- package/lib/build.js.map +1 -0
- package/lib/cli.d.ts +2 -0
- package/lib/cli.js +186 -0
- package/lib/cli.js.map +1 -0
- package/lib/commands/login-server.d.ts +7 -0
- package/lib/commands/login-server.js +133 -0
- package/lib/commands/login-server.js.map +1 -0
- package/lib/commands/run-create.d.ts +1 -0
- package/lib/commands/run-create.js +112 -0
- package/lib/commands/run-create.js.map +1 -0
- package/lib/commands/run-login.d.ts +1 -0
- package/lib/commands/run-login.js +136 -0
- package/lib/commands/run-login.js.map +1 -0
- package/lib/commands/run-version.d.ts +1 -0
- package/lib/commands/run-version.js +39 -0
- package/lib/commands/run-version.js.map +1 -0
- package/lib/config.d.ts +14 -0
- package/lib/config.js +64 -0
- package/lib/config.js.map +1 -0
- package/lib/key.d.ts +2 -0
- package/lib/key.js +44 -0
- package/lib/key.js.map +1 -0
- package/lib/upload.d.ts +2 -0
- package/lib/upload.js +189 -0
- package/lib/upload.js.map +1 -0
- package/lib/utils.d.ts +1 -0
- package/lib/utils.js +16 -0
- package/lib/utils.js.map +1 -0
- package/lib/webpack.config.js +47 -0
- package/package.json +63 -0
- package/src/build.ts +96 -0
- package/src/cli.ts +184 -0
- package/src/commands/login-server.ts +119 -0
- package/src/commands/run-create.ts +116 -0
- package/src/commands/run-login.ts +111 -0
- package/src/commands/run-version.ts +32 -0
- package/src/config.ts +72 -0
- package/src/key.ts +43 -0
- package/src/upload.ts +214 -0
- package/src/utils.ts +10 -0
- package/src/webpack.config.js +47 -0
- package/templates/aptos/abis/aptos/souffle.json +389 -0
- package/templates/aptos/jest.config.js +7 -0
- package/templates/aptos/package.json +20 -0
- package/templates/aptos/sentio.yaml +1 -0
- package/templates/aptos/src/processor.ts +13 -0
- package/templates/aptos/tsconfig.json +20 -0
- package/templates/evm/abis/evm/x2y2.json +296 -0
- package/templates/evm/jest.config.js +7 -0
- package/templates/evm/package.json +20 -0
- package/templates/evm/sentio.yaml +3 -0
- package/templates/evm/src/processor.ts +29 -0
- package/templates/evm/tsconfig.json +20 -0
- package/templates/raw/jest.config.js +7 -0
- package/templates/raw/package.json +20 -0
- package/templates/raw/sentio.yaml +3 -0
- package/templates/raw/src/processor.ts +0 -0
- package/templates/raw/tsconfig.json +20 -0
- package/templates/raw/yarn.lock +4095 -0
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sentio/cli",
|
|
3
|
+
"license": "Apache-2.0",
|
|
4
|
+
"version": "1.0.0-development",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"compile": "tsc -p . && cp src/webpack.config.js lib/",
|
|
7
|
+
"build": "yarn compile",
|
|
8
|
+
"cli": "ts-node src/cli.ts",
|
|
9
|
+
"test": "jest"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"command-line-args": "^5.2.1",
|
|
13
|
+
"command-line-usage": "^6.1.3",
|
|
14
|
+
"express": "^4.18.2",
|
|
15
|
+
"form-data": "^4.0.0",
|
|
16
|
+
"fs-extra": "^11.0.0",
|
|
17
|
+
"js-yaml": "^4.1.0",
|
|
18
|
+
"node-fetch": "2",
|
|
19
|
+
"open": "^8.4.0",
|
|
20
|
+
"ts-loader": "^9.3.0",
|
|
21
|
+
"webpack": "^5.72.1",
|
|
22
|
+
"webpack-cli": "^5.0.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/chai": "^4.3.1",
|
|
26
|
+
"@types/command-line-args": "^5.2.0",
|
|
27
|
+
"@types/command-line-usage": "^5.0.2",
|
|
28
|
+
"@types/expect": "^24.3.0",
|
|
29
|
+
"@types/express": "^4.17.14",
|
|
30
|
+
"@types/fs-extra": "^9.0.13",
|
|
31
|
+
"@types/google-protobuf": "^3.15.6",
|
|
32
|
+
"@types/js-yaml": "^4.0.5",
|
|
33
|
+
"@types/mkdirp": "^1.0.2",
|
|
34
|
+
"@types/node": "^18.0.4",
|
|
35
|
+
"@types/node-fetch": "^2.6.2",
|
|
36
|
+
"chai": "^4.3.6",
|
|
37
|
+
"clean-publish": "^4.0.1",
|
|
38
|
+
"conventional-changelog-conventionalcommits": "^5.0.0",
|
|
39
|
+
"eslint": "^8.27.0",
|
|
40
|
+
"eslint-import-resolver-typescript": "^3.5.2",
|
|
41
|
+
"eslint-plugin-import": "^2.26.0",
|
|
42
|
+
"jest": "^29.0.3",
|
|
43
|
+
"semantic-release": "^19.0.5",
|
|
44
|
+
"ts-jest": "^29.0.3",
|
|
45
|
+
"ts-node": "^10.9.1",
|
|
46
|
+
"tsconfig-paths": "^4.0.0",
|
|
47
|
+
"typescript": "^4.8.0"
|
|
48
|
+
},
|
|
49
|
+
"bin": {
|
|
50
|
+
"sentio": "./lib/cli.js"
|
|
51
|
+
},
|
|
52
|
+
"main": "./lib/index.js",
|
|
53
|
+
"types": "./lib/index.d.ts",
|
|
54
|
+
"module": "./lib/index.js",
|
|
55
|
+
"files": [
|
|
56
|
+
"{lib,src,templates}",
|
|
57
|
+
"!{lib,src}/tests",
|
|
58
|
+
"!**/*.test.{js,ts}"
|
|
59
|
+
],
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": ">=16"
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/build.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import chalk from 'chalk'
|
|
2
|
+
import path from 'path'
|
|
3
|
+
import fs from 'fs'
|
|
4
|
+
import { exec } from 'child_process'
|
|
5
|
+
|
|
6
|
+
export async function buildProcessor(onlyGen: boolean) {
|
|
7
|
+
if (!onlyGen) {
|
|
8
|
+
await installDeps()
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// targets.forEach(async (target) => await buildProcessorForTarget(onlyGen, target))
|
|
12
|
+
// for (const target) {
|
|
13
|
+
await buildProcessorForTarget(onlyGen)
|
|
14
|
+
// }
|
|
15
|
+
|
|
16
|
+
if (!onlyGen) {
|
|
17
|
+
const WEBPACK_CONFIG = path.join(__dirname, 'webpack.config.js')
|
|
18
|
+
await execStep('yarn tsc -p .', 'Compile')
|
|
19
|
+
await execStep('yarn webpack --config=' + WEBPACK_CONFIG, 'Packaging')
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function buildProcessorForTarget(onlyGen: boolean) {
|
|
24
|
+
await codeGenEthersProcessor(path.join('abis', 'evm'))
|
|
25
|
+
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
27
|
+
const solanaModule = require('@sentio/sdk/lib/solana-codegen/codegen')
|
|
28
|
+
solanaModule.codeGenSolanaProcessor(path.join('abis', 'solana'))
|
|
29
|
+
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
31
|
+
const aptosModuole = require('@sentio/sdk/lib/aptos-codegen/codegen')
|
|
32
|
+
aptosModuole.codeGenAptosProcessor(path.join('abis', 'aptos'))
|
|
33
|
+
|
|
34
|
+
if (onlyGen) {
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function installDeps() {
|
|
40
|
+
await execStep('yarn install --ignore-scripts', 'Yarn Install')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function codeGenEthersProcessor(
|
|
44
|
+
abisDir: string,
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
46
|
+
ETHERS_TARGET = path.dirname(require.resolve('@sentio/sdk/lib/target-ethers-sentio')),
|
|
47
|
+
outDir = 'src/types/internal'
|
|
48
|
+
) {
|
|
49
|
+
if (!fs.existsSync(abisDir)) {
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let haveJson = false
|
|
54
|
+
const files = fs.readdirSync(abisDir)
|
|
55
|
+
for (const file of files) {
|
|
56
|
+
if (file.toLowerCase().endsWith('.json')) {
|
|
57
|
+
haveJson = true
|
|
58
|
+
break
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (!haveJson) {
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
console.log(chalk.green('Generated Types for EVM'))
|
|
66
|
+
|
|
67
|
+
// TODO this will fail during postinstall, need to locate real typechain path
|
|
68
|
+
await execStep(
|
|
69
|
+
'yarn typechain --target ' + ETHERS_TARGET + ` --out-dir ${outDir} ${path.join(abisDir, '*.json')}`,
|
|
70
|
+
'Type definitions gen'
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function execStep(cmd: string, stepName: string) {
|
|
75
|
+
const child = exec(cmd)
|
|
76
|
+
console.log(chalk.blue(stepName + ' begin'))
|
|
77
|
+
|
|
78
|
+
if (!child.stdout || !child.stderr) {
|
|
79
|
+
console.error(chalk.red(stepName + ' failed'))
|
|
80
|
+
process.exit(1)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
child.stdout.pipe(process.stdout)
|
|
84
|
+
child.stderr.pipe(process.stderr)
|
|
85
|
+
|
|
86
|
+
await new Promise((resolve) => {
|
|
87
|
+
child.on('close', resolve)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
if (child.exitCode) {
|
|
91
|
+
console.error(chalk.red(stepName + ' failed'))
|
|
92
|
+
process.exit(child.exitCode)
|
|
93
|
+
}
|
|
94
|
+
console.log(chalk.blue(stepName + ' success'))
|
|
95
|
+
console.log()
|
|
96
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import commandLineArgs from 'command-line-args'
|
|
4
|
+
import commandLineUsage from 'command-line-usage'
|
|
5
|
+
import fs from 'fs'
|
|
6
|
+
import path from 'path'
|
|
7
|
+
|
|
8
|
+
import yaml from 'js-yaml'
|
|
9
|
+
import { finalizeHost, FinalizeProjectName, SentioProjectConfig } from './config'
|
|
10
|
+
import { uploadFile } from './upload'
|
|
11
|
+
import chalk from 'chalk'
|
|
12
|
+
import { buildProcessor } from './build'
|
|
13
|
+
import { runCreate } from './commands/run-create'
|
|
14
|
+
import { runVersion } from './commands/run-version'
|
|
15
|
+
import { runLogin } from './commands/run-login'
|
|
16
|
+
|
|
17
|
+
const mainDefinitions = [{ name: 'command', defaultOption: true }]
|
|
18
|
+
const mainOptions = commandLineArgs(mainDefinitions, {
|
|
19
|
+
stopAtFirstUnknown: true,
|
|
20
|
+
})
|
|
21
|
+
const argv = mainOptions._unknown || []
|
|
22
|
+
|
|
23
|
+
if (!mainOptions.command) {
|
|
24
|
+
usage()
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (mainOptions.command === 'login') {
|
|
28
|
+
runLogin(argv)
|
|
29
|
+
} else if (mainOptions.command === 'create') {
|
|
30
|
+
runCreate(argv)
|
|
31
|
+
} else if (mainOptions.command === 'version') {
|
|
32
|
+
runVersion(argv)
|
|
33
|
+
} else {
|
|
34
|
+
// For all the commands that need read project configs
|
|
35
|
+
// TODO move them to their own modules
|
|
36
|
+
|
|
37
|
+
// Process configs
|
|
38
|
+
let processorConfig: SentioProjectConfig = { host: '', project: '', build: true, debug: false }
|
|
39
|
+
// Fist step, read from project yaml file
|
|
40
|
+
try {
|
|
41
|
+
console.log(chalk.blue('Loading Process config'))
|
|
42
|
+
// TODO correctly located sentio.yaml
|
|
43
|
+
const pwd = process.cwd()
|
|
44
|
+
const packageJson = path.join(pwd, 'package.json')
|
|
45
|
+
if (!fs.existsSync(packageJson)) {
|
|
46
|
+
console.error('package.json not found, please run this command in the root of your project')
|
|
47
|
+
process.exit(1)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const yamlPath = path.join(pwd, 'sentio.yaml')
|
|
51
|
+
if (!fs.existsSync(yamlPath)) {
|
|
52
|
+
console.error('sentio.yaml not found, please create one according to: TODO docs')
|
|
53
|
+
process.exit(1)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
processorConfig = yaml.load(fs.readFileSync('sentio.yaml', 'utf8')) as SentioProjectConfig
|
|
57
|
+
if (!processorConfig.project === undefined) {
|
|
58
|
+
console.error('Config yaml must have contain a valid project identifier')
|
|
59
|
+
process.exit(1)
|
|
60
|
+
}
|
|
61
|
+
if (processorConfig.build === undefined) {
|
|
62
|
+
processorConfig.build = true
|
|
63
|
+
}
|
|
64
|
+
if (!processorConfig.host) {
|
|
65
|
+
processorConfig.host = 'prod'
|
|
66
|
+
}
|
|
67
|
+
if (processorConfig.debug === undefined) {
|
|
68
|
+
processorConfig.debug = false
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// if (!processorConfig.source) {
|
|
72
|
+
// processorConfig.source = 'src/processor.ts'
|
|
73
|
+
// }
|
|
74
|
+
// if (!processorConfig.targets) {
|
|
75
|
+
// console.warn('targets is not defined, use EVM as the default target')
|
|
76
|
+
// processorConfig.targets = []
|
|
77
|
+
// }
|
|
78
|
+
// if (processorConfig.targets.length === 0) {
|
|
79
|
+
// // By default evm
|
|
80
|
+
// processorConfig.targets.push({ chain: EVM })
|
|
81
|
+
// }
|
|
82
|
+
} catch (e) {
|
|
83
|
+
console.error(e)
|
|
84
|
+
process.exit(1)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (mainOptions.command === 'upload') {
|
|
88
|
+
const optionDefinitions = [
|
|
89
|
+
{
|
|
90
|
+
name: 'help',
|
|
91
|
+
alias: 'h',
|
|
92
|
+
type: Boolean,
|
|
93
|
+
description: 'Display this usage guide.',
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
name: 'api-key',
|
|
97
|
+
type: String,
|
|
98
|
+
description: '(Optional) Manually provide API key rather than use saved credential',
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
name: 'host',
|
|
102
|
+
description: '(Optional) Override Sentio Host name',
|
|
103
|
+
type: String,
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
name: 'owner',
|
|
107
|
+
description: '(Optional) Override Project owner',
|
|
108
|
+
type: String,
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: 'nobuild',
|
|
112
|
+
description: '(Optional) Skip build & pack file before uploading, default false',
|
|
113
|
+
type: Boolean,
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: 'debug',
|
|
117
|
+
description: '(Optional) Set driver logging level to debug',
|
|
118
|
+
type: Boolean,
|
|
119
|
+
},
|
|
120
|
+
]
|
|
121
|
+
const options = commandLineArgs(optionDefinitions, { argv })
|
|
122
|
+
if (options.help) {
|
|
123
|
+
const usage = commandLineUsage([
|
|
124
|
+
{
|
|
125
|
+
header: 'Sentio upload',
|
|
126
|
+
content: 'sentio upload',
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
header: 'Options',
|
|
130
|
+
optionList: optionDefinitions,
|
|
131
|
+
},
|
|
132
|
+
])
|
|
133
|
+
console.log(usage)
|
|
134
|
+
} else {
|
|
135
|
+
if (options.host) {
|
|
136
|
+
processorConfig.host = options.host
|
|
137
|
+
}
|
|
138
|
+
if (options.nobuild) {
|
|
139
|
+
processorConfig.build = false
|
|
140
|
+
}
|
|
141
|
+
if (options.debug) {
|
|
142
|
+
processorConfig.debug = true
|
|
143
|
+
}
|
|
144
|
+
finalizeHost(processorConfig)
|
|
145
|
+
FinalizeProjectName(processorConfig, options.owner)
|
|
146
|
+
console.log(processorConfig)
|
|
147
|
+
|
|
148
|
+
let apiOverride = undefined
|
|
149
|
+
if (options['api-key']) {
|
|
150
|
+
apiOverride = options['api-key']
|
|
151
|
+
}
|
|
152
|
+
uploadFile(processorConfig, apiOverride)
|
|
153
|
+
}
|
|
154
|
+
} else if (mainOptions.command === 'build') {
|
|
155
|
+
buildProcessor(false)
|
|
156
|
+
} else if (mainOptions.command === 'gen') {
|
|
157
|
+
buildProcessor(true)
|
|
158
|
+
} else {
|
|
159
|
+
usage()
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function usage() {
|
|
164
|
+
const usage = commandLineUsage([
|
|
165
|
+
{
|
|
166
|
+
header: 'Sentio',
|
|
167
|
+
content: 'Login & Manage your project files to Sentio.',
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
header: 'Usage',
|
|
171
|
+
content: [
|
|
172
|
+
'sentio <command> --help\t\tshow detail usage of specific command',
|
|
173
|
+
'sentio login\t\t\t\tlogin to sentio',
|
|
174
|
+
'sentio create\t\t\t\tcreate a template project',
|
|
175
|
+
'sentio upload\t\t\t\tbuild and upload processor to sentio',
|
|
176
|
+
'sentio gen\t\t\t\tgenerate abi',
|
|
177
|
+
'sentio build\t\t\t\tgenerate abi and build',
|
|
178
|
+
'sentio version\t\t\tcurrent cli version',
|
|
179
|
+
],
|
|
180
|
+
},
|
|
181
|
+
])
|
|
182
|
+
console.log(usage)
|
|
183
|
+
process.exit(1)
|
|
184
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import express from 'express'
|
|
2
|
+
import { getAuthConfig, getFinalizedHost } from '../config'
|
|
3
|
+
import url, { URL } from 'url'
|
|
4
|
+
import fetch from 'node-fetch'
|
|
5
|
+
import { getCliVersion } from '../utils'
|
|
6
|
+
import { WriteKey } from '../key'
|
|
7
|
+
import chalk from 'chalk'
|
|
8
|
+
import http from 'http'
|
|
9
|
+
import os from 'os'
|
|
10
|
+
import * as crypto from 'crypto'
|
|
11
|
+
|
|
12
|
+
interface AuthParams {
|
|
13
|
+
serverPort: number
|
|
14
|
+
sentioHost: string
|
|
15
|
+
codeVerifier: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const app = express()
|
|
19
|
+
|
|
20
|
+
let server: http.Server
|
|
21
|
+
let authParams: AuthParams
|
|
22
|
+
|
|
23
|
+
export function startServer(params: AuthParams) {
|
|
24
|
+
authParams = params
|
|
25
|
+
server = app.listen(params.serverPort)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
app.get('/callback', async (req, res) => {
|
|
29
|
+
res.end('login success, please go back to CLI to continue')
|
|
30
|
+
const host = getFinalizedHost(authParams.sentioHost)
|
|
31
|
+
const code = req.query.code
|
|
32
|
+
if (!code || (code as string).length == 0) {
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// exchange token
|
|
37
|
+
const tokenResRaw = await getToken(host, code as string)
|
|
38
|
+
if (!tokenResRaw.ok) {
|
|
39
|
+
console.error(chalk.red('request token error, code:', tokenResRaw.status, tokenResRaw.statusText))
|
|
40
|
+
return
|
|
41
|
+
}
|
|
42
|
+
const tokenRes = await tokenResRaw.json()
|
|
43
|
+
const accessToken = tokenRes['access_token']
|
|
44
|
+
|
|
45
|
+
// check if the account is ready
|
|
46
|
+
const userResRaw = await getUser(host, accessToken)
|
|
47
|
+
if (!userResRaw.ok) {
|
|
48
|
+
if (userResRaw.status == 401) {
|
|
49
|
+
console.error(chalk.red('please sign up on sentio first'))
|
|
50
|
+
} else {
|
|
51
|
+
console.error(chalk.red('get user error, code:', userResRaw.status, userResRaw.statusText))
|
|
52
|
+
}
|
|
53
|
+
return
|
|
54
|
+
}
|
|
55
|
+
const userRes = await userResRaw.json()
|
|
56
|
+
if (!userRes.emailVerified) {
|
|
57
|
+
console.error(chalk.red('please verify your email first'))
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// create API key
|
|
62
|
+
const apiKeyName = `${os.hostname()}-${crypto.randomBytes(4).toString('hex')}`
|
|
63
|
+
const createApiKeyResRaw = await createApiKey(host, apiKeyName, 'sdk_generated', accessToken)
|
|
64
|
+
if (!createApiKeyResRaw.ok) {
|
|
65
|
+
console.error(chalk.red('create api key error, code:', createApiKeyResRaw.status, createApiKeyResRaw.statusText))
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
const createApiKeyRes = await createApiKeyResRaw.json()
|
|
69
|
+
const apiKey = createApiKeyRes['key']
|
|
70
|
+
WriteKey(host, apiKey)
|
|
71
|
+
console.log(chalk.green('login success, new API key: ' + apiKey))
|
|
72
|
+
|
|
73
|
+
server.close()
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
async function getToken(host: string, code: string) {
|
|
77
|
+
const authConf = getAuthConfig(host)
|
|
78
|
+
const params = new url.URLSearchParams({
|
|
79
|
+
grant_type: 'authorization_code',
|
|
80
|
+
client_id: authConf.clientId,
|
|
81
|
+
code_verifier: authParams.codeVerifier,
|
|
82
|
+
code: code,
|
|
83
|
+
redirect_uri: `http://localhost:${authParams.serverPort}/callback`,
|
|
84
|
+
})
|
|
85
|
+
return fetch(`${authConf.domain}/oauth/token`, {
|
|
86
|
+
method: 'POST',
|
|
87
|
+
headers: {
|
|
88
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
89
|
+
},
|
|
90
|
+
body: params.toString(),
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function createApiKey(host: string, name: string, source: string, accessToken: string) {
|
|
95
|
+
const createApiKeyUrl = new URL('/api/v1/keys', host)
|
|
96
|
+
return fetch(createApiKeyUrl, {
|
|
97
|
+
method: 'POST',
|
|
98
|
+
headers: {
|
|
99
|
+
Authorization: 'Bearer ' + accessToken,
|
|
100
|
+
version: getCliVersion(),
|
|
101
|
+
},
|
|
102
|
+
body: JSON.stringify({
|
|
103
|
+
name: name,
|
|
104
|
+
scopes: ['write:project'],
|
|
105
|
+
source: source,
|
|
106
|
+
}),
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function getUser(host: string, accessToken: string) {
|
|
111
|
+
const getUserUrl = new URL('/api/v1/users', host)
|
|
112
|
+
return fetch(getUserUrl, {
|
|
113
|
+
method: 'GET',
|
|
114
|
+
headers: {
|
|
115
|
+
Authorization: 'Bearer ' + accessToken,
|
|
116
|
+
version: getCliVersion(),
|
|
117
|
+
},
|
|
118
|
+
})
|
|
119
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import commandLineArgs from 'command-line-args'
|
|
2
|
+
import commandLineUsage from 'command-line-usage'
|
|
3
|
+
import path from 'path'
|
|
4
|
+
import fs from 'fs-extra'
|
|
5
|
+
import chalk from 'chalk'
|
|
6
|
+
import { getCliVersion } from '../utils'
|
|
7
|
+
|
|
8
|
+
export function runCreate(argv: string[]) {
|
|
9
|
+
const optionDefinitions = [
|
|
10
|
+
{
|
|
11
|
+
name: 'help',
|
|
12
|
+
alias: 'h',
|
|
13
|
+
type: Boolean,
|
|
14
|
+
description: 'Display this usage guide.',
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
name: 'name',
|
|
18
|
+
alias: 'n',
|
|
19
|
+
defaultOption: true,
|
|
20
|
+
type: String,
|
|
21
|
+
description: 'Project name',
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
name: 'directory',
|
|
25
|
+
alias: 'd',
|
|
26
|
+
description: '(Optional) The root direct new project will be created, default current working dir',
|
|
27
|
+
type: String,
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
name: 'chain-type',
|
|
31
|
+
alias: 'c',
|
|
32
|
+
description:
|
|
33
|
+
'The type of project you want to create, can be evm, aptos, raw (if you want to start from scratch and support multiple types of chains)',
|
|
34
|
+
type: String,
|
|
35
|
+
defaultValue: 'evm',
|
|
36
|
+
},
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
const options = commandLineArgs(optionDefinitions, { argv })
|
|
40
|
+
const usage = commandLineUsage([
|
|
41
|
+
{
|
|
42
|
+
header: 'Create a template project',
|
|
43
|
+
content: 'sentio create <name>',
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
header: 'Options',
|
|
47
|
+
optionList: optionDefinitions,
|
|
48
|
+
},
|
|
49
|
+
])
|
|
50
|
+
|
|
51
|
+
if (options.help || !options.name) {
|
|
52
|
+
console.log(usage)
|
|
53
|
+
} else {
|
|
54
|
+
const chainType: string = options['chain-type'].toLowerCase()
|
|
55
|
+
switch (chainType) {
|
|
56
|
+
case 'evm':
|
|
57
|
+
break
|
|
58
|
+
case 'aptos':
|
|
59
|
+
break
|
|
60
|
+
case 'raw':
|
|
61
|
+
break
|
|
62
|
+
default:
|
|
63
|
+
console.error(chalk.red('non supported chain-type for template creation, use --help for more information.'))
|
|
64
|
+
console.log(usage)
|
|
65
|
+
process.exit(1)
|
|
66
|
+
}
|
|
67
|
+
const templateFolder = path.resolve(__dirname, '../../templates', chainType)
|
|
68
|
+
const projectName = options.name || 'default'
|
|
69
|
+
|
|
70
|
+
const rootDir = options.directory || process.cwd()
|
|
71
|
+
const dstFolder = path.resolve(rootDir, projectName)
|
|
72
|
+
if (fs.existsSync(dstFolder)) {
|
|
73
|
+
console.error(chalk.red("can't create project '" + projectName + "', directory already existed"))
|
|
74
|
+
process.exit(1)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
fs.copySync(templateFolder, dstFolder, {
|
|
78
|
+
filter: (src, _) => {
|
|
79
|
+
// TODO read from .gitignore to be more reliable
|
|
80
|
+
if (
|
|
81
|
+
src.endsWith('types') ||
|
|
82
|
+
src.endsWith('dist') ||
|
|
83
|
+
src.endsWith('node_modules') ||
|
|
84
|
+
src.endsWith('.lock') ||
|
|
85
|
+
src.endsWith('dist')
|
|
86
|
+
) {
|
|
87
|
+
return false
|
|
88
|
+
}
|
|
89
|
+
return true
|
|
90
|
+
},
|
|
91
|
+
overwrite: false,
|
|
92
|
+
})
|
|
93
|
+
if (options.name) {
|
|
94
|
+
const sentioYamlPath = path.resolve(dstFolder, 'sentio.yaml')
|
|
95
|
+
fs.writeFileSync(sentioYamlPath, 'project: ' + projectName + '\n', { flag: 'w+' })
|
|
96
|
+
|
|
97
|
+
const packageJsonPath = path.resolve(dstFolder, 'package.json')
|
|
98
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
|
|
99
|
+
|
|
100
|
+
let cliVersion = getCliVersion()
|
|
101
|
+
if (!cliVersion.startsWith('^')) {
|
|
102
|
+
cliVersion = '^' + cliVersion
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
packageJson.dependencies['@sentio/sdk'] = cliVersion
|
|
106
|
+
packageJson.dependencies['@sentio/cli'] = cliVersion
|
|
107
|
+
packageJson.name = projectName
|
|
108
|
+
|
|
109
|
+
// Don't add directly to avoid deps issue
|
|
110
|
+
packageJson.scripts.postinstall = 'sentio gen'
|
|
111
|
+
|
|
112
|
+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
|
|
113
|
+
}
|
|
114
|
+
console.log(chalk.green("successfully create project '" + projectName + "'"))
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import commandLineArgs from 'command-line-args'
|
|
2
|
+
import commandLineUsage from 'command-line-usage'
|
|
3
|
+
import { getAuthConfig, getFinalizedHost } from '../config'
|
|
4
|
+
import { startServer } from './login-server'
|
|
5
|
+
import url, { URL } from 'url'
|
|
6
|
+
import * as crypto from 'crypto'
|
|
7
|
+
import chalk from 'chalk'
|
|
8
|
+
import { WriteKey } from '../key'
|
|
9
|
+
import fetch from 'node-fetch'
|
|
10
|
+
import open from 'open'
|
|
11
|
+
|
|
12
|
+
const port = 20000
|
|
13
|
+
|
|
14
|
+
export function runLogin(argv: string[]) {
|
|
15
|
+
const optionDefinitions = [
|
|
16
|
+
{
|
|
17
|
+
name: 'help',
|
|
18
|
+
alias: 'h',
|
|
19
|
+
type: Boolean,
|
|
20
|
+
description: 'Display this usage guide.',
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: 'host',
|
|
24
|
+
description: '(Optional) Override Sentio Host name',
|
|
25
|
+
type: String,
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
name: 'api-key',
|
|
29
|
+
type: String,
|
|
30
|
+
description: '(Optional) Your API key',
|
|
31
|
+
},
|
|
32
|
+
]
|
|
33
|
+
const options = commandLineArgs(optionDefinitions, { argv })
|
|
34
|
+
|
|
35
|
+
const host = getFinalizedHost(options.host)
|
|
36
|
+
if (options.help) {
|
|
37
|
+
const usage = commandLineUsage([
|
|
38
|
+
{
|
|
39
|
+
header: 'Login to Sentio',
|
|
40
|
+
content: 'sentio login',
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
header: 'Options',
|
|
44
|
+
optionList: optionDefinitions,
|
|
45
|
+
},
|
|
46
|
+
])
|
|
47
|
+
console.log(usage)
|
|
48
|
+
} else if (options['api-key']) {
|
|
49
|
+
console.log(chalk.blue('login to ' + host))
|
|
50
|
+
const apiKey = options['api-key']
|
|
51
|
+
checkKey(host, apiKey).then((res) => {
|
|
52
|
+
if (res.status == 200) {
|
|
53
|
+
WriteKey(host, apiKey)
|
|
54
|
+
console.log(chalk.green('login success'))
|
|
55
|
+
} else {
|
|
56
|
+
console.error(chalk.red('login failed, code:', res.status, res.statusText))
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
} else {
|
|
60
|
+
// https://auth0.com/docs/get-started/authentication-and-authorization-flow/call-your-api-using-the-authorization-code-flow-with-pkce
|
|
61
|
+
const verifier = base64URLEncode(crypto.randomBytes(32))
|
|
62
|
+
const challenge = base64URLEncode(sha256(verifier))
|
|
63
|
+
|
|
64
|
+
const conf = getAuthConfig(host)
|
|
65
|
+
if (conf.domain === '') {
|
|
66
|
+
console.error(chalk.red('invalid host, try login with an API key if it is a dev env'))
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
const authURL = new URL(conf.domain + `/authorize?`)
|
|
70
|
+
const params = new url.URLSearchParams({
|
|
71
|
+
response_type: 'code',
|
|
72
|
+
code_challenge: challenge,
|
|
73
|
+
code_challenge_method: 'S256',
|
|
74
|
+
client_id: conf.clientId,
|
|
75
|
+
redirect_uri: `http://localhost:${port}/callback`,
|
|
76
|
+
audience: conf.audience,
|
|
77
|
+
prompt: 'login',
|
|
78
|
+
})
|
|
79
|
+
authURL.search = params.toString()
|
|
80
|
+
|
|
81
|
+
console.log('Continue your authorization in the browser')
|
|
82
|
+
open(authURL.toString()).catch((reason) => {
|
|
83
|
+
console.error(chalk.red('Unable to open browser: ' + reason))
|
|
84
|
+
console.error(chalk.red('Open this url in your browser: ' + authURL.toString()))
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
startServer({
|
|
88
|
+
serverPort: port,
|
|
89
|
+
sentioHost: options.host,
|
|
90
|
+
codeVerifier: verifier,
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function base64URLEncode(str: Buffer) {
|
|
96
|
+
return str.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function sha256(str: string) {
|
|
100
|
+
return crypto.createHash('sha256').update(str).digest()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async function checkKey(host: string, apiKey: string) {
|
|
104
|
+
const checkApiKeyUrl = new URL('/api/v1/processors/check_key', host)
|
|
105
|
+
return fetch(checkApiKeyUrl, {
|
|
106
|
+
method: 'GET',
|
|
107
|
+
headers: {
|
|
108
|
+
'api-key': apiKey,
|
|
109
|
+
},
|
|
110
|
+
})
|
|
111
|
+
}
|