Skip to content

Commit c1eea90

Browse files
gofumpt middleware.go
1 parent bbf9fe2 commit c1eea90

File tree

6 files changed

+226
-0
lines changed

6 files changed

+226
-0
lines changed

cli/src/commands/download.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { Command } from '@commander-js/extra-typings'
2+
import { JsonRpcPublicProvider, SmartContract } from '@massalabs/massa-web3'
3+
import { validateAddress } from './utils'
4+
import { getFileFromAddress, listFiles } from '../lib/website/read'
5+
import { dirname, join } from 'path'
6+
import { existsSync, mkdirSync, writeFileSync } from 'fs'
7+
8+
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
9+
10+
export const downloadCommand = new Command('download')
11+
.alias('dl')
12+
.description('Download files from the given website to the directory')
13+
.argument('<directory>', 'Directory to download files to')
14+
.option('-a, --address <address>', 'Address of the website to list')
15+
.action(async (directory, options, command) => {
16+
const globalOptions = command.optsWithGlobals()
17+
18+
const provider = JsonRpcPublicProvider.fromRPCUrl(
19+
globalOptions.node_url as string
20+
)
21+
22+
let sc: SmartContract
23+
if (options.address) {
24+
validateAddress(options.address)
25+
26+
sc = new SmartContract(provider, options.address)
27+
} else {
28+
console.log('No address provided')
29+
process.exit(1)
30+
}
31+
32+
console.log('Targeting website at address', sc.address)
33+
34+
const { files } = await listFiles(provider, sc.address)
35+
36+
console.log(`Total of ${files.length} files:`)
37+
for (const f of files.sort()) {
38+
console.log(`Downloading ${f}`)
39+
const content = await getFileFromAddress(provider, sc.address, f)
40+
41+
try {
42+
const filePath = join(directory, f)
43+
const dirPath = dirname(filePath)
44+
if (!existsSync(dirPath)) {
45+
mkdirSync(dirPath, { recursive: true })
46+
}
47+
writeFileSync(filePath, content)
48+
} catch (e) {
49+
console.error(`Failed to write file ${f}: ${e}`)
50+
}
51+
52+
await sleep(500) // 500ms delay between downloads
53+
}
54+
})

cli/src/commands/index.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { Command } from '@commander-js/extra-typings'
2+
import { JsonRpcPublicProvider, OperationStatus } from '@massalabs/massa-web3'
3+
4+
import { updateIndexScWebsite } from '../lib/index'
5+
import { getAddressWebsites, getWebsiteOwner } from '../lib/index/read'
6+
import { makeProviderFromNodeURLAndSecret, validateAddress } from './utils'
7+
8+
const indexUpdateCommand = new Command('update')
9+
.description(
10+
'Calls the DeWeb index smart contract to update a website in the index'
11+
)
12+
.option('-a, --address <address>', 'Address of the website to list')
13+
.action(async (options, command) => {
14+
const globalOptions = command.optsWithGlobals()
15+
16+
const provider = await makeProviderFromNodeURLAndSecret(globalOptions)
17+
18+
if (!options.address) {
19+
console.log('Please provide the address of the website to update')
20+
process.exit(1)
21+
}
22+
23+
validateAddress(options.address)
24+
25+
const tx = await updateIndexScWebsite(provider, options.address)
26+
27+
const status = await tx.waitFinalExecution()
28+
if (status === OperationStatus.Success) {
29+
console.log('Website updated successfully')
30+
console.log('Owner is:', await getWebsiteOwner(provider, options.address))
31+
} else {
32+
console.log('Something went wrong, Status is:', status)
33+
}
34+
})
35+
36+
const indexOwnerOfCommand = new Command('ownerOf')
37+
.description(
38+
'Calls the DeWeb index smart contract to get the owner of a website'
39+
)
40+
.option('-a, --address <address>', 'Address of the website to list')
41+
.action(async (options, command) => {
42+
const globalOptions = command.optsWithGlobals()
43+
44+
const provider = JsonRpcPublicProvider.fromRPCUrl(
45+
globalOptions.node_url as string
46+
)
47+
48+
if (!options.address) {
49+
console.log(
50+
'Please provide the address of the website to get the owner of'
51+
)
52+
process.exit(1)
53+
}
54+
55+
validateAddress(options.address)
56+
57+
await getWebsiteOwner(provider, options.address)
58+
.then((owner) => {
59+
console.log('Owner is:', owner)
60+
})
61+
.catch((e) => {
62+
console.log('Error:', e)
63+
})
64+
})
65+
66+
const indexAddressWebsitesCommand = new Command('addressWebsites')
67+
.description(
68+
'Calls the DeWeb index smart contract to get the websites owned by an address'
69+
)
70+
.option('-a, --address <address>', 'Address of the owner')
71+
.action(async (options, command) => {
72+
const globalOptions = command.optsWithGlobals()
73+
74+
const provider = JsonRpcPublicProvider.fromRPCUrl(
75+
globalOptions.node_url as string
76+
)
77+
78+
if (!options.address) {
79+
console.log('Please provide the address to get the websites of')
80+
process.exit(1)
81+
}
82+
83+
await getAddressWebsites(provider, options.address)
84+
.then((websites) => {
85+
console.log('Websites owned by', options.address)
86+
websites.forEach((w) => console.log(w))
87+
})
88+
.catch((e) => {
89+
console.log('Error:', e)
90+
})
91+
})
92+
93+
export const indexCommand = new Command('index')
94+
.addCommand(indexUpdateCommand)
95+
.addCommand(indexOwnerOfCommand)
96+
.addCommand(indexAddressWebsitesCommand)

