Skip to content

Commit

Permalink
update to v0.1.3 (#4)
Browse files Browse the repository at this point in the history
* feat: custom error support.
* refactor: example nodejs
* test: update example webpage

Co-authored-by: k-matsuzawa <[email protected]>
  • Loading branch information
ko-matsu and k-matsuzawa authored Jul 30, 2020
1 parent 95d7fa2 commit 09475b9
Show file tree
Hide file tree
Showing 63 changed files with 4,107 additions and 1,480 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/create_release-and-upload.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ jobs:
- name: Get the version
id: get_version
run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//}
- name: check package.json version
env:
version: ${{ steps.get_version.outputs.VERSION }}
run: node ./tools/checker.js version ${version}
- name: Create Release
id: create_release
uses: actions/create-release@v1
Expand Down
6 changes: 4 additions & 2 deletions cfdjs_wasm_json.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ cfdjsWasm['onRuntimeInitialized'] = async () => {
const funcNameResult = await cfdjsWasmJsonApi.ccallCfd(cfdjsWasm,
cfdjsWasm._cfdjsGetJsonApiNames, 'string', [], []);
if (funcNameResult.indexOf('Error:') >= 0) {
throw new Error(`cfdjsGetJsonApiNames internal error. ${funcNameResult}`);
throw new cfdjsWasmJsonApi.CfdError(
`cfdjsGetJsonApiNames internal error. ${funcNameResult}`);
}
const funcList = funcNameResult.split(',');

// register function list
funcList.forEach((requestName) => {
const hook = async function(...args) {
if (args.length > 1) {
throw Error('ERROR: Invalid argument passed:' +
throw new cfdjsWasmJsonApi.CfdError('ERROR: Invalid argument passed:' +
`func=[${requestName}], args=[${args}]`);
}
let arg = '';
Expand All @@ -41,3 +42,4 @@ cfdjsWasm['onRuntimeInitialized'] = async () => {
};

module.exports = wrappedModule;
module.exports.CfdError = cfdjsWasmJsonApi.CfdError;
14 changes: 13 additions & 1 deletion cfdjs_wasm_json_wrap.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
const cfdjsWasmJson = require('./cfdjs_wasm_json.js');

const wrappedModule = {};
let hasLoaded = false;
cfdjsWasmJson['onRuntimeInitialized'] = async () => {
if ('onRuntimeInitialized' in wrappedModule) {
wrappedModule.onRuntimeInitialized();
}
hasLoaded = true;
};

wrappedModule['addInitializedListener'] = function(func) {
wrappedModule['onRuntimeInitialized'] = func;
if (hasLoaded) {
if (func) func();
} else {
wrappedModule['onRuntimeInitialized'] = func;
}
};

wrappedModule['getCfd'] = function() {
return cfdjsWasmJson;
};

wrappedModule['hasLoadedWasm'] = function() {
return hasLoaded;
};

wrappedModule['CfdError'] = cfdjsWasmJson.CfdError;

module.exports = wrappedModule;
50 changes: 47 additions & 3 deletions cfdjs_wasm_jsonapi.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
/**
* cfd error class.
*/
class CfdError extends Error {
/**
* constructor.
* @param {string} message error message.
* @param {*} errorInformation error information object.
* @param {Error} cause cause error.
*/
constructor(message, errorInformation = undefined, cause = undefined) {
super((!errorInformation) ?
message : message + JSON.stringify(errorInformation));
this.name = 'CfdError';
this.errorInformation = errorInformation;
this.cause = cause;
}
// eslint-disable-next-line valid-jsdoc
/**
* error object string.
* @return message string.
*/
toString() {
return `${this.name}: ${this.message}`;
}
// eslint-disable-next-line valid-jsdoc
/**
* get error information.
* @return InnerErrorResponse object.
*/
getErrorInformation() {
return this.errorInformation;
}
// eslint-disable-next-line valid-jsdoc
/**
* get error cause.
* @return Error or undefined.
*/
getCause() {
return this.cause;
}
}

const ccallCfd = async function(module, func, returnType, argTypes, args) {
const UTF8Decoder = typeof TextDecoder !== 'undefined' ? new TextDecoder('utf8') : undefined;
const stringToUTF8Array = function(str, heap, outIdx, maxBytesToWrite) {
Expand Down Expand Up @@ -149,12 +192,12 @@ const callJsonApi = async function(wasmModule, reqName, arg) {
retObj = JSON.parse(retJson);
} catch (err) {
console.log(err);
throw new Error('ERROR: Invalid function call:' +
` func=[${reqName}], args=[${args}]`);
throw new CfdError('ERROR: Invalid function call:' +
` func=[${reqName}], arg=[${arg}]`, undefined, err);
}

if (retObj.hasOwnProperty('error')) {
throw new Error(JSON.stringify(retObj.error));
throw new CfdError('', retObj.error);
}
return retObj;
};
Expand All @@ -167,6 +210,7 @@ if (PLATFORM_IS_NODE) {
module['exports'] = {
callJsonApi: callJsonApi,
ccallCfd: ccallCfd,
CfdError: CfdError,
};
}
}
2 changes: 1 addition & 1 deletion dist/cfdjs_wasm.js

Large diffs are not rendered by default.

Binary file modified dist/cfdjs_wasm.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
target: cfdWasmBuilder
environment:
WASM_SRC: /private/cfd-js-wasm
WASM_WORK: /private/work
WASM_WORK: /private/work/cfd-js-wasm
working_dir: /private/cfd-js-wasm
volumes:
- /private/work
Expand Down
17 changes: 14 additions & 3 deletions example/createaddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,25 @@ const updateField = async function(event) {
}
}

const decodeBtnField = document.getElementById("execDecode");
decodeBtnField.addEventListener('click', updateField);

Module['onRuntimeInitialized'] = async function(){
const decoded = document.getElementById("decoded");
if (Module['_cfdjsJsonApi']) {
const decodeBtn = document.getElementById("execDecode");
decodeBtn.addEventListener('click', updateField);
console.log("exist cfdjsJsonApi.");
decoded.value = "";
} else {
console.log("cfdjsJsonApi not found!");
const decoded = document.getElementById("decoded");
decoded.value = "WebAssembly load fail.";
}
}

window.onload = function() {
const decoded = document.getElementById("decoded");
if (Module['_cfdjsJsonApi']) {
decoded.value = "";
} else {
decoded.value = "WebAssembly loading...";
}
}
18 changes: 15 additions & 3 deletions example/decodeaddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,33 @@ const updateField = async function(event) {
const resp = await callJsonApi(Module, 'GetAddressInfo', req);
if (confidentialKey.length > 0) {
resp['confidentialKey'] = confidentialKey;
resp['address'] = address;
}
decoded.value = JSON.stringify(resp, null, ' ');
} catch (e) {
decoded.value = 'Invalid address format';
}
}

const decodeBtnField = document.getElementById("execDecode");
decodeBtnField.addEventListener('click', updateField);

Module['onRuntimeInitialized'] = async function(){
const decoded = document.getElementById("decoded");
if (Module['_cfdjsJsonApi']) {
const decodeBtn = document.getElementById("execDecode");
decodeBtn.addEventListener('click', updateField);
console.log("exist cfdjsJsonApi.");
decoded.value = "";
} else {
console.log("cfdjsJsonApi not found!");
const decoded = document.getElementById("decoded");
decoded.value = "WebAssembly load fail.";
}
}

window.onload = function() {
const decoded = document.getElementById("decoded");
if (Module['_cfdjsJsonApi']) {
decoded.value = "";
} else {
decoded.value = "WebAssembly loading...";
}
}
19 changes: 15 additions & 4 deletions example/decoderawtransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,25 @@ const updateField = async function(event) {
}
}

const decodeBtnField = document.getElementById("execDecode");
decodeBtnField.addEventListener('click', updateField);

Module['onRuntimeInitialized'] = async function(){
const decoded = document.getElementById("decodedtx");
if (Module['_cfdjsJsonApi']) {
const decodeBtn = document.getElementById("execDecode");
decodeBtn.addEventListener('click', updateField);
console.log("exist cfdjsJsonApi.");
decoded.value = "";
} else {
console.log("cfdjsJsonApi not found!");
const decodedtx = document.getElementById("decodedtx");
decodedtx.value = "WebAssembly load fail.";
decoded.value = "WebAssembly load fail.";
}
}

window.onload = function() {
const decoded = document.getElementById("decodedtx");
if (Module['_cfdjsJsonApi']) {
decoded.value = "";
} else {
decoded.value = "WebAssembly loading...";
}
}
17 changes: 14 additions & 3 deletions example/decodescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,25 @@ const updateField = async function(event) {
}
}

const decodeBtnField = document.getElementById("execDecode");
decodeBtnField.addEventListener('click', updateField);

Module['onRuntimeInitialized'] = async function(){
const decoded = document.getElementById("decoded");
if (Module['_cfdjsJsonApi']) {
const decodeBtn = document.getElementById("execDecode");
decodeBtn.addEventListener('click', updateField);
console.log("exist cfdjsJsonApi.");
decoded.value = "";
} else {
console.log("cfdjsJsonApi not found!");
const decoded = document.getElementById("decoded");
decoded.value = "WebAssembly load fail.";
}
}

window.onload = function() {
const decoded = document.getElementById("decoded");
if (Module['_cfdjsJsonApi']) {
decoded.value = "";
} else {
decoded.value = "WebAssembly loading...";
}
}
65 changes: 65 additions & 0 deletions example/dumpprivkey.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Dump Privkey</title>
<style>
#main {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 120vh;
font-family: sans-serif;
}
.decodeInput {
width: 99%;
}
.decodeOutput {
width: 99%;
}
.decodetx-output {
vertical-align: top;
margin-top: 13px;
}
h1 {
font-size: 32px;
}
h2 {
font-size: 20px;
font-weight: bold;
}
h3 {
font-size: 16px;
margin-bottom: 0px;
padding-bottom: 0px;
}
.xpubkey-label {
font-size: 12px;
display: inline-block;
width: 8em;
}
</style>
</head>
<body>
<div class="decode">
<h3 class="decode-title">Dump Privkey</h3>
<div class="decode-input">
<div>
<div class="decode-label">Input privkey:</div>
<textarea rows="3" id="inputData" class="decodeInput"></textarea><br>
<input type="button" name="decode" id="execDecode" value="Parse" />
</div>
<div class="decode-output">
<div class="decode-label">Key Info:</div>
<textarea rows="11" id="decoded" class="decodeOutput" readonly></textarea>
</div>
</div>
</div>

<script src="/cfd-js-wasm/dist/cfdjs_wasm.js"></script>
<script src="/cfd-js-wasm/cfdjs_wasm_jsonapi.js"></script>
<script src="dumpprivkey.js"></script>
<p><a href="index.html">back</a></p>
</body>
</html>
Loading

0 comments on commit 09475b9

Please sign in to comment.