Skip to content

Commit

Permalink
chore(anchor): move together island & ship dirs into anchor
Browse files Browse the repository at this point in the history
  • Loading branch information
Wildan committed Jun 21, 2019
1 parent 55b6997 commit 033ab55
Show file tree
Hide file tree
Showing 32 changed files with 7,190 additions and 80 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ proto-web:

proto-island-nem:
@echo "*****Island nem package*****"
@cd ${PWD}/island/nem/ && \
@cd ${PWD}/anchor/island/nem/ && \
yarn run proto

proto-inspector-nem:
Expand Down Expand Up @@ -55,7 +55,7 @@ install-go-mod:

install-island-nem:
@echo "*****Island nem package*****"
@cd ${PWD}/island/nem/ && \
@cd ${PWD}/anchor/island/nem/ && \
yarn install

install-inspector-nem:
Expand All @@ -77,7 +77,7 @@ island: island-${type}
# usage: make run ship type=<ship endpoint type>
# example: make run ship type=nem2
ship:
@go run ship/${type}/main.go
@go run anchor/ship/${type}/main.go

# Run inspector service
# usage: make run ship type=<inspector endpoint type>
Expand All @@ -91,12 +91,12 @@ skipper: skipper-${type}

island-nem:
@echo "*****Running Island nem package*****"
@cd ${PWD}/island/nem/ && \
@cd ${PWD}/anchor/island/nem/ && \
yarn run dev

island-nem2:
@echo "*****Running Island nem2 package*****"
@go run ${PWD}/island/nem2/main.go
@go run ${PWD}/anchor/island/nem2/main.go

inspector-nem:
@echo "*****Running Inspector nem package*****"
Expand Down
59 changes: 3 additions & 56 deletions anchor/README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,3 @@
# Anchor proto

## Anchor object schema

| Name | Description |
| ---------------------- | -------------------------------------------------- |
| description (optional) | Generic identifier |
| version | Anchor schema version |
| target | Target island type. One of `nem`, `btc`, or `eth` |
| locks | Array of `Lock` objects. ***Minimum length of 1*** |

## Lock object schema

| Name | Description |
| ---------------------------- | ----------------------------------------------------------- |
| type | Ship type. One of `nem` or `eth` |
| version (optional) | Current version of ship. Eg. NEM catapult will be `0.2.0.2` |
| name (optional) | Ship identifier |
| block | Object containing ship's data |
| lastKnownPosition (optional) | Transaction hash of the last anchor point to an island |

## Block object schema

| Name | Description |
| -------------- | ------------------------------------------------------------ |
| height | Current block height |
| hash | Block hash at current height |
| timestamp | Timestamp at current height |
| uri (optional) | Array of uri's to check the validity of the block. It should provide a direct link to the block resource |

### Example anchor message in JSON format

