Skip to content

Commit

Permalink
[DDW-161] prepare js library setup
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikGuzei committed Mar 6, 2018
1 parent c894bcc commit 4ef07af
Show file tree
Hide file tree
Showing 15 changed files with 5,966 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"presets": [
["env", {
"targets": {
"chrome": 58
}
}]
]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
target/
Cargo.lock
node_modules/
dist
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Exclude everything:
*

# But the dist folder
!dist/
2 changes: 1 addition & 1 deletion build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
set +e
cargo +nightly build --target wasm32-unknown-unknown --release --verbose
cp target/wasm32-unknown-unknown/release/deps/wallet-wasm.wasm ./cardano.wasm
npm run build
42 changes: 42 additions & 0 deletions js/HdWallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { newArray, copyArray } from './utils/arrays';
import { applyModule } from './utils/wasm';
import { loadRustModule } from './RustModule';

export const fromSeed = (module, seed) => {
const bufseed = newArray(module, seed);
const bufxprv = newArray(module, 96, true);
module.wallet_from_seed(bufseed, bufxprv);
let result = copyArray(module, bufxprv, 96);
module.dealloc(bufseed);
module.dealloc(bufxprv);
return result;
};

export const derivePrivate = (module, xprv, index) => {
const bufxprv = newArray(module, xprv);
const bufchild = newArray(module, xprv.length, true);
module.wallet_derive_private(bufxprv, index, bufchild);
let result = copyArray(module, bufchild, xprv.length);
module.dealloc(bufxprv);
module.dealloc(bufchild);
return result;
};

export const sign = (module, xprv, msg) => {
let length = msg.length;
const bufsig = newArray(module, 64, true);
const bufxprv = newArray(module, xprv);
const bufmsg = newArray(module, msg);
module.wallet_sign(bufxprv, bufmsg, length, bufsig);
let result = copyArray(module, bufsig, 64);
module.dealloc(bufxprv);
module.dealloc(bufmsg);
module.dealloc(bufsig);
return result;
};

export default {
fromSeed: applyModule(loadRustModule, fromSeed),
derivePrivate: applyModule(loadRustModule, derivePrivate),
sign: applyModule(loadRustModule, sign),
};
4 changes: 4 additions & 0 deletions js/PaperWallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
scramble: (iv, password, data) => {},
unscramble: (password, shielded_data) => {},
}
13 changes: 13 additions & 0 deletions js/RustModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import loadModule from '../target/wasm32-unknown-unknown/release/wallet_wasm.wasm';

let Module = null;

// Ensure we are only creating a single instance of the web assembly module
export const loadRustModule = () => Module ?
Promise.resolve(Module)
:
loadModule().then((module) => {
Module = module.instance.exports;
return Module;
}
);
17 changes: 17 additions & 0 deletions js/example/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

const { HdWallet } = CardanoCrypto;
var seed = new Uint8Array([0xe3, 0x55, 0x24, 0xa5, 0x18, 0x03, 0x4d, 0xdc, 0x11, 0x92, 0xe1, 0xda, 0xcd, 0x32, 0xc1, 0xed, 0x3e, 0xaa, 0x3c, 0x3b, 0x13, 0x1c, 0x88, 0xed, 0x8e, 0x7e, 0x54, 0xc4, 0x9a, 0x5d, 0x09, 0x98]);

const test = async () => {
const v = await HdWallet.fromSeed(seed);
console.log(v)

const c = await HdWallet.derivePrivate(v, 0x80000000);
console.log(c)

const utf8Encoder = new TextEncoder("UTF-8");
const sig = await HdWallet.sign(c, utf8Encoder.encode("Hello World"));
console.log(sig);
};

test();
29 changes: 29 additions & 0 deletions js/example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
html, body {
width: 100%;
height: 100%;
margin: 0px;
border: 0;
overflow: hidden; /* Disable scrollbars */
display: block; /* No floating content on sides */
}
</style>
<title>HDWALLET</title>
</head>

<body>
<h1 id="header">WASM hdwallet</h1>
<canvas id="canvas" style='position:absolute; left:0px; top:0px; width: 100%'></canvas>
<p id="middle">r1: <span id="placeholder1"></span></p>
<p id="middle2">r2: <span id="placeholder2"></span></p>
<p id="middle3">r3: <span id="placeholder3"></span></p>
<p id="end">the end..</p>

<script src="../../dist/index.js"></script>
<script src="./example.js"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import HdWallet from './HdWallet.js';
import PaperWallet from './PaperWallet.js';

module.exports = {
HdWallet,
PaperWallet,
};
24 changes: 24 additions & 0 deletions js/utils/arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const newArray = (module, b, isZero=false) => {
let len = b.length
let ptr = module.alloc(len)

let memory = new Uint8Array(module.memory.buffer)
for (let i = 0; i < len; i++) {
memory[ptr+i] = isZero ? 0 : b[i];
}
return ptr
}

export const copyArray = (module, ptr, sz) => {
const collect = function* () {
let memory = new Uint8Array(module.memory.buffer);
let i = 0;
while (i < sz) {
yield memory[ptr+i];
i += 1
}
}

const buffer_as_u8 = new Uint8Array(collect());
return buffer_as_u8
}
3 changes: 3 additions & 0 deletions js/utils/wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const applyModule = (loadModule, target) => (
(...args) => loadModule().then((module) => target(module, ...args))
);
Loading

0 comments on commit 4ef07af

Please sign in to comment.