Skip to content

Commit 5b78148

Browse files
committed
fix: if can't resolve character path write seed to gitignored characters/file
1 parent 4b86222 commit 5b78148

File tree

1 file changed

+84
-5
lines changed

1 file changed

+84
-5
lines changed

packages/plugin-coinbase/src/plugins/massPayments.ts

+84-5
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,33 @@ async function initializeWallet(runtime: IAgentRuntime, networkId: string) {
122122
// Export wallet data directly
123123
const walletData: WalletData = wallet.export();
124124
const walletAddress = await wallet.getDefaultAddress();
125-
runtime.character.settings.secrets.COINBASE_GENERATED_WALLET_HEX_SEED =
126-
walletData.seed;
127-
runtime.character.settings.secrets.COINBASE_GENERATED_WALLET_ID =
128-
walletData.walletId;
125+
try {
126+
const characterFilePath = `characters/${runtime.character.name.toLowerCase()}.character.json`;
127+
const walletIDSave = await updateCharacterSecrets(
128+
characterFilePath,
129+
"COINBASE_GENERATED_WALLET_ID",
130+
walletData.walletId
131+
);
132+
const seedSave = await updateCharacterSecrets(
133+
characterFilePath,
134+
"COINBASE_GENERATED_WALLET_HEX_SEED",
135+
walletData.seed
136+
);
137+
if (walletIDSave && seedSave) {
138+
elizaLogger.log("Successfully updated character secrets.");
139+
} else {
140+
const seedFilePath = `characters/${runtime.character.name.toLowerCase()}-seed.txt`;
141+
elizaLogger.error(
142+
`Failed to update character secrets so adding gitignored ${seedFilePath} file please add it your env or character file and delete:`
143+
);
144+
// save it to gitignored file
145+
wallet.saveSeed(seedFilePath);
146+
}
147+
} catch (error) {
148+
elizaLogger.error("Error updating character secrets:", error);
149+
throw error;
150+
}
151+
129152
// Logging wallet creation
130153
elizaLogger.log("Created and stored new wallet:", walletAddress);
131154
} else {
@@ -138,7 +161,7 @@ async function initializeWallet(runtime: IAgentRuntime, networkId: string) {
138161
// Logging wallet import
139162
elizaLogger.log(
140163
"Imported existing wallet:",
141-
wallet.getDefaultAddress()
164+
await wallet.getDefaultAddress()
142165
);
143166
}
144167

@@ -438,3 +461,59 @@ export const coinbaseMassPaymentsPlugin: Plugin = {
438461
actions: [sendMassPayoutAction],
439462
providers: [massPayoutProvider],
440463
};
464+
465+
/**
466+
* Updates a key-value pair in character.settings.secrets.
467+
* @param {string} characterfilePath - The file path to the character.
468+
* @param {string} key - The secret key to update or add.
469+
* @param {string} value - The new value for the secret key.
470+
*/
471+
export async function updateCharacterSecrets(
472+
characterfilePath: string,
473+
key: string,
474+
value: string
475+
): Promise<boolean> {
476+
try {
477+
const characterFilePath = path.resolve(
478+
process.cwd(),
479+
characterfilePath
480+
);
481+
482+
// Check if the character file exists
483+
if (!fs.existsSync(characterFilePath)) {
484+
elizaLogger.error("Character file not found:", characterFilePath);
485+
return false;
486+
}
487+
488+
// Read the existing character file
489+
const characterData = JSON.parse(
490+
fs.readFileSync(characterFilePath, "utf-8")
491+
);
492+
493+
// Ensure settings and secrets exist in the character file
494+
if (!characterData.settings) {
495+
characterData.settings = {};
496+
}
497+
if (!characterData.settings.secrets) {
498+
characterData.settings.secrets = {};
499+
}
500+
501+
// Update or add the key-value pair
502+
characterData.settings.secrets[key] = value;
503+
504+
// Write the updated data back to the file
505+
fs.writeFileSync(
506+
characterFilePath,
507+
JSON.stringify(characterData, null, 2),
508+
"utf-8"
509+
);
510+
511+
console.log(
512+
`Updated ${key} in character.settings.secrets for ${characterFilePath}.`
513+
);
514+
} catch (error) {
515+
elizaLogger.error("Error updating character secrets:", error);
516+
return false;
517+
}
518+
return true;
519+
}

0 commit comments

Comments
 (0)