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

allow round number to not be included #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
end_of_line = lf
charset = utf-8
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 22 additions & 6 deletions src/drand/timelock-decrypter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,40 @@ import {PointG1, PointG2} from "@noble/bls12-381"
import * as ibe from "../crypto/ibe"
import {Ciphertext} from "../crypto/ibe"

export function createTimelockDecrypter(network: DrandClient) {
return async (recipients: Array<Stanza>): Promise<Uint8Array> => {
export function createTimelockDecrypter(network: DrandClient, roundNumberForAll?: number) {
return async (recipients: Array<Stanza>, roundNumber?: number): Promise<Uint8Array> => {
if (recipients.length !== 1) {
throw Error("Timelock only expects a single stanza!")
}

const {type, args, body} = recipients[0]
const { type, args, body } = recipients[0]

roundNumber = roundNumber || roundNumberForAll

if (type !== "tlock") {
throw Error(`Timelock expects the type of the stanza to be "tlock`)
}

if (args.length !== 2) {
throw Error(`Timelock stanza expected 2 args: roundNumber and chainHash. Only received ${args.length}`)
let chainHash: string
if (args.length === 1) {
if (roundNumber === undefined) {
throw Error(
`Timelock stanza was provided with only one argument (chainHash), but no roundNumber was specified`
)
}
chainHash = args[0];
} else if (args.length === 2) {
roundNumber = parseRoundNumber(args)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
chainHash = args[1];
} else {
throw Error(
`Timelock stanza expected 1 (chainHash) or 2 args (roundNumber and chainHash). Only received ${args.length}`
)
}

// should probably verify chain hash here too
const beacon = await network.get(parseRoundNumber(args))
const beacon = await network.get(roundNumber)
console.log(`beacon received: ${JSON.stringify(beacon)}`)

const g2 = PointG2.fromHex(beacon.signature)
Expand Down
4 changes: 2 additions & 2 deletions src/drand/timelock-encrypter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {DrandClient, DrandNetworkInfo} from "./drand-client"
import {Stanza} from "../age/age-encrypt-decrypt"
import {Ciphertext} from "../crypto/ibe"

export function createTimelockEncrypter(chainInfo: DrandNetworkInfo, network: DrandClient, roundNumber: number) {
export function createTimelockEncrypter(chainInfo: DrandNetworkInfo, network: DrandClient, roundNumber: number, options?: {doNotIncludeRoundNumber?: boolean}) {
if (roundNumber < 1) {
throw Error("You cannot encrypt for a roundNumber less than 1 (genesis = 0)")
}
Expand All @@ -17,7 +17,7 @@ export function createTimelockEncrypter(chainInfo: DrandNetworkInfo, network: Dr

return [{
type: "tlock",
args: [`${roundNumber}`, chainInfo.chainHash],
args: options?.doNotIncludeRoundNumber ? [chainInfo.chainHash] : [`${roundNumber}`, chainInfo.chainHash],
body: serialisedCiphertext(ciphertext)
}]
}
Expand Down
22 changes: 18 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,33 @@ import {createTimelockDecrypter} from "./drand/timelock-decrypter"
export async function timelockEncrypt(
roundNumber: number,
payload: Buffer,
drandHttpClient: DrandClient = DrandHttpClient.createFetchClient(defaultClientInfo),
options?: {
drandHttpClient?: DrandClient,
doNotIncludeRoundNumber?: boolean
}
): Promise<string> {
let drandHttpClient = options?.drandHttpClient;
if (!drandHttpClient) {
drandHttpClient = DrandHttpClient.createFetchClient(defaultClientInfo)
}
const chainInfo = await drandHttpClient.info()
const timelockEncrypter = createTimelockEncrypter(chainInfo, drandHttpClient, roundNumber)
const timelockEncrypter = createTimelockEncrypter(chainInfo, drandHttpClient, roundNumber, {doNotIncludeRoundNumber: options?.doNotIncludeRoundNumber})
const agePayload = await encryptAge(payload, timelockEncrypter)
return encodeArmor(agePayload)
}

export async function timelockDecrypt(
ciphertext: string,
ciphertext: string | {ciphertext: string; roundNumber?: number},
drandHttpClient: DrandClient = DrandHttpClient.createFetchClient(defaultClientInfo)
): Promise<string> {
const timelockDecrypter = createTimelockDecrypter(drandHttpClient)

let roundNumber;
if (typeof ciphertext !== "string") {
roundNumber = ciphertext.roundNumber
ciphertext = ciphertext.ciphertext
}

const timelockDecrypter = createTimelockDecrypter(drandHttpClient, roundNumber)

let cipher = ciphertext
if (isProbablyArmored(ciphertext)) {
Expand Down