Skip to content

Better support for ECDSA signatures in ABI arguments #319

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

Open
wants to merge 1 commit 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
16 changes: 10 additions & 6 deletions packages/cashscript/src/Argument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,20 @@ export function encodeFunctionArgument(argument: FunctionArgument, typeStr: stri
throw Error(`Value for type ${type} should be a Uint8Array or hex string`);
}

// Redefine SIG as a bytes65 so it is included in the size checks below
// Note that ONLY Schnorr signatures are accepted
// Redefine SIG as a bytes65 (Schnorr) or bytes71, bytes72 (ECDSA)
if (type === PrimitiveType.SIG && argument.byteLength !== 0) {
type = new BytesType(65);
if (![65, 71, 72].includes(argument.byteLength)) {
throw new TypeError(`bytes${argument.byteLength}`, type);
}
type = new BytesType(argument.byteLength);
}

// Redefine DATASIG as a bytes64 so it is included in the size checks below
// Note that ONLY Schnorr signatures are accepted
// Redefine DATASIG as a bytes64 (Schnorr) or bytes70 (ECDSA) so it is included in the size checks below
if (type === PrimitiveType.DATASIG && argument.byteLength !== 0) {
type = new BytesType(64);
if (![64, 70].includes(argument.byteLength)) {
throw new TypeError(`bytes${argument.byteLength}`, type);
}
type = new BytesType(argument.byteLength);
}

// Bounded bytes types require a correctly sized argument
Expand Down
23 changes: 23 additions & 0 deletions packages/cashscript/test/e2e/HodlVault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
ElectrumNetworkProvider,
Network,
TransactionBuilder,
SignatureAlgorithm,
HashType,
} from '../../src/index.js';
import {
alicePriv,
Expand Down Expand Up @@ -94,5 +96,26 @@ describe('HodlVault', () => {
const txOutputs = getTxOutputs(tx);
expect(txOutputs).toEqual(expect.arrayContaining([{ to, amount }]));
});

it('should succeed when price is high enough, ECDSA sig and datasig', async () => {
// given
const message = oracle.createMessage(100000n, 30000n);
const oracleSig = oracle.signMessage(message, SignatureAlgorithm.ECDSA);
const to = hodlVault.address;
const amount = 10000n;
const { utxos, changeAmount } = gatherUtxos(await hodlVault.getUtxos(), { amount, fee: 2000n });

// when
const tx = await new TransactionBuilder({ provider })
.addInputs(utxos, hodlVault.unlock.spend(new SignatureTemplate(alicePriv, HashType.SIGHASH_ALL, SignatureAlgorithm.ECDSA), oracleSig, message))
.addOutput({ to: to, amount: amount })
.addOutput({ to: to, amount: changeAmount })
.setLocktime(100_000)
.send();

// then
const txOutputs = getTxOutputs(tx);
expect(txOutputs).toEqual(expect.arrayContaining([{ to, amount }]));
});
});
});
8 changes: 6 additions & 2 deletions packages/cashscript/test/fixture/PriceOracle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { padMinimallyEncodedVmNumber, flattenBinArray, secp256k1 } from '@bitauth/libauth';
import { encodeInt, sha256 } from '@cashscript/utils';
import { SignatureAlgorithm } from '../../src/index.js';

export class PriceOracle {
constructor(public privateKey: Uint8Array) {}
Expand All @@ -12,8 +13,11 @@ export class PriceOracle {
return flattenBinArray([encodedBlockHeight, encodedBchUsdPrice]);
}

signMessage(message: Uint8Array): Uint8Array {
const signature = secp256k1.signMessageHashSchnorr(this.privateKey, sha256(message));
signMessage(message: Uint8Array, signatureAlgorithm: SignatureAlgorithm = SignatureAlgorithm.SCHNORR): Uint8Array {
const signature = signatureAlgorithm === SignatureAlgorithm.SCHNORR ?
secp256k1.signMessageHashSchnorr(this.privateKey, sha256(message)) :
secp256k1.signMessageHashDER(this.privateKey, sha256(message));

if (typeof signature === 'string') throw new Error();
return signature;
}
Expand Down