-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
68 changed files
with
2,939 additions
and
1 deletion.
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
packages/connect-explorer-nextra/scripts/add-legacy-methods.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
||
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 Error loading related location Loading This shell command depends on an uncontrolled file name Error loading related location Loading |
||
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 Error loading related location Loading This shell command depends on an uncontrolled file name Error loading related location Loading |
||
} 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); | ||
} |
54 changes: 54 additions & 0 deletions
54
packages/connect-explorer-nextra/src/data/methods/binance/getAddress.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}, | ||
], | ||
}, | ||
]; |
48 changes: 48 additions & 0 deletions
48
packages/connect-explorer-nextra/src/data/methods/binance/getPublicKey.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}, | ||
], | ||
}, | ||
]; |
5 changes: 5 additions & 0 deletions
5
packages/connect-explorer-nextra/src/data/methods/binance/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
132 changes: 132 additions & 0 deletions
132
packages/connect-explorer-nextra/src/data/methods/binance/signTransaction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
], | ||
}, | ||
]; |
Oops, something went wrong.