-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-api-keys.js
More file actions
106 lines (87 loc) · 3.16 KB
/
setup-api-keys.js
File metadata and controls
106 lines (87 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Script to set up API keys from environment variables
// This script creates a test user and stores the COINBASE_API_KEY and COINBASE_API_SECRET
// from environment variables as an active API key for that user
import pg from 'pg';
import crypto from 'crypto';
const { Pool } = pg;
// Connect to the database
const pool = new Pool({
connectionString: process.env.DATABASE_URL
});
// Hash a password
async function hashPassword(password) {
return new Promise((resolve, reject) => {
const salt = crypto.randomBytes(16).toString('hex');
crypto.scrypt(password, salt, 64, (err, derivedKey) => {
if (err) reject(err);
resolve(`${derivedKey.toString('hex')}.${salt}`);
});
});
}
async function main() {
try {
// Check for required environment variables
const apiKey = process.env.COINBASE_API_KEY;
const apiSecret = process.env.COINBASE_API_SECRET;
if (!apiKey || !apiSecret) {
console.error('Error: COINBASE_API_KEY and COINBASE_API_SECRET environment variables must be set');
process.exit(1);
}
console.log('Setting up API key vault with provided credentials...');
// Create a test user
const hashedPassword = await hashPassword('testuser123');
// Check if test user already exists
const userResult = await pool.query(
'SELECT id FROM users WHERE username = $1',
['testuser']
);
let userId;
if (userResult.rows.length === 0) {
// Create the user
const insertUserResult = await pool.query(
'INSERT INTO users (username, password) VALUES ($1, $2) RETURNING id',
['testuser', hashedPassword]
);
userId = insertUserResult.rows[0].id;
console.log(`Created test user with ID ${userId}`);
} else {
userId = userResult.rows[0].id;
console.log(`Using existing test user with ID ${userId}`);
}
// Check if API key already exists for this user
const keyResult = await pool.query(
'SELECT id FROM api_keys WHERE user_id = $1 AND api_key = $2',
[userId, apiKey]
);
if (keyResult.rows.length === 0) {
// Insert the API key
const now = new Date();
const insertKeyResult = await pool.query(
`INSERT INTO api_keys
(user_id, api_key, api_secret, is_active, label, priority, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id`,
[userId, apiKey, apiSecret, true, 'Coinbase API Key', 10, now]
);
const keyId = insertKeyResult.rows[0].id;
console.log(`Added API key with ID ${keyId} for user ${userId}`);
} else {
const keyId = keyResult.rows[0].id;
// Update the existing key
await pool.query(
`UPDATE api_keys
SET api_secret = $1, is_active = true, priority = 10
WHERE id = $2`,
[apiSecret, keyId]
);
console.log(`Updated existing API key with ID ${keyId} for user ${userId}`);
}
console.log('API keys set up successfully! You can now use the application.');
} catch (error) {
console.error('Error setting up API keys:', error);
process.exit(1);
} finally {
await pool.end();
}
}
main();