@covenance/dlc
Version:
Crypto and Bitcoin functions for Covenance DLC implementation
82 lines (68 loc) • 3.28 kB
text/typescript
import { expect } from 'chai';
import { Point, utils } from '../../src/crypto/secp256k1';
import { createAdaptorSig, adaptSig, verifyAdaptorSig } from '../../src/crypto/counterparty';
import { verifySig } from '../../src/crypto/general';
import { commitToEvent, attestEventOutcome } from '../../src/crypto/oracle';
import { PrivKey, EventOutcomeHash, Sighash } from '../../src/crypto/types';
import { bytesToHex } from '../../src/utils';
describe('Counterparty Functions', () => {
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 create valid adaptor signatures', async () => {
// Get oracle commitment
const { signaturePoints, nonce } = await commitToEvent(eventOutcomeHashes, oraclePubKey);
expect(nonce).to.be.a('bigint');
// Create adaptor signatures for both parties
const aliceAdaptorSig = await createAdaptorSig(alicePrivKey, signaturePoints[0], cetSighash);
const bobAdaptorSig = await createAdaptorSig(bobPrivKey, signaturePoints[0], cetSighash);
// Verify both adaptor signatures
const isAliceValid = await verifyAdaptorSig(aliceAdaptorSig, alicePubKey, cetSighash, signaturePoints[0]);
const isBobValid = await verifyAdaptorSig(bobAdaptorSig, bobPubKey, cetSighash, signaturePoints[0]);
expect(isAliceValid).to.be.true;
expect(isBobValid).to.be.true;
});
it('should adapt signatures correctly', async () => {
// Get oracle commitment and attestation
const { signaturePoints, nonce } = await commitToEvent(eventOutcomeHashes, oraclePubKey);
const oracleSig = await attestEventOutcome(oraclePrivKey, nonce, eventOutcomeHashes[0]);
// Create and adapt Alice's signature
const aliceAdaptorSig = await createAdaptorSig(alicePrivKey, signaturePoints[0], cetSighash);
const aliceSig = adaptSig(aliceAdaptorSig, oracleSig.s);
// Verify adapted signature
const isValid = await verifySig(aliceSig, alicePubKey, cetSighash);
expect(isValid).to.be.true;
});
it('should adapt signatures correctly - scalar value only', async () => {
const s = utils.randomPrivateKey();
const sBigInt = BigInt('0x' + bytesToHex(s));
const signaturePoint = Point.fromPrivateKey(s);
// Create and adapt Alice's signature
const aliceAdaptorSig = await createAdaptorSig(alicePrivKey, signaturePoint, cetSighash);
const aliceSig = adaptSig(aliceAdaptorSig, sBigInt);
// Verify adapted signature
const isValid = await verifySig(aliceSig, alicePubKey, cetSighash);
expect(isValid).to.be.true;
});
});