|
1 | | -import { PublicKey } from '@solana/web3.js' |
| 1 | +import { Keypair, PublicKey } from '@solana/web3.js' |
2 | 2 | import { readAllFromRPC } from '@staratlas/data-source' |
3 | 3 | import { PlayerProfile } from '@staratlas/player-profile' |
4 | 4 | import { CronJob } from 'cron' |
| 5 | +import * as fs from 'node:fs' |
| 6 | +import path from 'node:path' |
5 | 7 |
|
6 | 8 | import { config } from '../../config/index.js' |
7 | 9 | import { airdrop } from '../../lib/airdrop.js' |
8 | 10 | import { logger } from '../../logger.js' |
9 | 11 | import { connection } from '../../service/sol/index.js' |
| 12 | +import { loadKeypairFromFile } from '../../service/wallet' |
10 | 13 | import { programs } from '../basedbot/lib/programs.js' |
11 | 14 | import { sageGame } from '../basedbot/lib/sage/state/game.js' |
12 | | -import { Faction } from '../basedbot/lib/util/galaxy-sectors-data.js' |
13 | | -import { createAndInitializeCharacter } from '../basedbot/lib/util/profile.js' |
| 15 | +import { Faction } from '../basedbot/lib/util/galaxy-sectors-data' |
| 16 | +import { createAndInitializeCharacter } from '../basedbot/lib/util/profile' |
14 | 17 |
|
15 | 18 | let airdropCronJob: CronJob | undefined |
16 | 19 |
|
| 20 | +const findKeyFiles = (dirPath: string): Map<string, Keypair> => { |
| 21 | + let keyMap = new Map<string, Keypair>() |
| 22 | + if (!fs.existsSync(dirPath)) { |
| 23 | + logger.warn(`${dirPath} does not exist`) |
| 24 | + return keyMap |
| 25 | + } |
| 26 | + |
| 27 | + try { |
| 28 | + const entries = fs.readdirSync(dirPath, { withFileTypes: true }) |
| 29 | + |
| 30 | + for (const entry of entries) { |
| 31 | + const fullPath = path.join(dirPath, entry.name) |
| 32 | + |
| 33 | + if (entry.isDirectory()) { |
| 34 | + keyMap = new Map([...keyMap, ...findKeyFiles(fullPath)]) |
| 35 | + } else if (entry.isFile() && entry.name.endsWith('.json')) { |
| 36 | + try { |
| 37 | + const keyPair = loadKeypairFromFile(fullPath) |
| 38 | + keyMap.set(keyPair.publicKey.toBase58(), keyPair) |
| 39 | + } catch { |
| 40 | + logger.warn(`${entry.name} is not a Key`) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } catch (error) { |
| 45 | + console.error(`Error reading directory: ${dirPath}`, error) |
| 46 | + } |
| 47 | + |
| 48 | + return keyMap |
| 49 | +} |
| 50 | + |
17 | 51 | export const create = async (): Promise<void> => { |
18 | 52 | logger.info('Starting airdrop...') |
19 | 53 | } |
@@ -57,7 +91,25 @@ const airdropOrCreateProfile = async (user: PublicKey): Promise<void> => { |
57 | 91 | ) |
58 | 92 | } else { |
59 | 93 | logger.info(`Creating profile for ${user.toBase58()}`) |
60 | | - await createAndInitializeCharacter(game, 'fleetbot', Faction.ONI) |
| 94 | + const keyDir = process.env.KEY_DIR || '/tmp/keys' |
| 95 | + logger.info(`Looking for keys in ${path.dirname(keyDir)}`) |
| 96 | + const keys = findKeyFiles(keyDir) |
| 97 | + |
| 98 | + const keyPair = keys.get(user.toBase58()) |
| 99 | + |
| 100 | + if (keyPair) { |
| 101 | + const pubStr = keyPair.publicKey.toBase58() |
| 102 | + await createAndInitializeCharacter( |
| 103 | + game, |
| 104 | + `${pubStr.slice(0, 4)}...${pubStr.slice(-4)}`, |
| 105 | + Faction.ONI, |
| 106 | + keyPair, |
| 107 | + ) |
| 108 | + } else { |
| 109 | + logger.warn( |
| 110 | + `No key found for ${user.toBase58()}, cannot create profile`, |
| 111 | + ) |
| 112 | + } |
61 | 113 | } |
62 | 114 | } |
63 | 115 |
|
|
0 commit comments