Last active
January 14, 2022 13:55
-
-
Save lanmower/3e97ee700ac6a07a9401475ab23b92c7 to your computer and use it in GitHub Desktop.
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
<script src="https://unpkg.com/msgpackr@1.2.5/dist/index.js"></script> | |
<script src="https://unpkg.com/tweetnacl@1.0.3/nacl.min.js"></script> | |
<script src="https://bundle.run/buffer@6.0.3"></script> | |
// SIGNER | |
window.packr = new msgpackr.Packr({ structuredClone: true }); | |
const C = { | |
CONTRACT: 'c', | |
ACTION: 'a', | |
INPUT: 'i', | |
TRANSACTION: 't', | |
KEYS: 'k', | |
SIGNATURE: 's', | |
PUBLICKEY: 'k' | |
}; | |
window.sign = (from) => { | |
const tx = {}; | |
tx[C.ACTION] = from.action; | |
tx[C.INPUT] = from.input; | |
const keyUint8Array = new Uint8Array(Buffer.from(from.keyPair.secretKey, "hex")); | |
const packed = packr.pack(tx); | |
const out = {}; | |
out[C.TRANSACTION] = new Uint8Array(packed); | |
out[C.SIGNATURE] = nacl.sign.detached(out[C.TRANSACTION], keyUint8Array); | |
out[C.PUBLICKEY] = new Uint8Array(Buffer.from(from.keyPair.publicKey, "hex")); | |
return packr.pack(out); | |
}; | |
//VALIDATOR | |
global.crypto = require("hypercore-crypto"); | |
const Packr = require("msgpackr").Packr; | |
const verify = (message) => { | |
const input = packr.unpack(message); | |
const verified = crypto.verify( | |
input['transaction'[0]], | |
input['signature'[0]], | |
input['key'[0]] | |
); | |
if (!verified) throw new Error("could not verify transaction"); | |
input['transaction'[0]] = packr.unpack(input['transaction'[0]]); | |
return input; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment