-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts.js
66 lines (51 loc) · 1.36 KB
/
accounts.js
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
const aws = require('aws-sdk');
const dynamoDBClient = new aws.DynamoDB.DocumentClient();
const table = process.env.DYNAMO_DB_TABLE_NAME;
const { getLogger } = require('./logger');
const logger = getLogger('ROSSIGNOL');
const {
CryptoUtils: {
generatePrivateKey, publicKeyFromPrivateKey, Uint8ArrayToB64,
},
LocalAddress: {
fromPublicKey,
},
} = require('loom-js');
const buildAccount = () => {
const privateKey = generatePrivateKey();
const publicKey = publicKeyFromPrivateKey(privateKey);
const address = fromPublicKey(publicKey).toString();
return {
private_key: Uint8ArrayToB64(privateKey),
public_key: Uint8ArrayToB64(publicKey),
address,
};
};
const saveAccount = async (account) => {
const params = {
TableName: table,
Item: account,
};
await dynamoDBClient.put(params).promise();
};
const createAccount = async () => {
const account = buildAccount();
await saveAccount(account);
logger.info(`Created new sidechain account at address ${account.address}`);
return account;
};
const getAccount = async (address) => {
const params = {
TableName: table,
Key: {
address,
},
};
const { Item: result } = await dynamoDBClient.get(params).promise();
logger.info(`Retrieved sidechain account data for address ${address}`);
return result;
};
module.exports = {
createAccount,
getAccount,
};