Skip to content
Closed
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
2 changes: 2 additions & 0 deletions packages/suite-build/configs/base.webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ const config: webpack.Configuration = {
]
: []),
],
// For tiny-secp256k1
experiments: { asyncWebAssembly: true },
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it required by tiny-secp256k1 or optional? if required then this should be added to all the projects which are using utxo-lib (connect, connect-iframe, connect-popup, connect-explorer)

i know that we build some other wasm files (cardano?), will this have effect on them as well?

};

export default config;
2 changes: 1 addition & 1 deletion packages/utxo-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"create-hmac": "^1.1.7",
"int64-buffer": "^1.0.1",
"pushdata-bitcoin": "^1.0.1",
"tiny-secp256k1": "^1.1.6",
"tiny-secp256k1": "^2.2.3",
"typeforce": "^1.18.0",
"varuint-bitcoin": "^1.1.2",
"wif": "^4.0.0"
Expand Down
25 changes: 16 additions & 9 deletions packages/utxo-lib/src/bip32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
// - `identifier` method is using different hashing for Decred.
// - `fromBase58` and `toBase58` methods are using additional "network" param in bs58check.encode/decode (Decred support).

import ecc from 'tiny-secp256k1';
import * as ecc from 'tiny-secp256k1';
import wif from 'wif';
import { typeforce } from './types/typeforce';
import * as bs58check from './bs58check';
import * as crypto from './crypto';
import { bitcoin as BITCOIN, isNetworkType } from './networks';
import type { Network } from './networks';
import { uin8ArrayToBuffer } from './uin8ArrayToBuffer';

