Skip to content

Commit

Permalink
stop 2 hash fn from being pub with wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
RGGH committed Nov 4, 2024
1 parent cee3b65 commit a1d9a68
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 80 deletions.
14 changes: 1 addition & 13 deletions pkg/bitcoin_hashing_wasm.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
/* tslint:disable */
/* eslint-disable */
/**
* @param {Uint8Array} input
* @returns {Uint8Array}
*/
export function sha256(input: Uint8Array): Uint8Array;
/**
* @param {Uint8Array} input
* @returns {Uint8Array}
*/
export function ripemd160(input: Uint8Array): Uint8Array;
/**
* @param {string} public_key_hex
* @returns {string}
Expand All @@ -20,13 +10,11 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl

export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly sha256: (a: number, b: number, c: number) => void;
readonly ripemd160: (a: number, b: number, c: number) => void;
readonly generate_bitcoin_address: (a: number, b: number, c: number) => void;
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
}

export type SyncInitInput = BufferSource | WebAssembly.Module;
Expand Down
73 changes: 11 additions & 62 deletions pkg/bitcoin_hashing_wasm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
let wasm;

let WASM_VECTOR_LEN = 0;

let cachedUint8ArrayMemory0 = null;

function getUint8ArrayMemory0() {
Expand All @@ -9,68 +11,6 @@ function getUint8ArrayMemory0() {
return cachedUint8ArrayMemory0;
}

let WASM_VECTOR_LEN = 0;

function passArray8ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 1, 1) >>> 0;
getUint8ArrayMemory0().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}

let cachedDataViewMemory0 = null;

function getDataViewMemory0() {
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
}
return cachedDataViewMemory0;
}

function getArrayU8FromWasm0(ptr, len) {
ptr = ptr >>> 0;
return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
}
/**
* @param {Uint8Array} input
* @returns {Uint8Array}
*/
export function sha256(input) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(input, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.sha256(retptr, ptr0, len0);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
var v2 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1, 1);
return v2;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}

/**
* @param {Uint8Array} input
* @returns {Uint8Array}
*/
export function ripemd160(input) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(input, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.ripemd160(retptr, ptr0, len0);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
var v2 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 1, 1);
return v2;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}

const cachedTextEncoder = (typeof TextEncoder !== 'undefined' ? new TextEncoder('utf-8') : { encode: () => { throw Error('TextEncoder not available') } } );

const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
Expand Down Expand Up @@ -125,6 +65,15 @@ function passStringToWasm0(arg, malloc, realloc) {
return ptr;
}

let cachedDataViewMemory0 = null;

function getDataViewMemory0() {
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
}
return cachedDataViewMemory0;
}

const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } );

if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); };
Expand Down
Binary file modified pkg/bitcoin_hashing_wasm_bg.wasm
Binary file not shown.
4 changes: 1 addition & 3 deletions pkg/bitcoin_hashing_wasm_bg.wasm.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/* tslint:disable */
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function sha256(a: number, b: number, c: number): void;
export function ripemd160(a: number, b: number, c: number): void;
export function generate_bitcoin_address(a: number, b: number, c: number): void;
export function __wbindgen_add_to_stack_pointer(a: number): number;
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_free(a: number, b: number, c: number): void;
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
export function __wbindgen_free(a: number, b: number, c: number): void;
17 changes: 15 additions & 2 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@
</head>
<body>
<div class="container mt-5">
<h1>Bitcoin Address Generator</h1>
<h1>Legacy Bitcoin Address Generator</h1>
<div class="mb-3">
<label for="input" class="form-label">Public Key (Hex):</label>
<input type="text" class="form-control" id="input" placeholder="Enter public key in hex">
</div>
<button class="btn btn-primary" onclick="generateBitcoinAddress()">Generate Bitcoin Address</button>
<pre id="output" class="mt-3"></pre>
<pre id="output" class="mt-2"></pre>
<div>
This is a demo, it uses Legacy Bitcoin addresses, which start with a 1 for P2PKH (Pay-to-Public-Key-Hash) addresses. Address Formats in Bitcoin
</div>

<div>

Paste in this Public key if you just want to test the generator
</div>
<div>
<b>
0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352
</b>
</div>
</div>

<script type="module" src="./index.js"></script>
Expand Down

0 comments on commit a1d9a68

Please sign in to comment.