Skip to content

Commit

Permalink
ZEUS-2636: CLNRest: Destination address not shown in transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloudis committed Dec 16, 2024
1 parent 2bb3052 commit 677ed38
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 6 deletions.
130 changes: 127 additions & 3 deletions backends/CoreLightningRequestHandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import * as bitcoin from 'bitcoinjs-lib';
import ecc from '../zeus_modules/noble_ecc';

import stores from '../stores/Stores';

bitcoin.initEccLib(ecc);

import CLNRest from './CLNRest';

const api = new CLNRest();
Expand Down Expand Up @@ -139,7 +146,122 @@ export const getChainTransactions = async () => {
api.postRequest('/v1/listtransactions'),
api.postRequest('/v1/getinfo')
]);
const [sqlResult, listTxsResult, getinfoResult] = results;
const [sqlResult, listTxsResult, getinfoResult]: any = results;

listTxsResult?.value?.transactions?.forEach((tx: any) => {
const addresses: Array<string> = [];
tx.outputs.forEach((output: any) => {
const nodeInfo = stores?.nodeInfoStore?.nodeInfo;
const { isTestnet, isRegtest } = nodeInfo;

let network = bitcoin.networks.bitcoin;
if (isTestnet) network = bitcoin.networks.testnet;
if (isRegtest) network = bitcoin.networks.regtest;

const scriptPubKeyHex = output.scriptPubKey;

const scriptBuffer = Buffer.from(scriptPubKeyHex, 'hex');

const decodedScript = bitcoin.script.decompile(scriptBuffer);

// Handle P2PKH (Pay-to-PubKey-Hash)
if (
decodedScript &&
decodedScript[0] === bitcoin.opcodes.OP_DUP &&
decodedScript[1] === bitcoin.opcodes.OP_HASH160
) {
try {
const pubKeyHash: any = decodedScript[2];
const { address } = bitcoin.payments.p2pkh({
hash: pubKeyHash,
network
});
if (address) addresses.push(address);
} catch (e) {
console.log('error decoding p2pkh pkscript', e);
}
return;
}

// Handle P2PK (Pay-to-PubKey)
if (
decodedScript &&
decodedScript[1] === bitcoin.opcodes.OP_CHECKSIG
) {
try {
const pubkey: any = decodedScript[0];
const { address } = bitcoin.payments.p2pk({
pubkey,
network
});
if (address) addresses.push(address);
} catch (e) {
console.log('error decoding p2kh pkscript', e);
}
return;
}

// Handle P2SH (Pay-to-Script-Hash)
if (
decodedScript &&
decodedScript[0] === bitcoin.opcodes.OP_HASH160
) {
try {
const scriptHash: any = decodedScript[1];
const { address } = bitcoin.payments.p2sh({
hash: scriptHash,
network
});
if (address) addresses.push(address);
} catch (e) {
console.log('error decoding p2sh pkscript', e);
}
return;
}

// Handle P2WPKH (Pay-to-Witness-PubKey-Hash) - SegWit
if (
decodedScript &&
decodedScript[0] === bitcoin.opcodes.OP_0 &&
decodedScript[1] === 0x14
) {
try {
const pubKeyHash: any = decodedScript[2];
const { address } = bitcoin.payments.p2wpkh({
pubkey: pubKeyHash,
network
});
if (address) addresses.push(address);
} catch (e) {
console.log('error decoding p2wpkh pkscript', e);
}
return;
}

// Handle P2TR (Pay-to-Taproot) - Taproot address
if (decodedScript && decodedScript[0] === 0x51) {
// OP_CHECKSIG
console.log('attempting to decode taproot pkscript');
try {
const taprootPubKey: any = decodedScript[1];
const { address } = bitcoin.payments.p2tr({
pubkey: taprootPubKey,
network
});
if (address) addresses.push(address);
} catch (e) {
console.log('error decoding taproot pkscript', e);
}
return;
}

console.log(
'unknown address type for script pubkey',
scriptPubKeyHex
);
});
tx.dest_addresses = addresses;
});

// If getinfo fails, return blank txs
if (getinfoResult.status !== 'fulfilled') {
Expand Down Expand Up @@ -218,7 +340,8 @@ export const getChainTransactions = async () => {
num_confirmations: getinfo.blockheight - withdrawal[6],
time_stamp: withdrawal[5],
txid: tx.hash,
note: 'on-chain withdrawal'
note: 'on-chain withdrawal',
dest_addresses: tx.dest_addresses
};
}

Expand All @@ -229,7 +352,8 @@ export const getChainTransactions = async () => {
num_confirmations: getinfo.blockheight - deposit[6],
time_stamp: deposit[5],
txid: tx.hash,
note: 'on-chain deposit'
note: 'on-chain deposit',
dest_addresses: tx.dest_addresses
};
}

Expand Down
6 changes: 5 additions & 1 deletion models/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ export default class Transaction extends BaseModel {
}

@computed public get destAddresses(): Array<string> {
return this.dest_addresses || [this.address];
return this.dest_addresses
? this.dest_addresses
: this.address
? [this.address]
: [];
}

