Skip to content

Commit

Permalink
fix uses of deprecated functions + add eslint-plugin-deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
antonilol committed Nov 7, 2024
1 parent 49adbcf commit a6c12cc
Show file tree
Hide file tree
Showing 15 changed files with 601 additions and 254 deletions.
7 changes: 6 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "prettier"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
"plugin:deprecation/recommended"
],
"rules": {
"@typescript-eslint/no-namespace": 0,
"@typescript-eslint/no-unused-vars": 0,
Expand Down
10 changes: 5 additions & 5 deletions addresses.js

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

10 changes: 5 additions & 5 deletions addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function desc(t: string, data: Buffer | string): string {

/** pub is a 64 byte Buffer: 32 bytes x, 32 bytes y */
function rskAddress(pub: Buffer, main: boolean): string {
const addr = new Keccak(256).update(pub).digest().slice(12).toString('hex');
const addr = new Keccak(256).update(pub).digest().subarray(12).toString('hex');
const csumHash = new Keccak(256).update((main ? '30' : '31') + '0x' + addr).digest('hex');
return (
'0x' +
Expand Down Expand Up @@ -179,15 +179,15 @@ Private key descriptors:
if (data.pub) {
console.log(`
Public keys:
X-Only: ${data.pub.slice(1).toString('hex')}
X-Only: ${data.pub.subarray(1).toString('hex')}
Compressed: ${data.pub.toString('hex')}
Uncompressed: ${data.pubu.toString('hex')}
Public key descriptors:
P2PKH: ${desc('pkh(KEY)', data.pub)}
P2WPKH: ${desc('wpkh(KEY)', data.pub)}
P2SH-P2WPKH: ${desc('sh(wpkh(KEY))', data.pub)}
P2TR: ${desc('tr(KEY)', data.pub.slice(1))}`);
P2TR: ${desc('tr(KEY)', data.pub.subarray(1))}`);
}

if (data.pub) {
Expand All @@ -201,8 +201,8 @@ Public Key Hashes:
Testnet P2WPKH (compressed public key): ${bitcoin.address.toBech32(data.pkh, 0, testnet.bech32)}
Hex (compressed public key): ${data.pkh.toString('hex')}
Hex (uncompressed public key): ${data.pkhu.toString('hex')}
RSK Mainnet account: ${rskAddress(data.pubu.slice(1), true)}
RSK Testnet account: ${rskAddress(data.pubu.slice(1), false)}`);
RSK Mainnet account: ${rskAddress(data.pubu.subarray(1), true)}
RSK Testnet account: ${rskAddress(data.pubu.subarray(1), false)}`);
} else if (data.pkh) {
console.log(`
Public Key Hashes:
Expand Down
4 changes: 2 additions & 2 deletions btc.js

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

7 changes: 5 additions & 2 deletions btc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,10 @@ export function tapBranch(branch1: Buffer, branch2: Buffer): Buffer {
}

export function tapTweak(pubkey: Buffer, root?: Buffer): Buffer {
return bitcoin.crypto.taggedHash('TapTweak', root ? Buffer.concat([pubkey.slice(-32), root]) : pubkey.slice(-32));
return bitcoin.crypto.taggedHash(
'TapTweak',
root ? Buffer.concat([pubkey.subarray(-32), root]) : pubkey.subarray(-32),
);
}

export function bip86(ecpair: ECPairInterface): ECPairInterface | undefined {
Expand Down Expand Up @@ -746,7 +749,7 @@ export function createTaprootOutput(
if (!tweaked) {
return;
}
const key = Buffer.from(tweaked).slice(-32);
const key = Buffer.from(tweaked).subarray(-32);
return {
key,
parity: (tweaked[0] & 1) as 0 | 1,
Expand Down
12 changes: 8 additions & 4 deletions ecdh_stealth.js

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

12 changes: 8 additions & 4 deletions ecdh_stealth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class StealthAddress {
}

static checkOneTimeKey(script: Buffer, publicKey: Buffer): 'p2wpkh' | 'p2tr' | undefined {
if (script.equals(bitcoin.script.compile([bitcoin.opcodes.OP_1, publicKey.slice(1)]))) {
if (script.equals(bitcoin.script.compile([bitcoin.opcodes.OP_1, publicKey.subarray(1)]))) {
return 'p2tr';
} else if (script.equals(bitcoin.script.compile([bitcoin.opcodes.OP_0, bitcoin.crypto.hash160(publicKey)]))) {
return 'p2wpkh';
Expand Down Expand Up @@ -136,15 +136,19 @@ export class StealthAddress {
} else if (this.viewPriv) {
const parity = this.spendPub[0] & 1;
return bs58check.encode(
Buffer.concat([Buffer.from([0x28, 0x70, 0x42, 0xb0 | parity]), this.spendPub.slice(1), this.viewPriv]),
Buffer.concat([
Buffer.from([0x28, 0x70, 0x42, 0xb0 | parity]),
this.spendPub.subarray(1),
this.viewPriv,
]),
);
} else {
const parity = ((this.spendPub[0] & 1) << 1) | (this.viewPub[0] & 1);
return bs58check.encode(
Buffer.concat([
Buffer.from([0x28, 0x6f, 0xba, 0x94 | parity]),
this.spendPub.slice(1),
this.viewPub.slice(1),
this.spendPub.subarray(1),
this.viewPub.subarray(1),
]),
);
}
Expand Down
16 changes: 8 additions & 8 deletions p2tr_ptlc.js

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

34 changes: 15 additions & 19 deletions p2tr_ptlc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@ import * as bitcoin from 'bitcoinjs-lib';
import { randomBytes } from 'crypto';
import { ECPairFactory, ECPairInterface } from 'ecpair';
import * as curve from 'tiny-secp256k1';
import {
btc,
cloneBuf,
createTaprootOutput,
ecPrivateMul,
input,
negateIfOddPubkey,
send,
sleep,
tapLeaf,
} from './btc';
import { btc, createTaprootOutput, ecPrivateMul, input, negateIfOddPubkey, send, sleep, tapLeaf } from './btc';

const ECPair = ECPairFactory(curve);

Expand Down Expand Up @@ -73,7 +63,7 @@ class Adaptor {
if (RT[0] !== 2) {
return false;
}
const Pclone = cloneBuf(P);
const Pclone = Buffer.from(P);
Pclone[0] = 0x02;
const eP = curve.pointMultiply(Pclone, bip340Hash(RT, Pclone, m));
// negate to get -e*P, which we add to s*G to get R' = s*G - e*P
Expand All @@ -90,7 +80,7 @@ class Adaptor {
}

public extract(sig: Buffer): Buffer {
return Buffer.from(curve.privateSub(sig.slice(32), this.s));
return Buffer.from(curve.privateSub(sig.subarray(32), this.s));
}
}

Expand All @@ -101,8 +91,8 @@ function lockupAddress(
key1: Buffer,
key2: Buffer,
): ReturnType<typeof createTaprootOutput> & { leafScript: Buffer; first: boolean } {
const xonly1 = key1.slice(-32);
const xonly2 = key2.slice(-32);
const xonly1 = key1.subarray(-32);
const xonly2 = key2.subarray(-32);
const first = xonly1 < xonly2;
const [k1, k2] = first ? [xonly1, xonly2] : [xonly2, xonly1];
const leafScript = bitcoin.script.compile([k1, bitcoin.opcodes.OP_CHECKSIGVERIFY, k2, bitcoin.opcodes.OP_CHECKSIG]);
Expand Down Expand Up @@ -168,7 +158,7 @@ bitcoin-cli -testnet fundrawtransaction ${ltx.toHex()} | jq -r '.hex' | bitcoin-
const txr = new bitcoin.Transaction();
txr.version = 2;
txr.addInput(Buffer.from(ltxf.getId(), 'hex').reverse(), vout, reltimelock);
txr.addOutput(bitcoin.script.compile([bitcoin.opcodes.OP_1, keypair.publicKey.slice(-32)]), input_sat - fee_sat);
txr.addOutput(bitcoin.script.compile([bitcoin.opcodes.OP_1, keypair.publicKey.subarray(-32)]), input_sat - fee_sat);
const sighashr = txr.hashForWitnessV1(0, [lockup.scriptPubKey], [input_sat], hashtype, tapLeaf(lockup.leafScript));
const refsig = Buffer.from(await input('paste the refund sig: '), 'hex');
if (otherKey.verifySchnorr(sighashr, refsig)) {
Expand All @@ -191,7 +181,10 @@ bitcoin-cli -testnet fundrawtransaction ${ltx.toHex()} | jq -r '.hex' | bitcoin-
const txp = new bitcoin.Transaction();
txp.version = 2;
txp.addInput(Buffer.from(ltxf.getId(), 'hex').reverse(), vout);
txp.addOutput(bitcoin.script.compile([bitcoin.opcodes.OP_1, otherKey.publicKey.slice(-32)]), input_sat - fee_sat);
txp.addOutput(
bitcoin.script.compile([bitcoin.opcodes.OP_1, otherKey.publicKey.subarray(-32)]),
input_sat - fee_sat,
);
const sighashp = txp.hashForWitnessV1(0, [lockup.scriptPubKey], [input_sat], hashtype, tapLeaf(lockup.leafScript));

const a = Adaptor.sign(sighashp, keypair, T);
Expand Down Expand Up @@ -239,7 +232,10 @@ async function bob() {
const txr = new bitcoin.Transaction();
txr.version = 2;
txr.addInput(Buffer.from(txid, 'hex').reverse(), parseInt(vout), reltimelock);
txr.addOutput(bitcoin.script.compile([bitcoin.opcodes.OP_1, otherKey.publicKey.slice(-32)]), input_sat - fee_sat);
txr.addOutput(
bitcoin.script.compile([bitcoin.opcodes.OP_1, otherKey.publicKey.subarray(-32)]),
input_sat - fee_sat,
);
const sighashr = txr.hashForWitnessV1(0, [lockup.scriptPubKey], [input_sat], hashtype, tapLeaf(lockup.leafScript));
console.log('refund signature for alice', keypair.signSchnorr(sighashr).toString('hex'));

Expand All @@ -249,7 +245,7 @@ async function bob() {
const txp = new bitcoin.Transaction();
txp.version = 2;
txp.addInput(Buffer.from(txid, 'hex').reverse(), parseInt(vout));
txp.addOutput(bitcoin.script.compile([bitcoin.opcodes.OP_1, keypair.publicKey.slice(-32)]), input_sat - fee_sat);
txp.addOutput(bitcoin.script.compile([bitcoin.opcodes.OP_1, keypair.publicKey.subarray(-32)]), input_sat - fee_sat);
const sighashp = txp.hashForWitnessV1(0, [lockup.scriptPubKey], [input_sat], hashtype, tapLeaf(lockup.leafScript));

const a = Adaptor.deserialize(await input("give me alice's adaptor signature: "));
Expand Down
6 changes: 3 additions & 3 deletions p2tr_tapscript.js

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

8 changes: 4 additions & 4 deletions p2tr_tapscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
bech32toScriptPubKey,
createTaprootOutput,
decodeRawTransaction,
fundAddress,
fundOutputScript,
getnewaddress,
negateIfOddPubkey,
OP_CHECKSIGADD,
Expand All @@ -26,9 +26,9 @@ const ecpair2 = ECPair.makeRandom({ network });

// build taptree
const leaf1script = bitcoin.script.compile([
ecpair2.publicKey.slice(1, 33),
ecpair2.publicKey.subarray(1, 33),
bitcoin.opcodes.OP_CHECKSIG,
ecpair2.publicKey.slice(1, 33),
ecpair2.publicKey.subarray(1, 33),
OP_CHECKSIGADD,
bitcoin.opcodes.OP_2,
bitcoin.opcodes.OP_EQUAL,
Expand All @@ -47,7 +47,7 @@ const input_sat = 1000;

console.log(tr.address);

fundAddress(tr.address, input_sat).then(async outpoint => {
fundOutputScript(tr.scriptPubKey, input_sat).then(async outpoint => {
const tx = new bitcoin.Transaction();

tx.version = 2;
Expand Down
Loading

0 comments on commit a6c12cc

Please sign in to comment.