UNPKG

@covenance/dlc

Version:

Crypto and Bitcoin functions for Covenance DLC implementation

51 lines (41 loc) 1.77 kB
import { expect } from 'chai'; import { Point, utils } from '../../src/crypto/secp256k1'; import { commitToEvent, attestEventOutcome } from '../../src/crypto/oracle'; import { verifySig } from '../../src/crypto/general'; import { PrivKey, EventOutcomeHash } from '../../src/crypto/types'; describe('Oracle Functions', () => { let oraclePrivKey: PrivKey; let oraclePubKey: Point; let eventOutcomeHashes: EventOutcomeHash[]; before(async () => { // Generate test keys oraclePrivKey = utils.randomPrivateKey(); oraclePubKey = Point.fromPrivateKey(oraclePrivKey); // Generate test event outcome hashes eventOutcomeHashes = [ new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6]) ]; }); it('should create valid event commitments', async () => { const { signaturePoints, nonce } = await commitToEvent(eventOutcomeHashes, oraclePubKey); // Verify we got the right number of signature points expect(signaturePoints.length).to.equal(eventOutcomeHashes.length); // Verify each signature point is valid signaturePoints.forEach(point => { expect(point).to.be.instanceOf(Point); }); // Verify nonce is a valid bigint expect(typeof nonce).to.equal('bigint'); expect(nonce > BigInt(0)).to.be.true; }); it('should create valid event attestations', async () => { // Get oracle commitment const { nonce } = await commitToEvent(eventOutcomeHashes, oraclePubKey); // Create attestation for first outcome const sig = await attestEventOutcome(oraclePrivKey, nonce, eventOutcomeHashes[0]); // Verify the signature const isValid = await verifySig(sig, oraclePubKey, eventOutcomeHashes[0], new Uint8Array(0)); expect(isValid).to.be.true; }); });