Skip to content

Commit 6c20136

Browse files
committed
Added e2e test, still ignored since not integrated with gamma
1 parent 9869c56 commit 6c20136

21 files changed

+350
-37
lines changed

e2e/browser/browser.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const path = require("path");
2+
3+
describe("E2E browser", () => {
4+
5+
beforeAll(async () => {
6+
await page.goto(`file:${path.join(__dirname, "index.html")}`);
7+
});
8+
9+
it("should display 'success' on page", async () => {
10+
await expect(page).toMatch("success");
11+
});
12+
13+
});

e2e/browser/index.html

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
</head>
55
<body>
66
<script>
7-
const el = document.createElement("h1");
8-
el.textContent = "hello" + OrbsClient.double(5);
9-
document.body.appendChild(el);
7+
if (Orbs.createAccount().publicKey.byteLength == 32) {
8+
const el = document.createElement("h1");
9+
el.textContent = "success";
10+
document.body.appendChild(el);
11+
}
1012
</script>
1113
</body>
1214
</html>

e2e/browser/index.test.js

-13
This file was deleted.

e2e/nodejs/e2e.test.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const Orbs = require("../../dist/index.js");
2+
3+
const GAMMA_PORT = 8080;
4+
const GAMMA_ENDPOINT = "localhost";
5+
const VIRTUAL_CHAIN_ID = 42;
6+
7+
test("E2E nodejs: CreateAccount", () => {
8+
const account = Orbs.createAccount();
9+
console.log(account.address);
10+
expect(account.publicKey.byteLength).toBe(32);
11+
expect(account.privateKey.byteLength).toBe(64);
12+
});
13+
14+
test.skip("E2E nodejs: SimpleTransfer", async () => {
15+
// TODO: start gamma here
16+
17+
// create sender account
18+
const sender = Orbs.createAccount();
19+
20+
// create receiver account
21+
const receiver = Orbs.createAccount();
22+
23+
// create client
24+
const endpoint = `http://${GAMMA_ENDPOINT}:${GAMMA_PORT}`;
25+
const client = new Orbs.Client(endpoint, VIRTUAL_CHAIN_ID, "TEST_NET");
26+
27+
// create transfer transaction payload
28+
const [payload1, txId] = client.createSendTransactionPayload(
29+
sender.publicKey,
30+
sender.privateKey,
31+
"BenchmarkToken",
32+
"transfer",
33+
[ new Orbs.Uint64Arg(10), new Orbs.BytesArg(receiver.rawAddress) ]
34+
);
35+
36+
// send the payload
37+
const transferResponse = await client.sendTransaction(payload1);
38+
expect(transferResponse.requestStatus).toEqual("COMPLETED");
39+
expect(transferResponse.executionResult).toEqual("SUCCESS");
40+
expect(transferResponse.transactionStatus).toEqual("COMMITTED");
41+
42+
// create get status payload
43+
const payload2 = client.createGetTransactionStatusPayload(txId);
44+
45+
// send the payload
46+
const statusResponse = await client.getTransactionStatus(payload2);
47+
expect(statusResponse.requestStatus).toEqual("COMPLETED");
48+
expect(statusResponse.executionResult).toEqual("SUCCESS");
49+
expect(statusResponse.transactionStatus).toEqual("COMMITTED");
50+
51+
// create balance method call payload
52+
const payload3 = client.createCallMethodPayload(
53+
receiver.publicKey,
54+
"BenchmarkToken",
55+
"getBalance",
56+
[ new Orbs.BytesArg(receiver.rawAddress) ]
57+
);
58+
59+
// send the payload
60+
const balanceResponse = await client.callMethod(payload3);
61+
expect(balanceResponse.requestStatus).toEqual("COMPLETED");
62+
expect(balanceResponse.executionResult).toEqual("SUCCESS");
63+
expect(balanceResponse.OutputArguments[0]).toEqual(new Orbs.Uint64Arg(10));
64+
65+
});

e2e/nodejs/index.test.js

-5
This file was deleted.

package-lock.json

+69-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@
3434
"typescript": "^3.1.5"
3535
},
3636
"dependencies": {
37+
"axios": "^0.18.0",
38+
"base-58": "0.0.1",
3739
"elliptic": "^6.4.1",
3840
"fast-text-encoding": "^1.0.0",
41+
"get-random-values": "^1.2.0",
3942
"hash.js": "^1.1.5"
4043
}
4144
}

rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default {
1515
{
1616
file: pkg.browser,
1717
format: 'iife',
18-
name: 'OrbsClient'
18+
name: 'Orbs'
1919
},
2020
],
2121
external: [

src/crypto/Base58.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import "../membuffers/matcher-extensions";
2+
import * as Base58 from "./Base58";
3+
4+
test("Base58Encode", () => {
5+
const encoded = Base58.encode(Buffer.from("helloworldandstuff1124z24"));
6+
expect(encoded).toEqual("j1Q1Y54mCcVfR5jVAQMMJEy6VbZEtYeM3R");
7+
});
8+
9+
test("Base58Encode binary data", () => {
10+
const source = new Uint8Array([0x01, 0x02, 0x03]);
11+
const encoded = Base58.encode(source);
12+
const decoded = Base58.decode(encoded);
13+
expect(decoded).toBeEqualToUint8Array(source);
14+
});

src/crypto/Base58.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const Base58 = require("base-58");
2+
3+
export function encode(source: Uint8Array): string {
4+
return Base58.encode(source);
5+
}
6+
7+
export function decode(source: string): Uint8Array {
8+
return Base58.decode(source);
9+
}

src/crypto/Hash.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ import { getTextEncoder } from "../membuffers/text";
44

55
const someData = getTextEncoder().encode("testing");
66
const ExpectedSha256 = Buffer.from("cf80cd8aed482d5d1527d7dc72fceff84e6326592848447d2dc0b0e87dfc9a90", "hex");
7+
const ExpectedSha256Ripmd160 = Buffer.from("1acb19a469206161ed7e5ed9feb996a6e24be441", "hex");
78

89
test("CalcSha256", () => {
910
const h = Hash.calcSha256(someData);
1011
expect(h).toBeEqualToUint8Array(ExpectedSha256);
12+
});
13+
14+
test("CalcRipmd160Sha256", () => {
15+
const h = Hash.calcRipmd160Sha256(someData);
16+
expect(h).toBeEqualToUint8Array(ExpectedSha256Ripmd160);
1117
});

src/crypto/Hash.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
const hash = require("hash.js");
22

3+
const SHA256_HASH_SIZE_BYTES = 32;
4+
const RIPMD160_HASH_SIZE_BYTES = 20;
5+
36
export function calcSha256(data: Uint8Array): Uint8Array {
47
const inputArr = [].slice.call(data);
58
const outputArr = hash.sha256().update(inputArr).digest();
69
return new Uint8Array(outputArr);
10+
}
11+
12+
export function calcRipmd160Sha256(data: Uint8Array): Uint8Array {
13+
const inputArr = [].slice.call(data);
14+
const outputArr1 = hash.sha256().update(inputArr).digest();
15+
const outputArr2 = hash.ripemd160().update(outputArr1).digest();
16+
return new Uint8Array(outputArr2);
717
}

src/crypto/Keys.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import "../membuffers/matcher-extensions";
2+
import * as Keys from "./Keys";
3+
4+
test("GenerateEd25519", () => {
5+
const keyPair1 = Keys.generateEd25519Key();
6+
console.log("pair1:", Buffer.from(keyPair1.privateKey).toString("hex"));
7+
expect(keyPair1.publicKey.byteLength).toBe(Keys.ED25519_PUBLIC_KEY_SIZE_BYTES);
8+
expect(keyPair1.privateKey.byteLength).toBe(Keys.ED25519_PRIVATE_KEY_SIZE_BYTES);
9+
10+
const keyPair2 = Keys.generateEd25519Key();
11+
console.log("pair2:", Buffer.from(keyPair2.privateKey).toString("hex"));
12+
expect(keyPair1.privateKey).not.toBeEqualToUint8Array(keyPair2.privateKey);
13+
});

src/crypto/Keys.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,32 @@
11
export const ED25519_PUBLIC_KEY_SIZE_BYTES = 32;
2-
export const ED25519_PRIVATE_KEY_SIZE_BYTES = 64;
2+
export const ED25519_PRIVATE_KEY_SIZE_BYTES = 64;
3+
4+
export interface Ed25519KeyPair {
5+
publicKey: Uint8Array;
6+
privateKey: Uint8Array;
7+
}
8+
9+
export function generateEd25519Key(): Ed25519KeyPair {
10+
// generate secure random
11+
const getRandomValues = require("get-random-values");
12+
const secureRandom = new Uint8Array(ED25519_PRIVATE_KEY_SIZE_BYTES - ED25519_PUBLIC_KEY_SIZE_BYTES);
13+
getRandomValues(secureRandom);
14+
15+
// generate key
16+
const eddsa = require("elliptic").eddsa;
17+
const ec = new eddsa("ed25519");
18+
const key = ec.keyFromSecret(uint8ArrayToHexString(secureRandom));
19+
// console.log(key.getPublic("hex"));
20+
// console.log(key.getSecret("hex"));
21+
22+
// return
23+
const publicKey = new Uint8Array(key.getPublic("bytes"));
24+
const privateKey = new Uint8Array(ED25519_PRIVATE_KEY_SIZE_BYTES);
25+
privateKey.set(new Uint8Array(key.getSecret("bytes")), 0);
26+
privateKey.set(publicKey, ED25519_PRIVATE_KEY_SIZE_BYTES - ED25519_PUBLIC_KEY_SIZE_BYTES);
27+
return { publicKey, privateKey };
28+
}
29+
30+
function uint8ArrayToHexString(arr: Uint8Array): string {
31+
return Array.prototype.map.call(arr, (x: any) => ("00" + x.toString(16)).slice(-2)).join("");
32+
}

0 commit comments

Comments
 (0)