-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from claudiucelfilip/key-generation
Generate Wavelet compatible keys
- Loading branch information
Showing
9 changed files
with
157 additions
and
21 deletions.
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
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
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
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,45 @@ | ||
// Worker.ts | ||
import * as nacl from "tweetnacl"; | ||
import { blake2b } from "blakejs"; | ||
|
||
const ctx: Worker = self as any; | ||
|
||
onmessage = evt => { | ||
const { type, ...data } = evt.data; | ||
switch (type) { | ||
case "generateKeys": | ||
generateNewKeys(data.c1); | ||
break; | ||
} | ||
}; | ||
|
||
const generateNewKeys = (c1 = 1) => { | ||
let generatedKeys; | ||
let checksum; | ||
|
||
const prefixLen = (buf: Uint8Array) => { | ||
for (let i = 0; i < buf.length; i++) { | ||
const b = buf[i]; | ||
if (b !== 0) { | ||
// b.toString(2) removes leading 0s; so we just see how many were removed | ||
const leadingZeros = 8 - b.toString(2).length; | ||
|
||
return i * 8 + leadingZeros; | ||
} | ||
} | ||
|
||
return buf.length * 8 - 1; | ||
}; | ||
|
||
do { | ||
generatedKeys = nacl.sign.keyPair(); | ||
|
||
const id = blake2b(generatedKeys.publicKey, undefined, 32); | ||
checksum = blake2b(id, undefined, 32); | ||
} while (prefixLen(checksum) < c1); | ||
|
||
ctx.postMessage({ | ||
type: "newKeys", | ||
data: generatedKeys | ||
}); | ||
}; |
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
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
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 @@ | ||
declare module "blakejs"; |
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 @@ | ||
declare module "worker-loader!*" { | ||
class WebpackWorker extends Worker { | ||
constructor(); | ||
} | ||
|
||
export default WebpackWorker; | ||
} |
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