Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
martykan committed Apr 22, 2024
1 parent a9a09f1 commit 5986703
Show file tree
Hide file tree
Showing 68 changed files with 2,939 additions and 1 deletion.
140 changes: 140 additions & 0 deletions packages/connect-explorer-nextra/scripts/add-legacy-methods.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';

import oldMenu from '@trezor/connect-explorer/src/data/menu';

Check failure on line 5 in packages/connect-explorer-nextra/scripts/add-legacy-methods.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

'@trezor/connect-explorer' should be listed in the project's dependencies. Run 'npm i -S @trezor/connect-explorer' to add it

function firstLetterToUpperCase(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1);
}

function findNameInMenu(url: string) {
let found = null;
oldMenu.forEach(section => {
section.children.forEach(method => {
if (method.url === url) {
found = method.name;
}
if (method.children) {
method.children.forEach(subMethod => {
if (subMethod.url === url) {
found = subMethod.name;
}
});
}
});
});

if (!found) {
console.error('Method not found in the menu:', url);

return null;
}

return firstLetterToUpperCase(found);
}

function findFilesForMethod(method: string) {
const output: any[] = [];

const filesData = fs.readdirSync(
path.join(__dirname, '../../connect-explorer/src/data/methods'),
);
filesData.forEach(dir => {
if (dir.includes('.')) return;
const methods = fs.readdirSync(
path.join(__dirname, `../../connect-explorer/src/data/methods/${dir}`),
);
methods.forEach(file => {
if (!file.endsWith('.ts')) return;
const content = fs.readFileSync(
path.join(__dirname, `../../connect-explorer/src/data/methods/${dir}/${file}`),
'utf-8',
);
if (content.includes(`name = '${method}'`) || content.includes(`name: '${method}'`)) {
console.log(content);
const methods = [...content.matchAll(/url: '(.*)'/g)].map((match, i) => ({
url: match[1],
title: findNameInMenu(match[1]),
dir,
file,
cleanfile: file.replace('.ts', '').replace('.', '_').replace('-', '_'),
i,
}));
output.push(...methods);
}
});
});

return output;
}

