Skip to content

Commit 25a58af

Browse files
committed
first commit
0 parents  commit 25a58af

File tree

6 files changed

+2607
-0
lines changed

6 files changed

+2607
-0
lines changed

.gitignore

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
6+
# testing
7+
/coverage
8+
9+
# production
10+
/build
11+
12+
# misc
13+
.DS_Store
14+
.env.local
15+
.env.development.local
16+
.env.test.local
17+
.env.production.local
18+
.env
19+
20+
npm-debug.log*
21+
yarn-debug.log*
22+
yarn-error.log*
23+
package-lock.json

README.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Introduction
2+
3+
This just a POC project for Online game store. Steam game store concept build on NEM blockchain, which mean user allow to using web applicaiton to purchase game (activation code) from the store.
4+
5+
NEM has powerful messaging feature, by using messaging feature, it allow user to purchase a game and received actication code from message in NEM blockchain.
6+
7+
NEM Blockchain is API driven, and with super easy to use SDK, it's easy to build an applicaiton on NEM Blockchain.
8+
9+
This project is build on [React App](https://github.com/facebook/create-react-app) with [material-ui theme](https://github.com/mui-org/material-ui) and using [NEM Library SDK](https://github.com/aleixmorgadas/nem-library-ts).
10+
11+
Server is build on [Nodejs](https://nodejs.org/en/docs/guides/getting-started-guide/).
12+
13+
## NEM-Steam-Store
14+
15+
[NEM-Steam-Store](https://github.com/AnthonyLaw/nem-steam-store) is a web base application, it allow user to purchase a game from the store. In the applicaiton you will received NEM's encrypted message after purchased the game from store. But in application it will decrypted the message and show activation code.
16+
Tx example: [Purchase game](http://bob.nem.ninja:8765/#/transfer/ad7de767f87b15feb4de3fc5587227a07aa7d5b77f1efd5bbd864c7804df17c0)
17+
18+
## NEM-Steam-Store-Server
19+
20+
[NEM-Steam-Store-Server](https://github.com/AnthonyLaw/nem-steam-store-server) is host with Store account, which is listening incoming transaction from the account address. When received fund from buyer, the server will trigger a NEM's encrypted message which content of activaiton code to the buyer account address.
21+
Tx example: [Invoice for the game (encrypted)](http://bob.nem.ninja:8765/#/transfer/455380757e87c7245f2b20543c27330de1039f6e977c0ddd7d6bd6f655763b2d).
22+
23+
## Setup NEM-Steam-Store (Reactjs)
24+
25+
1. `git clone https://github.com/AnthonyLaw/nem-steam-store`.
26+
2. `cd nem-steam-store`.
27+
3. create `.env` file in root.
28+
```
29+
REACT_APP_PRIVATE_KEY= address private key
30+
REACT_APP_ADDRESS= your address
31+
REACT_APP_STEAMSTORE_ADDRESS= store address from server
32+
REACT_APP_STEAMSTORE_PK= store public key from server
33+
```
34+
4. `npm install`
35+
5. `npm start`
36+
6. Open [http://localhost:3000](http://localhost:3000/) to view it in the browser.
37+
38+
## Setup NEM-Steam-Store-Server (nodejs)
39+
40+
1. `git clone https://github.com/AnthonyLaw/nem-steam-store-server`.
41+
2. `cd nem-steam-store-server`.
42+
3. create `.env` file in root.
43+
```
44+
NEM_PRIVATE_KEY= store private key
45+
NEM_ADDRESS= store address
46+
```
47+
4. `npm install`
48+
5. `npm start`
49+
50+
## Summary
51+
52+
Always running [NEM-Steam-Store-Server](https://github.com/AnthonyLaw/nem-steam-store-server) first and then using [NEM-Steam-Store](https://github.com/AnthonyLaw/nem-steam-store) to make purchase.
53+
54+
Make sure account address have enough (xem) to make transaction.
55+
56+
## Feedback
57+
58+
Welcome every giving feedback, create issue or pull request. :)

app.js

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
var nem = require('nem-library');
2+
var nemsdk = require('nem-sdk').default;
3+
var gameCode = require('./game');
4+
var dotenv = require('dotenv');
5+
6+
dotenv.load();
7+
8+
// Initialize NEMLibrary for TEST_NET Network
9+
nem.NEMLibrary.bootstrap(nem.NetworkTypes.TEST_NET);
10+
11+
const nodeEndpoint = [
12+
{
13+
domain: '192.3.61.243',
14+
},
15+
];
16+
17+
const address = new nem.Address(process.env.NEM_ADDRESS);
18+
19+
let confirmedTransactionListener = new nem.ConfirmedTransactionListener(
20+
nodeEndpoint
21+
).given(address);
22+
23+
confirmedTransactionListener.subscribe(
24+
tx => {
25+
purchaseGame(tx);
26+
},
27+
err => {
28+
console.log(err);
29+
}
30+
);
31+
32+
function purchaseGame(tx) {
33+
const buyerAdd = tx.signer.address.plain();
34+
const amount = tx._xem.amount;
35+
const code = JSON.parse(nemsdk.utils.convert.hex2a(tx.message.payload));
36+
37+
var gamepurchase = [];
38+
39+
gameCode.map(game => {
40+
if (game.name === code.gamecode) {
41+
gamepurchase.push(game);
42+
}
43+
});
44+
45+
booking(gamepurchase[0], buyerAdd, amount);
46+
47+
if (gamepurchase.length != 0) {
48+
if (amount >= gamepurchase[0].info.price) {
49+
sendActivationCode(tx, gamepurchase[0]);
50+
} else {
51+
console.log('credit not enough');
52+
}
53+
}
54+
}
55+
56+
function sendActivationCode(tx, gamepurchase) {
57+
const transactionHttp = new nem.TransactionHttp();
58+
59+
const privateKey = process.env.NEM_PRIVATE_KEY;
60+
61+
const recipientPublicAccount = nem.PublicAccount.createWithPublicKey(
62+
tx.signer.publicKey
63+
);
64+
65+
const account = nem.Account.createWithPrivateKey(privateKey);
66+
67+
let key =
68+
gamepurchase.info.activationCode[
69+
Math.floor(Math.random() * gamepurchase.info.activationCode.length)
70+
];
71+
72+
let invoice = Math.floor(Math.random() * 99999);
73+
74+
let receipt =
75+
'{"invoice": ' +
76+
invoice +
77+
',"code": "' +
78+
key +
79+
'","game": "' +
80+
gamepurchase.name +
81+
'"}';
82+
83+
const encryptedMessage = account.encryptMessage(
84+
receipt,
85+
recipientPublicAccount
86+
);
87+
const transferTransaction = nem.TransferTransaction.create(
88+
nem.TimeWindow.createWithDeadline(),
89+
recipientPublicAccount.address,
90+
new nem.XEM(0),
91+
encryptedMessage
92+
);
93+
94+
const signedTransaction = account.signTransaction(transferTransaction);
95+
96+
transactionHttp.announceTransaction(signedTransaction).subscribe(x => {
97+
sendInvoice(
98+
invoice,
99+
x.transactionHash.data,
100+
recipientPublicAccount.address.plain(),
101+
gamepurchase
102+
);
103+
});
104+
}
105+
106+
function sendInvoice(invoice, tx, address, game) {
107+
console.log('----------------------Invoice------------------------');
108+
console.log('Invoice: ' + invoice);
109+
console.log('Tx: ' + tx);
110+
console.log('Receiver: ' + address);
111+
console.log('Game: ' + game.name);
112+
}
113+
114+
function booking(game, address, amount) {
115+
console.log('----------------------Booking------------------------');
116+
console.log('Game: ' + game.name);
117+
console.log('Buyer: ' + address);
118+
console.log('Amount: ' + amount);
119+
}
120+
121+
console.log('----------------------Steam Store------------------------');
122+
console.log('Good day.....');

game.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module.exports = [
2+
{
3+
name: 'dota2',
4+
info: {
5+
price: 5,
6+
activationCode: [
7+
'7DFB49E895518C137E623F784F7A2',
8+
'46DB88E834EC2E4B9D73D548CF728',
9+
'473B89D711C24D85822A9915E9A33',
10+
'1E78CD242EC7D33524E3CBE73E44E',
11+
],
12+
},
13+
},
14+
{
15+
name: 'overwatch',
16+
info: {
17+
price: 20,
18+
activationCode: [
19+
'7DFB49E895518C137E623F784F7A2',
20+
'46DB88E834EC2E4B9D73D548CF728',
21+
'473B89D711C24D85822A9915E9A33',
22+
'1E78CD242EC7D33524E3CBE73E44E',
23+
],
24+
},
25+
},
26+
{
27+
name: 'far_cry',
28+
info: {
29+
price: 15,
30+
activationCode: [
31+
'7DFB49E895518C137E623F784F7A2',
32+
'46DB88E834EC2E4B9D73D548CF728',
33+
'473B89D711C24D85822A9915E9A33',
34+
'1E78CD242EC7D33524E3CBE73E44E',
35+
],
36+
},
37+
},
38+
{
39+
name: 'god_of_war',
40+
info: {
41+
price: 10,
42+
activationCode: [
43+
'7DFB49E895518C137E623F784F7A2',
44+
'46DB88E834EC2E4B9D73D548CF728',
45+
'473B89D711C24D85822A9915E9A33',
46+
'1E78CD242EC7D33524E3CBE73E44E',
47+
],
48+
},
49+
},
50+
];

package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "nem-steam-store-server",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "node app.js"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"dotenv": "^6.0.0",
14+
"express": "^4.16.3",
15+
"nem-library": "^1.0.7",
16+
"nem-sdk": "^1.6.7",
17+
"socket.io": "^2.1.1"
18+
}
19+
}

0 commit comments

Comments
 (0)