Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated tlock and added options for quicknet/fastnet #66

Merged
merged 4 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
599 changes: 465 additions & 134 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"clean": "rm -rf ./dist/*",
"lint": "eslint",
"lint:fix": "eslint --fix",
"start": "parcel ./src/index.html",
"start": "parcel ./src/index.html --public-url /",
"test": "mocha --require ts-node/register ./test/**/*.test.ts"
},
"author": "drand.love",
Expand All @@ -36,7 +36,7 @@
"dependencies": {
"bootstrap": "^5.2.0",
"preact": "^10.10.1",
"tlock-js": "^0.6.1",
"tlock-js": "^0.9.0",
"yup": "^0.32.11"
},
"browserslist": [
Expand Down
18 changes: 13 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ import {VulnerabilityReportEncrypt} from "./views/VulnerabilityReportEncrypt"
import {MultiDecrypt} from "./views/MultiDecrypt"
import {Dropdown, DropdownItem} from "./components/Dropdown"

export type Network = "mainnet" | "testnet"
export type Network = "quicknet" | "fastnet" | "testnet-unchained-3s" | "quicknet-t"
const networks: Array<DropdownItem<Network>> = [
{
label: "Mainnet",
value: "mainnet"
label: "Mainnet (quicknet)",
value: "quicknet"
},
{
label: "Testnet",
value: "testnet"
label: "Mainnet (fastnet - deprecated)",
value: "fastnet"
},
{
label: "Testnet (quicknet-t)",
value: "quicknet-t"
},
{
label: "Testnet (testnet-unchained-3s - deprecated)",
value: "testnet-unchained-3s"
},
]
const App = () => {
Expand Down
36 changes: 31 additions & 5 deletions src/actions/client-utils.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
import {HttpCachingChain, HttpChainClient} from "tlock-js"
import {MAINNET_CHAIN_URL, TESTNET_CHAIN_URL} from "tlock-js/drand/defaults"
import {MAINNET_CHAIN_URL, MAINNET_CHAIN_URL_NON_RFC, TESTNET_CHAIN_URL} from "tlock-js/drand/defaults"

export function mainnet(): HttpChainClient {
export function quicknet(): HttpChainClient {
const clientOpts = {
disableBeaconVerification: false,
noCache: false,
chainVerificationParams: {
chainHash: "dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493",
publicKey: "a0b862a7527fee3a731bcb59280ab6abd62d5c0b6ea03dc4ddf6612fdfc9d01f01c31542541771903475eb1ec6615f8d0df0b8b6dce385811d6dcf8cbefb8759e5e616a3dfd054c928940766d9a5b9db91e3b697e5d70a975181e007f87fca5e"
chainHash: "52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971",
publicKey: "83cf0f2896adee7eb8b5f01fcad3912212c437e0073e911fb90022d3e760183c8c4b450b6a0a6c3ac6a5776a2d1064510d1fec758c921cc22b0e17e63aaf4bcb5ed66304de9cf809bd274ca73bab4af5a6e9c76a4bc09e76eae8991ef5ece45a"
}
}
// passing an empty httpOptions arg to strip the user agent header to stop CORS issues
return new HttpChainClient(new HttpCachingChain(MAINNET_CHAIN_URL, clientOpts), clientOpts, {})
}

export function testnet(): HttpChainClient {
export function fastnet(): HttpChainClient {
const clientOpts = {
disableBeaconVerification: false,
noCache: false,
chainVerificationParams: {
chainHash: "dbd506d6ef76e5f386f41c651dcb808c5bcbd75471cc4eafa3f4df7ad4e4c493",
publicKey: "a0b862a7527fee3a731bcb59280ab6abd62d5c0b6ea03dc4ddf6612fdfc9d01f01c31542541771903475eb1ec6615f8d0df0b8b6dce385811d6dcf8cbefb8759e5e616a3dfd054c928940766d9a5b9db91e3b697e5d70a975181e007f87fca5e"
}
}

return new HttpChainClient(new HttpCachingChain(MAINNET_CHAIN_URL_NON_RFC, clientOpts), clientOpts, {})
}

export function testnetQuicknet(): HttpChainClient {
const clientOpts = {
disableBeaconVerification: false,
noCache: false,
chainVerificationParams: {
chainHash: "cc9c398442737cbd141526600919edd69f1d6f9b4adb67e4d912fbc64341a9a5",
publicKey: "b15b65b46fb29104f6a4b5d1e11a8da6344463973d423661bb0804846a0ecd1ef93c25057f1c0baab2ac53e56c662b66072f6d84ee791a3382bfb055afab1e6a375538d8ffc451104ac971d2dc9b168e2d3246b0be2015969cbaac298f6502da"
}
}

// passing an empty httpOptions arg to strip the user agent header to stop CORS issues
return new HttpChainClient(new HttpCachingChain("https://pl-us.testnet.drand.sh/cc9c398442737cbd141526600919edd69f1d6f9b4adb67e4d912fbc64341a9a5", clientOpts), clientOpts, {})
}
export function testnetUnchained(): HttpChainClient {
const clientOpts = {
disableBeaconVerification: false,
noCache: false,
Expand Down
26 changes: 19 additions & 7 deletions src/actions/decrypt-multi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {HttpChainClient, timelockDecrypt} from "tlock-js"
import {vulnerabilityDecryptionSchema} from "../schema/vulnerability-encryption-schema"
import {Network} from "../App"
import {mainnet, testnet} from "./client-utils"
import {fastnet, quicknet, testnetQuicknet, testnetUnchained} from "./client-utils"

type TextContent = { type: "text", value: string }
type VulnerabilityReportContent = {
Expand All @@ -17,12 +17,24 @@ export type DecryptionContent = TextContent | VulnerabilityReportContent

export async function decryptMulti(network: Network, ciphertext: string): Promise<DecryptionContent> {
let client: HttpChainClient
if (network === "mainnet") {
client = mainnet()
} else {
client = testnet()
switch (network) {
case "quicknet":
client = quicknet()
break
case "fastnet":
client = fastnet()
break
case "quicknet-t":
client = testnetQuicknet()
break
case "testnet-unchained-3s":
client = testnetUnchained()
break
default: throw Error("unknown network")
}
const plaintext = await timelockDecrypt(ciphertext, client)

const buf = await timelockDecrypt(ciphertext, client)
const plaintext = buf.toString()

if (await vulnerabilityDecryptionSchema.isValid(plaintext)) {
const vulnReport = await vulnerabilityDecryptionSchema.validate(plaintext)
Expand All @@ -46,6 +58,6 @@ export async function decryptMulti(network: Network, ciphertext: string): Promis

return {
type: "text",
value: plaintext
value: plaintext.toString()
}
}
22 changes: 16 additions & 6 deletions src/actions/encrypt-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@ import * as yup from "yup"
import {HttpChainClient, roundAt, timelockDecrypt, timelockEncrypt} from "tlock-js"
import {textEncryptionSchema} from "../schema/text-encryption-schema"
import {Network} from "../App"
import {mainnet, testnet} from "./client-utils"
import {fastnet, quicknet, testnetQuicknet, testnetUnchained} from "./client-utils"

export type CompletedWebForm = yup.InferType<typeof textEncryptionSchema>

export async function encryptedOrDecryptedFormData(network: Network, form: unknown): Promise<CompletedWebForm> {
const partialWebForm = await textEncryptionSchema.validate(form)
let client: HttpChainClient
if (network === "mainnet") {
client = mainnet()
} else {
client = testnet()
switch (network) {
case "quicknet":
client = quicknet()
break
case "fastnet":
client = fastnet()
break
case "quicknet-t":
client = testnetQuicknet()
break
case "testnet-unchained-3s":
client = testnetUnchained()
break
default: throw Error("unknown network")
}

if (partialWebForm.plaintext) {
Expand All @@ -39,7 +49,7 @@ async function encrypt(client: HttpChainClient, plaintext: string, decryptionTim
async function decrypt(client: HttpChainClient, ciphertext: string, decryptionTime: number): Promise<CompletedWebForm> {
const plaintext = await timelockDecrypt(ciphertext, client)
return {
plaintext,
plaintext: plaintext.toString(),
decryptionTime,
ciphertext,
}
Expand Down
29 changes: 20 additions & 9 deletions src/actions/encrypt-vulnerability-report.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {vulnerabilityEncryptionSchema} from "../schema/vulnerability-encryption-schema"
import {roundAt, timelockEncrypt} from "tlock-js"
import {MAINNET_CHAIN_INFO, TESTNET_CHAIN_INFO} from "tlock-js/drand/defaults"
import {HttpChainClient, roundAt, timelockEncrypt} from "tlock-js"
import {fileAsBuffer} from "./file-utils"
import {Network} from "../App"
import {mainnet, testnet} from "./client-utils"
import {fastnet, quicknet, testnetQuicknet, testnetUnchained} from "./client-utils"

export async function encryptVulnerabilityReport(network: Network, form: unknown): Promise<string> {
const input = await vulnerabilityEncryptionSchema.validate(form)
Expand All @@ -30,11 +29,23 @@ export async function encryptVulnerabilityReport(network: Network, form: unknown
file
})

if (network === "mainnet") {
const roundNumber = roundAt(input.decryptionTime, MAINNET_CHAIN_INFO)
return await timelockEncrypt(roundNumber, Buffer.from(plaintext), mainnet())
} else {
const roundNumber = roundAt(input.decryptionTime, TESTNET_CHAIN_INFO)
return await timelockEncrypt(roundNumber, Buffer.from(plaintext), testnet())
let client: HttpChainClient
switch (network) {
case "quicknet":
client = quicknet()
break
case "fastnet":
client = fastnet()
break
case "quicknet-t":
client = testnetQuicknet()
break
case "testnet-unchained-3s":
client = testnetUnchained()
break
default:
throw Error("unknown network")
}
const roundNumber = roundAt(input.decryptionTime, await client.chain().info())
return await timelockEncrypt(roundNumber, Buffer.from(plaintext), client)
}
24 changes: 24 additions & 0 deletions test/actions/beacon.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {bls12_381} from "@noble/curves/bls12-381"
import {sha256} from "@noble/hashes/sha256"
import {expect} from "chai"

// the new noble curves lib
describe("beacon", () => {
it("should be compatible with the new noble curves lib", async () => {
const beacon = {
round: 19398585,
randomness: '9ff50f8dd76a4c6a7e11f075b457bb95deb680c9a51eda6e81e471b4ede7869b',
signature: 'af8695d75961338f379158022f24526203289a37ce94f29067f10b9f18db74f88ad189d0576d54f4b9b07f03195a0900191029ff7ff9a7db955ee381813a1f2875edc4a90151ffaf2e40f4106b9167b0a02e9507881e6b0028f09df236f838d2'
}

const publicKey = "8200fc249deb0148eb918d6e213980c5d01acd7fc251900d9260136da3b54836ce125172399ddc69c4e3e11429b62c11"
const msg = hashedRoundNumber(beacon.round)
expect(bls12_381.verify(beacon.signature, msg, publicKey)).to.be.true
})
})

function hashedRoundNumber(round: number) {
const roundNumberBuffer = Buffer.alloc(8);
roundNumberBuffer.writeBigUInt64BE(BigInt(round));
return sha256(roundNumberBuffer)
}
9 changes: 5 additions & 4 deletions test/actions/decrypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import {encryptedOrDecryptedFormData} from "../../src/actions/encrypt-text"
import "isomorphic-fetch"

describe("multi-decryption", () => {
const network = "testnet-unchained-3s"
it("correctly identifies vulnerability reports", async () => {
const report = {
title: "something",
description: "cool",
cve: "1234",
decryptionTime: Date.now()
}
const encryptedVulnerabilityReport = await encryptVulnerabilityReport("testnet", report)
const result = await decryptMulti("testnet", encryptedVulnerabilityReport)
const encryptedVulnerabilityReport = await encryptVulnerabilityReport(network, report)
const result = await decryptMulti(network, encryptedVulnerabilityReport)

expect(result.type).to.equal("vulnerability_report")
assert(result.type === "vulnerability_report")
Expand All @@ -30,12 +31,12 @@ describe("multi-decryption", () => {
plaintext: "blah",
decryptionTime: Date.now()
}
const {ciphertext} = await encryptedOrDecryptedFormData("testnet", encryptionForm)
const {ciphertext} = await encryptedOrDecryptedFormData(network, encryptionForm)
if (!ciphertext) {
throw Error("Expected a ciphertext!")
}

const result = await decryptMulti("testnet", ciphertext)
const result = await decryptMulti(network, ciphertext)

expect(result.type).to.equal("text")
expect(result.value).to.equal(encryptionForm.plaintext)
Expand Down
Loading