This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodefetch3_nodb.js
148 lines (111 loc) · 5.97 KB
/
nodefetch3_nodb.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const fetch = require('node-fetch');
const utils = require('ethers').utils;
const sha256 = require('js-sha256');
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'us-east-1'});
// Create an SQS service object
var sqs = new AWS.SQS({apiVersion: '2012-11-05'});
const IPFS = require('ipfs');
const fs = require('file-system');
const node = new IPFS();
try{
node.on('ready', async () => {
const version = await node.version()
console.log("node ready")
//arn:aws:lambda:us-east-1:711302153787:function:luxarity-lambda-sensui-csi-develop-soldOrderToMint
//684348276847
//684348276847
//https://00000000000000000000000000000000:[email protected]/admin/orders.json?since_id=670021091439
var orderjson = await fetch('https://00000000000000000000000000000000:[email protected]/admin/orders.json?ids=693655208047')
.then(res => res.json())
console.log("calling jsonloop ")
//jsonLoop(json);
for(var i = 0; i < orderjson.orders.length; i++){
console.log("###LOOP NUMBER "+i+"###")
console.log("orderid: "+orderjson.orders[i].id)
if(!(orderjson.orders[i].id && orderjson.orders[i].total_price && orderjson.orders[i].order_number && orderjson.orders[i].customer)){
//place holder for std out - possibly dlq
console.log("order missing attributes: "+orderjson.orders[i].id+" loop number: "+i)
}else{
console.log("sending to ipfs")
let orderMetaData = {
"buyerId": orderjson.orders[i].customer.email,
"orderId": orderjson.orders[i].id,
"orderAmount": orderjson.orders[i].total_price,
"vendor": 'Luxarity',
"dateOfSale": new Date(),
};
let orderMdString = JSON.stringify(orderMetaData);
//let mdHash = sha256(orderMdString);
let mdHash = utils.solidityKeccak256(['string'], [orderMdString])
//fs.writeFile(mdHash + ".json", orderMdString);
console.log("Waiting for IPFS URL to be generated.");
//setting up IPFS node
let ipfsHash;
let ipfsURL;
//////////////
console.log('Version:', version.version);
const filesAdded = await node.files.add({
path: mdHash + ".json",
content: Buffer.from(orderMdString)
});
console.log('Added file:', filesAdded[0].path, filesAdded[0].hash);
ipfsHash = filesAdded[0].hash;
//const fileBuffer = await node.files.cat(filesAdded[0].hash);
//console.log('Added file contents:', fileBuffer.toString());
//let ipfsURL = "https://ipfs.io/ipfs/" + ipfsHash;
ipfsURL = "https://ipfs.io/ipfs/" + ipfsHash;
console.log("ipfsURL: "+ipfsURL)
//return ipfsURL;
var redemptionPin = orderjson.orders[i].id.toString()+orderjson.orders[i].order_number.toString();
//console.log("remptionPin: "+redemptionPin);
//var redemptionPin256 = utils.solidityKeccak256(['string'], [redemptionPin])
var redemptionPin256reg = sha256(redemptionPin)
var redemptionPin256 = redemptionPin256reg.toUpperCase()
//var customerEmail256 = utils.solidityKeccak256(['string'], [orderjson.orders[i].customer.email])
var customerEmail256reg = sha256(orderjson.orders[i].customer.email)
var customerEmail256 = customerEmail256reg.toUpperCase()
console.log("sending sqs for: "+orderjson.orders[i].id)
//console.log("order total_price: "+)
await sendsqs(ipfsURL, orderjson.orders[i].id, orderjson.orders[i].total_price, orderjson.orders[i].order_number, customerEmail256, orderjson.orders[i].customer.email, redemptionPin256);
console.log("done");
/////////////
}
}
node.stop(() => {
// node is now 'offline'
console.log("stopping node")
})
})
}catch(err){console.log("ipfs on ready error: "+err)}
async function sendsqs(tokenUri, orderid, total_price, order_number, customer_email_sha256, customerEmail, redemption_pin_sha256){
var params = {
DelaySeconds: 10,
MessageAttributes: {
"LuxarityOrder": {
DataType: "String",
StringValue: "Individual Lux Order for SoldOrderToMint endpoint"
}
},
//MessageBody: "{ \"orderid\" : \""+orderid+"\" , \"total_price\" : \""+total_price+"\" , \"order_number\" : \""+order_number+"\" , \"customer_email\" : \""+customer_email+"\"}",
MessageBody: "{ \"tokenURI\" : \""+tokenUri+"\", \"totalPrice\" : "+total_price+", \"customerEmailSHA256\" : \""+customer_email_sha256+"\", \"customerEmail\" : \""+customerEmail+"\" , \"orderId\" : "+orderid+", \"orderNumber\" : "+order_number+", \"redemptionPinSHA256\" : \""+redemption_pin_sha256+"\", \"blockchain\" : \"Rinkeby\" }",
QueueUrl: "https://sqs.us-east-1.amazonaws.com/711302153787/luxarity-orders"
//QueueUrl: "https://sqs.us-east-1.amazonaws.com/711302153787/lux-ebs-test"
//QueueUrl: "https://sqs.us-east-1.amazonaws.com/711302153787/luxarity-orders"
//QueueUrl: "https://sqs.us-east-1.amazonaws.com/711302153787/lux-ebs-test"
//QueueUrl: "https://sqs.us-east-1.amazonaws.com/711302153787/luxarity-orders"
//QueueUrl: "https://sqs.us-east-1.amazonaws.com/711302153787/lux-ebs-test"
//QueueUrl: "https://sqs.us-east-1.amazonaws.com/711302153787/luxarity-orders"
//QueueUrl: "https://sqs.us-west-1.amazonaws.com/711302153787/SQS_QUEUE_NAME"
};
console.log("message body: "+params.MessageBody)
await sqs.sendMessage(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.MessageId);
}
});
}