forked from input-output-hk/js-cardano-wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c894bcc
commit 4ef07af
Showing
15 changed files
with
5,966 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"presets": [ | ||
["env", { | ||
"targets": { | ||
"chrome": 58 | ||
} | ||
}] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
target/ | ||
Cargo.lock | ||
node_modules/ | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Exclude everything: | ||
* | ||
|
||
# But the dist folder | ||
!dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default { | ||
scramble: (iv, password, data) => {}, | ||
unscramble: (password, shielded_data) => {}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
); |
Oops, something went wrong.