@computed public get getOutpoint(): string {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@keystonehq/bc-ur-registry": "0.7.0",
"@lightninglabs/lnc-core": "file:zeus_modules/@lightninglabs/lnc-core",
"@ngraveio/bc-ur": "1.1.12",
"@noble/secp256k1": "1.6.3",
"@react-native-async-storage/async-storage": "1.23.1",
"@react-native-clipboard/clipboard": "1.14.3",
"@react-native-community/netinfo": "11.3.2",
Expand Down Expand Up @@ -62,6 +63,7 @@
"dateformat": "5.0.3",
"dns.js": "1.0.1",
"domain-browser": "1.2.0",
"ecpair": "2.0.1",
"elliptic": "6.6.0",
"events": "1.1.1",
"fast-sha256": "1.3.0",
Expand Down Expand Up @@ -148,6 +150,7 @@
"@react-native/babel-preset": "0.74.84",
"@react-native/eslint-config": "0.74.84",
"@react-native/metro-config": "0.74.84",
"@types/create-hash": "1.2.2",
"@types/crypto-js": "4.1.1",
"@types/jest": "29.5.12",
"@types/lodash": "4.14.198",
Expand Down
32 changes: 30 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,11 @@
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.3.tgz#39908da56a4adc270147bb07968bf3b16cfe1699"
integrity sha512-V7/fPHgl+jsVPXqqeOzT8egNj2iBIVt+ECeMMG8TdcnTikP3oaBtUVqpT/gYCR68aEBJSF+XbYUxStjbFMqIIA==

"@noble/[email protected]":
version "1.6.3"
resolved "https://registry.yarnpkg.com/@noble/secp256k1/-/secp256k1-1.6.3.tgz#7eed12d9f4404b416999d0c87686836c4c5c9b94"
integrity sha512-T04e4iTurVy7I8Sw4+c5OSN9/RkPlo1uKxAomtxQNLq8j1uPAqnsqG1bqvY3Jv7c13gyr6dui0zmh/I3+f/JaQ==

"@nodelib/[email protected]":
version "2.1.5"
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
Expand Down Expand Up @@ -3070,6 +3075,13 @@
dependencies:
"@types/node" "*"

"@types/[email protected]":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@types/create-hash/-/create-hash-1.2.2.tgz#e87247083df8478f6b83655592bde0d709028235"
integrity sha512-Fg8/kfMJObbETFU/Tn+Y0jieYewryLrbKwLCEIwPyklZZVY2qB+64KFjhplGSw+cseZosfFXctXO+PyIYD8iZQ==
dependencies:
"@types/node" "*"

"@types/[email protected]":
version "4.1.1"
resolved "https://registry.yarnpkg.com/@types/crypto-js/-/crypto-js-4.1.1.tgz#602859584cecc91894eb23a4892f38cfa927890d"
Expand Down Expand Up @@ -4206,7 +4218,7 @@ bs58@^5.0.0:
dependencies:
base-x "^4.0.0"

[email protected], bs58check@^2.1.2:
[email protected], bs58check@<3.0.0, bs58check@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/bs58check/-/bs58check-2.1.2.tgz#53b018291228d82a5aa08e7d796fdafda54aebfc"
integrity sha512-0TS1jicxdU09dwJMNZtVAfzPi6Q6QeN0pM1Fkzrjn+XYHvzMKPU3pHVpva+769iNVSfIYWf7LJ6WR+BuuMf8cA==
Expand Down Expand Up @@ -5044,6 +5056,15 @@ dot-case@^3.0.4:
no-case "^3.0.4"
tslib "^2.0.3"

[email protected]:
version "2.0.1"
resolved "https://registry.yarnpkg.com/ecpair/-/ecpair-2.0.1.tgz#e25ab416f1ecb6b05477ca601313df937b075d2e"
integrity sha512-iT3wztQMeE/nDTlfnAg8dAFUfBS7Tq2BXzq3ae6L+pWgFU0fQ3l0woTzdTBrJV3OxBjxbzjq8EQhAbEmJNWFSw==
dependencies:
randombytes "^2.1.0"
typeforce "^1.18.0"
wif "^2.0.6"

[email protected]:
version "1.1.1"
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
Expand Down Expand Up @@ -10258,7 +10279,7 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==

typeforce@^1.11.3:
typeforce@^1.11.3, typeforce@^1.18.0:
version "1.18.0"
resolved "https://registry.yarnpkg.com/typeforce/-/typeforce-1.18.0.tgz#d7416a2c5845e085034d70fcc5b6cc4a90edbfdc"
integrity sha512-7uc1O8h1M1g0rArakJdf0uLRSSgFcYexrVoKo+bzJd32gd4gDy2L/Z+8/FjPnU9ydY3pEnVPtr9FyscYY60K1g==
Expand Down Expand Up @@ -10549,6 +10570,13 @@ which@^2.0.1:
dependencies:
isexe "^2.0.0"

wif@^2.0.6:
version "2.0.6"
resolved "https://registry.yarnpkg.com/wif/-/wif-2.0.6.tgz#08d3f52056c66679299726fade0d432ae74b4704"
integrity sha512-HIanZn1zmduSF+BQhkE+YXIbEiH0xPr1012QbFEGB0xsKqJii0/SqJjyn8dFv6y36kOznMgMB+LGcbZTJ1xACQ==
dependencies:
bs58check "<3.0.0"

word-wrap@^1.2.5, word-wrap@~1.2.3:
version "1.2.5"
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.5.tgz#d2c45c6dd4fbce621a66f136cbe328afd0410b34"
Expand Down
Loading

0 comments on commit 677ed38

Please sign in to comment.