const UINT256_TYPE = typeforce.BufferN(32);
const NETWORK_TYPE = typeforce.compile({
Expand Down Expand Up @@ -131,13 +132,15 @@ class BIP32 implements BIP32Interface {
}

get publicKey(): Buffer {
if (this.__Q === undefined) this.__Q = ecc.pointFromScalar(this.__D, true);
if (this.__Q === undefined && this.__D !== undefined) {
this.__Q = uin8ArrayToBuffer(ecc.pointFromScalar(this.__D, true)) ?? undefined;
}

return this.__Q!;
}

get privateKey(): Buffer | undefined {
return this.__D;
return uin8ArrayToBuffer(this.__D) ?? undefined;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed? this.__D is defined as Buffer and should be a Buffer.

this comment applies for all the usage of uin8ArrayToBuffer in this file

}

get identifier(): Buffer {
Expand Down Expand Up @@ -248,14 +251,18 @@ class BIP32 implements BIP32Interface {
// Private parent key -> private child key
let hd: BIP32Interface;
if (!this.isNeutered()) {
if (this.privateKey === undefined) {
throw Error('PrivateKey is undefined');
}

// ki = parse256(IL) + kpar (mod n)
const ki = ecc.privateAdd(this.privateKey, IL);
const ki = ecc.privateAdd(uin8ArrayToBuffer(this.privateKey), IL);

// In case ki == 0, proceed with the next value for i
if (ki == null) return this.derive(index + 1);

hd = fromPrivateKeyLocal(
ki,
uin8ArrayToBuffer(ki),
IR,
this.network,
this.depth + 1,
Expand All @@ -273,7 +280,7 @@ class BIP32 implements BIP32Interface {
if (Ki === null) return this.derive(index + 1);

hd = fromPublicKeyLocal(
Ki,
uin8ArrayToBuffer(Ki),
IR,
this.network,
this.depth + 1,
Expand Down Expand Up @@ -319,7 +326,7 @@ class BIP32 implements BIP32Interface {
if (!this.privateKey) throw new Error('Missing private key');
if (lowR === undefined) lowR = this.lowR;
if (lowR === false) {
return ecc.sign(hash, this.privateKey);
return uin8ArrayToBuffer(ecc.sign(hash, this.privateKey));
}
let sig = ecc.sign(hash, this.privateKey);
const extraData = Buffer.alloc(32, 0);
Expand All @@ -329,10 +336,10 @@ class BIP32 implements BIP32Interface {
while (sig[0] > 0x7f) {
counter++;
extraData.writeUIntLE(counter, 0, 6);
sig = ecc.signWithEntropy(hash, this.privateKey, extraData);
sig = ecc.sign(hash, this.privateKey, extraData);
}

return sig;
return uin8ArrayToBuffer(sig);
}

verify(hash: Buffer, signature: Buffer): boolean {
Expand Down
2 changes: 0 additions & 2 deletions packages/utxo-lib/src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ declare module 'bitcoin-ops';

declare module 'minimaldata';

declare module 'tiny-secp256k1';

declare module 'pushdata-bitcoin' {
function encodingLength(len: number): number;
function encode(buffer: Buffer, number: number, offset: number): number;
Expand Down
2 changes: 1 addition & 1 deletion packages/utxo-lib/src/payments/p2ms.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// upstream: https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/ts_src/payments/p2ms.ts

import ecc from 'tiny-secp256k1';
import * as ecc from 'tiny-secp256k1';
import { bitcoin as BITCOIN_NETWORK } from '../networks';
import * as bscript from '../script';
import * as lazy from './lazy';
Expand Down
11 changes: 9 additions & 2 deletions packages/utxo-lib/src/payments/p2pk.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/ts_src/payments/p2pk.ts

import ecc from 'tiny-secp256k1';
import * as ecc from 'tiny-secp256k1';
import { bitcoin as BITCOIN_NETWORK } from '../networks';
import * as bscript from '../script';
import * as lazy from './lazy';
import { Payment, PaymentOpts, StackFunction, typeforce } from '../types';
import { uin8ArrayToBuffer } from '../uin8ArrayToBuffer';

const { OPS } = bscript;

Expand Down Expand Up @@ -64,7 +65,13 @@ export function p2pk(a: Payment, opts?: PaymentOpts): Payment {
if (a.output) {
if (a.output[a.output.length - 1] !== OPS.OP_CHECKSIG)
throw new TypeError('Output is invalid');
if (!ecc.isPoint(o.pubkey)) throw new TypeError('Output pubkey is invalid');

if (o.pubkey === undefined) {
throw new Error('pubkey is undefined');
}

if (!ecc.isPoint(uin8ArrayToBuffer(o.pubkey)))
throw new TypeError('Output pubkey is invalid');
if (a.pubkey && !a.pubkey.equals(o.pubkey!)) throw new TypeError('Pubkey mismatch');
}

Expand Down
2 changes: 1 addition & 1 deletion packages/utxo-lib/src/payments/p2pkh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// - using bs58check.decodeAddress instead of bs58check.decode
// - using bs58check.encodeAddress instead of bs58check.encode

import ecc from 'tiny-secp256k1';
import * as ecc from 'tiny-secp256k1';
import * as bs58check from '../bs58check';
import * as bcrypto from '../crypto';
import { bitcoin as BITCOIN_NETWORK } from '../networks';
Expand Down
2 changes: 1 addition & 1 deletion packages/utxo-lib/src/payments/p2tr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SegWit version 1 P2TR output type for Taproot defined in
// https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki

import ecc from 'tiny-secp256k1';
import * as ecc from 'tiny-secp256k1';
import { bech32m } from 'bech32';
import { bitcoin as BITCOIN_NETWORK } from '../networks';
import * as bcrypto from '../crypto';
Expand Down
2 changes: 1 addition & 1 deletion packages/utxo-lib/src/payments/p2wpkh.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// upstream: https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/ts_src/payments/p2wpkh.ts

import ecc from 'tiny-secp256k1';
import * as ecc from 'tiny-secp256k1';
import { bech32 } from 'bech32';
import * as bcrypto from '../crypto';
import { bitcoin as BITCOIN_NETWORK } from '../networks';
Expand Down
2 changes: 1 addition & 1 deletion packages/utxo-lib/src/payments/p2wsh.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// upstream: https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/ts_src/payments/p2wsh.ts

import ecc from 'tiny-secp256k1';
import * as ecc from 'tiny-secp256k1';
import { bech32 } from 'bech32';
import * as bcrypto from '../crypto';
import { bitcoin as BITCOIN_NETWORK } from '../networks';
Expand Down
2 changes: 1 addition & 1 deletion packages/utxo-lib/src/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import bip66 from 'bip66';
import pushdata from 'pushdata-bitcoin';
import ecc from 'tiny-secp256k1';
import * as ecc from 'tiny-secp256k1';
import * as scriptNumber from './scriptNumber';
import * as scriptSignature from './scriptSignature';
import { OPS, REVERSE_OPS } from './ops';
Expand Down
13 changes: 13 additions & 0 deletions packages/utxo-lib/src/uin8ArrayToBuffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const uin8ArrayToBuffer = <T extends Uint8Array | null | undefined>(
array: T,
): T extends Uint8Array ? Buffer : T extends null ? null : undefined =>
// eslint-disable-next-line no-nested-ternary
(array === null
? null
: array === undefined
? undefined
: Buffer.from(array)) as T extends Uint8Array
? Buffer
: T extends null
? null
: undefined;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note te me - to be discussed

2 changes: 1 addition & 1 deletion suite-native/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@
"typescript": "^5.3.3"
},
"private": true
}
}
26 changes: 14 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11287,7 +11287,7 @@ __metadata:
int64-buffer: "npm:^1.0.1"
minimaldata: "npm:^1.0.2"
pushdata-bitcoin: "npm:^1.0.1"
tiny-secp256k1: "npm:^1.1.6"
tiny-secp256k1: "npm:^2.2.3"
tsx: "npm:^4.7.0"
typeforce: "npm:^1.18.0"
varuint-bitcoin: "npm:^1.1.2"
Expand Down Expand Up @@ -30025,7 +30025,7 @@ __metadata:
languageName: node
linkType: hard

"nan@npm:^2.13.2, nan@npm:^2.14.0":
"nan@npm:^2.14.0":
version: 2.18.0
resolution: "nan@npm:2.18.0"
dependencies:
Expand Down Expand Up @@ -37727,17 +37727,12 @@ __metadata:
languageName: node
linkType: hard

"tiny-secp256k1@npm:^1.1.6":
version: 1.1.6
resolution: "tiny-secp256k1@npm:1.1.6"
"tiny-secp256k1@npm:^2.2.3":
version: 2.2.3
resolution: "tiny-secp256k1@npm:2.2.3"
dependencies:
bindings: "npm:^1.3.0"
bn.js: "npm:^4.11.8"
create-hmac: "npm:^1.1.7"
elliptic: "npm:^6.4.0"
nan: "npm:^2.13.2"
node-gyp: "npm:latest"
checksum: 10/e25b45ba3e95a332d21556617f2049f7ef065c737dd979e197856f35ef0bd9ee5608872f0068e5cddf0b50919b43b8eaca86bdea560f623e63ec5c560934ca9b
uint8array-tools: "npm:0.0.7"
checksum: 10/9975134c5c86587bb0e9886dd2e66a7a9b79931cb2c3e32b24bcfc2096216781828bb7c5482c0fa18a632ce1b907f6cc86bf12238704ec9cd43d1e81d8af502e
languageName: node
linkType: hard

Expand Down Expand Up @@ -38554,6 +38549,13 @@ __metadata:
languageName: node
linkType: hard

"uint8array-tools@npm:0.0.7":
version: 0.0.7
resolution: "uint8array-tools@npm:0.0.7"
checksum: 10/6ffc45c7d2136757d63c6e556eb8345f908948618a9de37c805fec1249d989c265187b3fbef6cffc4ce5129083204829025b3c58800a0f24c8548e243d42ba13
languageName: node
linkType: hard

"ultron@npm:~1.1.0":
version: 1.1.1
resolution: "ultron@npm:1.1.1"
Expand Down