@covenance/dlc
Version:
Crypto and Bitcoin functions for Covenance DLC implementation
92 lines (82 loc) • 2.77 kB
text/typescript
import { expect } from 'chai';
import { Point, utils } from '../../src/crypto/secp256k1';
import {
commitToEvent,
attestEventOutcome,
createAdaptorSig,
adaptSig,
verifyAdaptorSig,
verifySig
} from '../../src';
import { PrivKey, EventOutcomeHash, Sighash } from '../../src/crypto/types';
describe('End-to-End Test', () => {
let oraclePrivKey: PrivKey;
let oraclePubKey: Point;
let alicePrivKey: PrivKey;
let alicePubKey: Point;
let bobPrivKey: PrivKey;
let bobPubKey: Point;
let eventOutcomeHashes: EventOutcomeHash[];
let cetSighash: Sighash;
before(async () => {
// Generate test keys
oraclePrivKey = utils.randomPrivateKey();
oraclePubKey = Point.fromPrivateKey(oraclePrivKey);
alicePrivKey = utils.randomPrivateKey();
alicePubKey = Point.fromPrivateKey(alicePrivKey);
bobPrivKey = utils.randomPrivateKey();
bobPubKey = Point.fromPrivateKey(bobPrivKey);
// Generate test event outcome hashes
eventOutcomeHashes = [
new Uint8Array([1, 2, 3]),
new Uint8Array([4, 5, 6])
];
// Generate test CET sighash
cetSighash = new Uint8Array([7, 8, 9]);
});
it('should complete a full DLC flow', async () => {
// 1. Oracle commits to event
const { signaturePoints, nonce } = await commitToEvent(eventOutcomeHashes, oraclePubKey);
const outcomeIndex = 0;
// 2. Alice and Bob create adaptor signatures
const aliceAdaptorSig = await createAdaptorSig(
alicePrivKey,
signaturePoints[outcomeIndex],
cetSighash
);
const bobAdaptorSig = await createAdaptorSig(
bobPrivKey,
signaturePoints[outcomeIndex],
cetSighash
);
// 3. Verify each other's adaptor signatures
const isAliceAdaptorValid = await verifyAdaptorSig(
aliceAdaptorSig,
alicePubKey,
cetSighash,
signaturePoints[outcomeIndex]
);
const isBobAdaptorValid = await verifyAdaptorSig(
bobAdaptorSig,
bobPubKey,
cetSighash,
signaturePoints[outcomeIndex]
);
expect(isAliceAdaptorValid).to.be.true;
expect(isBobAdaptorValid).to.be.true;
// 4. Oracle attests to the outcome
const oracleSig = await attestEventOutcome(
oraclePrivKey,
nonce,
eventOutcomeHashes[outcomeIndex]
);
// 5. Alice and Bob adapt their signatures
const aliceFinalSig = adaptSig(aliceAdaptorSig, oracleSig.s);
const bobFinalSig = adaptSig(bobAdaptorSig, oracleSig.s);
// 6. Verify final signatures
const isAliceFinalValid = await verifySig(aliceFinalSig, alicePubKey, cetSighash);
const isBobFinalValid = await verifySig(bobFinalSig, bobPubKey, cetSighash);
expect(isAliceFinalValid).to.be.true;
expect(isBobFinalValid).to.be.true;
});
});