```json
{
"description": "LuxTag Chain Anchoring Service",
"version": "1.0.0",
"target": "eth",
"locks": [
{
"type": "nem",
"version": "0.2.0.2",
"name": "LuxTag Private Chain X",
"block": {
"height": 1,
"hash": "FAD4265E038881BC9772E69A…DD4FF86F191FD5F8C3AD566",
"timestamp": 0,
"uri": [
"http://localhost:3000/block/1"
]
},
"lastKnownPosition": ""
},
... Anchor multiple private chains at once
]
}
```
Example:
- curl https://cas-shipper.luxtagofficial.now.sh/bcm::iium
- curl https://cas-shipper.luxtagofficial.now.sh/bcm::nem
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions anchor/anchors.sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"bcm::iium": {
"ship": {
"type": "NEM2",
"endpoint": "http://api.bcm.luxtag.io",
"networkType": "MIJIN_TEST"
},
"island": {
"type": "NEM2",
"endpoint": "https://api.iium.luxtag.io",
"networkType": "MIJIN_TEST",
"privateKey": "some-private-key"
}
},
"bcm::nem": {
"ship": {
"type": "NEM2",
"endpoint": "https://api.bcm.luxtag.io",
"networkType": "MIJIN_TEST"
},
"island": {
"type": "NEM",
"endpoint": "http://bigalice2.nem.ninja:7890",
"networkType": "testnet",
"privateKey": "some-private-key"
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 8 additions & 8 deletions island/nem/cli.ts → anchor/island/nem/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import grpc from 'grpc';
import yargs from 'yargs';
import * as services from './anchor/anchor_grpc_pb';
import * as services from '../../_proto/anchor_grpc_pb';
import { Island, IIslandArgs } from './island';

const parseArguments = (): IIslandArgs => {
const parseArguments = (): {islandArgs: IIslandArgs, port: string} => {
const args = yargs
.help('help').alias('help', 'h')
.env('ISLAND_NEM')
Expand Down Expand Up @@ -61,22 +61,22 @@ const parseArguments = (): IIslandArgs => {
};
}

const startServer = (port: string) => {
const startServer = (island: Island, grpcPort: string) => {
const server = new grpc.Server();
server.addService(services.AnnounceService, {location: island.location.bind(island)});
const port = server.bind(`0.0.0.0${port}`, grpc.ServerCredentials.createInsecure());
if (port === 0) {
console.error(`Failed to bind to ${port}`);
const port = server.bind(`0.0.0.0${grpcPort}`, grpc.ServerCredentials.createInsecure());
if (!port) {
console.error(`Failed to bind to ${grpcPort}`);
process.exit(1);
}
server.start();
console.log(`Island listening on ${port}`);
console.log(`Island listening on ${grpcPort}`);
}

function main() {
const { islandArgs, port } = parseArguments()
const island = new Island(islandArgs)
startServer(port);
startServer(island, port);
}

main();
5 changes: 2 additions & 3 deletions island/nem/island.ts → anchor/island/nem/island.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import fetch from 'node-fetch';
import { parse } from 'url';
import * as nemSDK from 'nem-sdk';
import * as services from '../_proto/anchor_grpc_pb';
import * as messages from '../_proto/anchor_pb';
import * as messages from '../../_proto/anchor_pb';

const nem = nemSDK.default;

Expand All @@ -22,7 +21,7 @@ export class Island {

const { protocol, hostname, port } = parse(this.args.endpoint)
this.hostname = protocol + '//' + hostname
this.port = port
this.port = port || (protocol == 'http:' ? '80' : '443')
}

public location(call, callback) {
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions island/nem/package.json → anchor/island/nem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"build": "tsc",
"dev": "ts-node cli.ts",
"proto": "yarn run proto-generate && yarn run proto-generate-types",
"proto-generate": "grpc_tools_node_protoc --js_out=import_style=commonjs,binary:./anchor --grpc_out=./anchor --plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin` -I ../../anchor anchor.proto",
"proto-generate-types": "protoc --plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts --ts_out=./anchor -I ../../anchor anchor.proto"
"proto-generate": "grpc_tools_node_protoc --js_out=import_style=commonjs,binary:./anchor --grpc_out=./anchor --plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin` -I ../../_proto anchor.proto",
"proto-generate-types": "protoc --plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts --ts_out=./_proto -I ../../_proto anchor.proto"
},
"devDependencies": {
"@types/node": "^12.0.0",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
88 changes: 88 additions & 0 deletions anchor/island/nem2/island.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import fetch from 'node-fetch';
import { parse } from 'url';
import * as services from '../../_proto/anchor_grpc_pb';
import * as messages from '../../_proto/anchor_pb';
import {
Account,
Deadline,
AccountHttp,
NetworkType,
TransactionHttp,
PlainMessage,
TransferTransaction,
} from 'nem2-sdk';

export interface IIslandArgs {
endpoint: string;
networkType: string;
privateKey: string;
}

export class Island {
private args: IIslandArgs;
private hostname: string;
private port: string;

public constructor(args: IIslandArgs) {
this.args = args;

const { protocol, hostname, port } = parse(this.args.endpoint)
this.hostname = protocol + '//' + hostname
this.port = port
}

public location(call, callback) {
const reply = new messages.CallSign();
const anchor = this.generateAnchor(call.request);
this.announceAnchor(anchor).then((response) => {
console.log(response);
reply.setId(response.hash);
callback(null, reply);
});
}

public generateAnchor(lock: messages.Lock): messages.Anchor {
const anchor = new messages.Anchor();
anchor.setDescription('LuxTag Chain Anchoring Service');
anchor.setVersion('1.0.2');
anchor.setTarget(messages.IslandType.NEM2);
anchor.addLocks(lock);
return anchor;
}

public async announceAnchor(anchor: messages.Anchor): Promise<any> {
const sender = Account.createFromPrivateKey(this.args.privateKey, NetworkType[this.args.networkType]);
const accountHttp = new AccountHttp(this.args.endpoint);
const transactionHttp = new TransactionHttp(this.args.endpoint);

const serialized = anchor.serializeBinary();

// let msg = Buffer.from(serialized).toString('utf8')
// console.log("msg:", msg)
// // fix "Error: hex string has unexpected size '285'"
// msg += '\u001a\f101645436340'

const msg = 'hello'

const recipientAddress = sender.address
const transferTransaction = TransferTransaction.create(
Deadline.create(),
recipientAddress,
[],
PlainMessage.create(msg),
NetworkType.MIJIN_TEST);

const signedTx = sender.sign(transferTransaction)
return transactionHttp.announce(signedTx)
.toPromise()
.then(() => ({
hash: signedTx.hash
}))
}

public currentBlockHeight(): Promise<string> {
return fetch(this.args.endpoint + '/diagnostic/storage')
.then(resp => resp.json())
.then(json => json.numBlocks.toString())
}
}
2 changes: 1 addition & 1 deletion island/nem2/main.go → anchor/island/nem2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"time"

"github.com/golang/protobuf/proto"
pb "github.com/luxtagofficial/chain-anchoring-service/anchor"
pb "github.com/luxtagofficial/chain-anchoring-service/_proto"
"github.com/luxtagofficial/nem2-sdk-go/sdk"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand Down
7 changes: 7 additions & 0 deletions anchor/island/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"nem-sdk": "^1.6.7",
"nem2-sdk": "^0.11.6",
"node-fetch": "^2.6.0"
}
}
Loading

0 comments on commit 033ab55

Please sign in to comment.