function handleMethod(filePath: string) {
const content = fs.readFileSync(
path.join(__dirname, `../src/pages/methods/${filePath}`),
'utf-8',
);
if (!content.includes('ApiPlayground') || content.includes('No payload')) return;

const method = content.match(/method: '(.*)'/)?.[1];
console.log(filePath, method);

if (!method) return;

const relevantFiles = findFilesForMethod(method);

const newImports = new Set(
relevantFiles.map(
file =>
`import ${file.cleanfile} from '../../../data/methods/${file.dir}/${file.file}';`,
),
);
const newImportsStr = [...newImports].join('\n');
console.log(newImportsStr);
const newOptions = relevantFiles
.map(
file =>
`\n { title: '${file.title}', legacyConfig: ${file.cleanfile}[${file.i}] },`,
)
.join('');
console.log(newOptions);
console.log(content.match(/options\=\{\[/gi));

let newContent = content
.replace(/options\=\{\[/gi, 'options={[' + newOptions)
.replace(/\<ApiPlayground/gi, newImportsStr + '\n\n<ApiPlayground')
.replace(/title: 'Bundle([^\)]*)',\s+method:/, "title: 'Advanced bundle', method:")
.replace(/title: '([^']*)',\s+method:/, "title: 'Advanced schema', method:");
console.log(newContent);

const outputPath = path.join(__dirname, `../src/pages/methods/${filePath}`);
fs.writeFileSync(outputPath, newContent);

// Format the file and check with eslint
try {
execSync(`yarn g:eslint --fix ${outputPath}`);

Check warning

Code scanning / CodeQL

Shell command built from environment values Medium

This shell command depends on an uncontrolled
file name
.
This shell command depends on an uncontrolled
file name
.
execSync(`yarn g:prettier --write ${outputPath}`);

Check warning

Code scanning / CodeQL

Shell command built from environment values Medium

This shell command depends on an uncontrolled
file name
.
This shell command depends on an uncontrolled
file name
.
} catch (error) {
console.error('Error while formatting the file:', error.message);
console.error(error.output?.[1]?.toString());
}
}

function convertAll() {
const methodTypes = fs.readdirSync(path.join(__dirname, '../src/pages/methods'));

methodTypes.forEach(methodType => {
const files = fs.readdirSync(path.join(__dirname, `../src/pages/methods/${methodType}`));
files.forEach(file => {
if (!file.endsWith('.mdx')) return;
handleMethod(path.join(methodType, file));
});
});
}

const arg = process.argv[2];
if (arg === '--all') {
convertAll();
} else {
handleMethod(arg);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const name = 'binanceGetAddress';
const docs = 'methods/binanceGetAddress.md';
const batch = [
{
name: 'path',
label: 'Bip44 path',
type: 'input',
value: `m/44'/714'/0'/0/0`,
},
{
name: 'showOnTrezor',
label: 'Show on Trezor',
type: 'checkbox',
value: true,
},
{
name: 'chunkify',
label: 'Display address in chunks of 4 characters',
type: 'checkbox',
value: false,
},
];

export default [
{
url: '/method/binanceGetAddress',
name,
docs,
submitButton: 'Get address',

fields: batch,
},

{
url: '/method/binanceGetAddress-multiple',
name,
docs,
submitButton: 'Get multiple addresses',

fields: [
{
name: 'bundle',
type: 'array',
batch: [
{
type: 'doesnt-matter',
fields: batch,
},
],
items: [batch, batch],
},
],
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const name = 'binanceGetPublicKey';
const docs = 'methods/binanceGetPublicKey.md';
const batch = [
{
name: 'path',
label: 'Bip44 path',
type: 'input',
value: `m/44'/714'/0'/0/0`,
},
{
name: 'showOnTrezor',
label: 'Show on Trezor',
type: 'checkbox',
value: true,
},
];

export default [
{
url: '/method/binanceGetPublicKey',
name,
docs,
submitButton: 'Get public key',

fields: batch,
},

{
url: '/method/binanceGetPublicKey-multiple',
name,
docs,
submitButton: 'Get multiple public keys',

fields: [
{
name: 'bundle',
type: 'array',
batch: [
{
type: '',
fields: batch,
},
],
items: [batch, batch],
},
],
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import getPublicKey from './getPublicKey';
import getAddress from './getAddress';
import signTransaction from './signTransaction';

export default [...getPublicKey, ...getAddress, ...signTransaction];
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
const name = 'binanceSignTransaction';
const docs = 'methods/binanceSignTransaction.md';
const transfer = {
chain_id: 'Binance-Chain-Nile',
account_number: 34,
memo: 'test',
sequence: 31,
source: 1,
transfer: {
inputs: [
{
address: 'tbnb1hgm0p7khfk85zpz5v0j8wnej3a90w709zzlffd',
coins: [{ amount: 1000000000, denom: 'BNB' }],
},
],
outputs: [
{
address: 'tbnb1ss57e8sa7xnwq030k2ctr775uac9gjzglqhvpy',
coins: [{ amount: 1000000000, denom: 'BNB' }],
},
],
},
};

const placeOrder = {
chain_id: 'Binance-Chain-Nile',
account_number: 34,
memo: '',
sequence: 32,
source: 1,
placeOrder: {
id: 'BA36F0FAD74D8F41045463E4774F328F4AF779E5-33',
ordertype: 2,
price: 100000000,
quantity: 100000000,
sender: 'tbnb1hgm0p7khfk85zpz5v0j8wnej3a90w709zzlffd',
side: 1,
symbol: 'ADA.B-B63_BNB',
timeinforce: 1,
},
};

const cancelOrder = {
chain_id: 'Binance-Chain-Nile',
account_number: 34,
memo: '',
sequence: 33,
source: 1,
cancelOrder: {
refid: 'BA36F0FAD74D8F41045463E4774F328F4AF779E5-29',
sender: 'tbnb1hgm0p7khfk85zpz5v0j8wnej3a90w709zzlffd',
symbol: 'BCHSV.B-10F_BNB',
},
};

export default [
{
url: '/method/binanceSignTransaction-transfer',
name,
docs,
submitButton: 'Sign transaction',
fields: [
{
name: 'path',
label: 'Bip44 path',
type: 'input',
value: `m/44'/714'/0'/0/0`,
},
{
name: 'transaction',
type: 'json',
value: transfer,
},
{
name: 'chunkify',
label: 'Display recipient address in chunks of 4 characters',
type: 'checkbox',
value: false,
},
],
},
{
url: '/method/binanceSignTransaction-placeorder',
name,
docs,
submitButton: 'Sign transaction',
fields: [
{
name: 'path',
label: 'Bip44 path',
type: 'input',
value: `m/44'/714'/0'/0/0`,
},
{
name: 'transaction',
type: 'json',
value: placeOrder,
},
{
name: 'chunkify',
label: 'Display recipient address in chunks of 4 characters',
type: 'checkbox',
value: false,
},
],
},
{
url: '/method/binanceSignTransaction-cancelorder',
name,
docs,
submitButton: 'Sign transaction',
fields: [
{
name: 'path',
label: 'Bip44 path',
type: 'input',
value: `m/44'/714'/0'/0/0`,
},
{
name: 'transaction',
type: 'json',
value: cancelOrder,
},
{
name: 'chunkify',
label: 'Display recipient address in chunks of 4 characters',
type: 'checkbox',
value: false,
},
],
},
];
Loading

0 comments on commit 5986703

Please sign in to comment.