cli/src/commands/mns.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Command } from '@commander-js/extra-typings'
2+
import { CHAIN_ID, JsonRpcPublicProvider, MNS } from '@massalabs/massa-web3'
3+
4+
export const resolveCommand = new Command('resolve')
5+
.description('Resolve MNS domain')
6+
.argument('<domain>', 'domain to resolve')
7+
.action(async (address, _, command) => {
8+
const globalOptions = command.optsWithGlobals()
9+
10+
const provider = JsonRpcPublicProvider.fromRPCUrl(
11+
globalOptions.node_url as string
12+
)
13+
14+
let mns: MNS
15+
switch (await (await provider.networkInfos()).chainId) {
16+
case CHAIN_ID.Buildnet:
17+
mns = MNS.buildnet(provider)
18+
break
19+
case CHAIN_ID.Mainnet:
20+
mns = MNS.mainnet(provider)
21+
break
22+
default:
23+
throw new Error('Unsupported chain')
24+
}
25+
26+
const resolved = await mns.resolve(address)
27+
console.log('Resolved:', resolved)
28+
})
29+
30+
export const mnsCommands = new Command('mns').addCommand(resolveCommand)

cli/src/lib/mns.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Args, CHAIN_ID, Provider } from '@massalabs/massa-web3'
2+
3+
const MAINNET_ADDRESS = 'AS1q5hUfxLXNXLKsYQVXZLK7MPUZcWaNZZsK7e9QzqhGdAgLpUGT'
4+
const BUILDNET_ADDRESS = 'AS12qKAVjU1nr66JSkQ6N4Lqu4iwuVc6rAbRTrxFoynPrPdP1sj3G'
5+
6+
const dnsResolveMethod = 'dnsResolve'
7+
8+
export async function resolveMNS(
9+
provider: Provider,
10+
domain: string
11+
): Promise<string> {
12+
// If ends with ".massa", remove it
13+
if (domain.endsWith('.massa')) {
14+
domain = domain.slice(0, -6)
15+
}
16+
17+
const network = (await provider.networkInfos()).chainId
18+
const contractAddress =
19+
CHAIN_ID.Mainnet === network
20+
? MAINNET_ADDRESS
21+
: CHAIN_ID.Buildnet === network
22+
? BUILDNET_ADDRESS
23+
: ''
24+
if (contractAddress === '') {
25+
throw new Error('Unsupported network')
26+
}
27+
28+
const args = new Args().addString(domain)
29+
30+
const op = await provider.readSC({
31+
target: contractAddress,
32+
func: dnsResolveMethod,
33+
parameter: args,
34+
})
35+
36+
if (op.info.error) {
37+
throw new Error(op.info.error)
38+
}
39+
40+
// convert op.value byte array to string
41+
const result = new TextDecoder().decode(op.value)
42+
43+
return result
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"root":["./src/main.tsx","./src/vite-env.d.ts","./src/colors/colors.ts","./src/components/rootapp.tsx","./src/hooks/usegeneratetheme.tsx","./src/pages/devhomepage.tsx","./src/pages/brokenwebsite/broken.tsx","./src/pages/brokenwebsite/index.tsx","./src/pages/domainnotfound/domainnotfound.tsx","./src/pages/domainnotfound/index.tsx","./src/pages/home/home.tsx","./src/pages/home/index.tsx","./src/pages/notavailable/index.tsx","./src/pages/notavailable/notavailable.tsx","./src/utils/rootapp.tsx","./src/utils/consts.ts"],"version":"5.8.2"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"root":["./vite.config.ts"],"version":"5.8.2"}

0 commit comments

Comments
 (0)