Skip to content

Commit

Permalink
feat: updated bitgo-utxo-lib to fix zcash, updated bch endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
0x31 committed Nov 26, 2020
1 parent e02a911 commit 2cdec25
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 92 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@
"axios": "^0.19.0",
"bchaddrjs": "^0.4.9",
"bignumber.js": "^9.0.0",
"bitgo-utxo-lib": "https://github.com/ren-forks/bitgo-utxo-lib#7939387dda1199956fcb9d720a9493ce12cf013e",
"bitcore-lib": "^8.23.1",
"bitcore-lib-zcash": "^0.13.20",
"bitgo-utxo-lib": "https://github.com/ren-forks/bitgo-utxo-lib#b848585e65b42c48b98c207e72d7d3006c9a5da0",
"dotenv": "^8.2.0",
"immutable": "^4.0.0-rc.12",
"web3": "^1.3.0",
Expand Down
139 changes: 64 additions & 75 deletions src/common/apis/bitcoinDotCom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,21 @@ import { fixUTXO, fixUTXOs, fixValue, sortUTXOs, UTXO } from "../../lib/utxo";
import { FetchTXsResult } from "./insight";
import { DEFAULT_TIMEOUT } from "./timeout";

export interface ScriptSig {
hex: string;
asm: string;
}

export interface Vin {
txid: string; // "4f72fce028ff1e99459393232e8bf8a430815ec5cea8700676c101b02be3649c",
vout: number; // 1,
sequence: number; // 4294967295,
n: number; // 0,
scriptSig: ScriptSig;
value: number; // 7223963,
legacyAddress: string; // "1D4NXvNvjucShZeyLsDzYz1ky2W8gYKQH7",
cashAddress: string; // "bitcoincash:qzzyfwmnz3dlld7svwzn53xzr6ycz5kwavpd9uqf4l"
}

export interface ScriptPubKey {
hex: string; // "76a91427bad06158841621ed33eb91efdb1cc4af4996cb88ac",
asm: string; // "OP_DUP OP_HASH160 27bad06158841621ed33eb91efdb1cc4af4996cb OP_EQUALVERIFY OP_CHECKSIG",
addresses: string[]; // ["14d59YbC2D9W9y5kozabCDhxkY3eDQq7B3"],
type: string; // "pubkeyhash",
cashAddrs: string[]; // ["bitcoincash:qqnm45rptzzpvg0dx04erm7mrnz27jvkevaf3ys3c5"]
}

export interface Vout {
value: string; // "0.04159505",
n: number; // 0,
scriptPubKey: ScriptPubKey;
spentTxId: string; // "9fa7bc86ad4729cbd5c182a8cd5cfc5eb457608fe430ce673f33ca52bfb1a187",
spentIndex: number; // 1,
spentHeight: number; // 617875
}

export interface QueryTransaction {
vin: Vin[];
vout: Vout[];
txid: string; // "03e29b07bb98b1e964296289dadb2fb034cb52e178cc306d20cc9ddc951d2a31",
version: number; // 1,
locktime: number; // 0,
blockhash: string; // "0000000000000000011c71094699e3ba47c43da76d775cf5eb5fbea1787fafb5",
blockheight: number; // 616200,
confirmations: number; // 27433,
time: number; // 1578054427,
blocktime: number; // 1578054427,
firstSeenTime: number; // 1578054360,
valueOut: number; // 0.07213963,
size: number; // 226,
valueIn: number; // 0.07223963,
fees: number; // 0.0001
}

const endpoint = (testnet: boolean) =>
testnet ? "https://trest.bitcoin.com/v2/" : "https://rest.bitcoin.com/v2/";

const endpointV2 = (testnet: boolean) =>
testnet
? "https://explorer-tbch.api.bitcoin.com/tbch/v1"
: "https://explorer.api.bitcoin.com/bch/v1";

const fetchUTXO = (testnet: boolean) => async (
txHash: string,
vOut: number
): Promise<UTXO> => {
const url = `${endpoint(testnet).replace(
/\/$/,
""
)}/transaction/details/${txHash}`;
const url = `${endpointV2(testnet)}/tx/${txHash}`;

const response = await axios.get<QueryTransaction>(`${url}`, {
const response = await axios.get<FetchTXResponse>(`${url}`, {
timeout: DEFAULT_TIMEOUT,
});

Expand All @@ -85,32 +36,16 @@ const fetchUTXO = (testnet: boolean) => async (
);
};

interface FetchUTXOs {
utxos: ReadonlyArray<{
address: string;
txid: string;
vout: number;
scriptPubKey: string;
amount: number;
satoshis: number;
confirmations: number;
ts: number;
}>;
}

const fetchUTXOs = (testnet: boolean) => async (
address: string,
confirmations: number
): Promise<readonly UTXO[]> => {
const url = `${endpoint(testnet).replace(
/\/$/,
""
)}/address/utxo/${address}`;
const response = await axios.get<FetchUTXOs>(url, {
const url = `${endpointV2(testnet)}/addr/${address}/utxo`;
const response = await axios.get<FetchUTXOSResponse>(url, {
timeout: DEFAULT_TIMEOUT,
});
return fixUTXOs(
response.data.utxos
response.data
.map((utxo) => ({
txHash: utxo.txid,
amount: utxo.amount,
Expand Down Expand Up @@ -185,3 +120,57 @@ export const BitcoinDotCom = {
fetchTXs,
broadcastTransaction,
};

type FetchUTXOSResponse = Array<{
address: string; // "miMi2VET41YV1j6SDNTeZoPBbmH8B4nEx6";
txid: string; // "cfa3301a29937b0571b759a9af895b713214060aaeef2ac35b9d290dd7d10553";
vout: number; // 1;
scriptPubKey: string; // "76a9141f28b9198368dcc57cbdadd55092ba8d0cfc0cdb88ac";
amount: number; // 1.39903978;
satoshis: number; // 139903978;
height: number; // 1401543;
confirmations: number; // 21295;
}>;

type FetchTXResponse = {
txid: string; // "cfa3301a29937b0571b759a9af895b713214060aaeef2ac35b9d290dd7d10553";
version: number; // 1;
locktime: number; // 0;
vin: Array<{
txid: string; // "195e1f3a70235216e1e61c19b3c3b89c6384f707f5c68b0570c30603eed72f12";
vout: number; // 0;
sequence: number; // 4294967295;
n: number; // 0;
scriptSig: {
hex: string; // "4730440220732686ea0e3582e23a0008682b215bc8b03bd4ecb730828f94d28df2b3fa865f0220310273bb51e727bbb6e2e816ec6c4f79276508d9f634b183450dabd38898d8ca41210268ccfdd69648ff16ddc607994462d235b520bc29f5b1f88d3e4a6403971f1413";
asm: string; // "30440220732686ea0e3582e23a0008682b215bc8b03bd4ecb730828f94d28df2b3fa865f0220310273bb51e727bbb6e2e816ec6c4f79276508d9f634b183450dabd38898d8ca41 0268ccfdd69648ff16ddc607994462d235b520bc29f5b1f88d3e4a6403971f1413";
};
addr: string; // "miMi2VET41YV1j6SDNTeZoPBbmH8B4nEx6";
valueSat: number; // 11769;
value: number; // 0.00011769;
doubleSpentTxID: null; // null;
}>;
vout: Array<{
value: string; // "0.10000000";
n: number; // 0;
scriptPubKey: {
hex: string; // "76a914eb7af4368d7c9365a09f445827b8a6841792773388ac";
asm: string; // "OP_DUP OP_HASH160 eb7af4368d7c9365a09f445827b8a68417927733 OP_EQUALVERIFY OP_CHECKSIG";
addresses: number; // ["n2z4P8CQxeDMeckx817v9qvkfNsxMpC48E"];
type: string; // "pubkeyhash";
};
spentTxId: string; // "7a794b095f169f554ff89df2c07179803fb6a71496fd1e3a1ff273641d8050ac";
spentIndex: number; // 2;
spentHeight: number; // 1403107;
}>;
blockhash: string; // "00000000001d49c2bf7a8509e4f35aa49f091801c144ed9046cff4354bea7389";
blockheight: number; // 1401543;
confirmations: number; // 21295;
time: number; // 1597282685;
blocktime: number; // 1597282685;
firstSeenTime: number; // 1597282384;
valueOut: number; // 1.49903978;
size: number; // 1699;
valueIn: number; // 1.49913978;
fees: number; // 0.0001;
};
3 changes: 1 addition & 2 deletions src/common/apis/sochain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ const broadcastTransaction = (network: string) => async (
txid: string; // Hex without 0x
};
}>(
// `https://sochain.com/api/v2/send_tx/${network}`,
`https://chain.so/send_tx/${network}`,
`https://sochain.com/api/v2/send_tx/${network}`,
{ tx_hex: txHex },
{ timeout: DEFAULT_TIMEOUT }
);
Expand Down
8 changes: 6 additions & 2 deletions src/common/libraries/bitgoUtxoLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const buildUTXO = async (
versionGroupID?: number;
expiryHeight?: number;
lockTime?: number;
consensusBranchId?: number;
}
): Promise<{ toHex: () => string }> => {
const fees = new BigNumber(
Expand All @@ -38,14 +39,17 @@ const buildUTXO = async (
tx.setVersion(options.version);
}
if (options && options.versionGroupID) {
tx.setVersionGroupId(0xf5b9230b);
tx.setVersionGroupId(options.versionGroupID);
}
if (options && options.expiryHeight) {
tx.setExpiryHeight(options.expiryHeight);
}
if (options && options.lockTime) {
tx.setLockTime(options.lockTime);
}
if (options && options.consensusBranchId) {
tx.setConsensusBranchId(options.consensusBranchId);
}

// Only use the required utxos
const [usedUTXOs, sum] = utxos.reduce(
Expand Down Expand Up @@ -78,7 +82,7 @@ const buildUTXO = async (
tx.sign(
i,
privateKey,
"",
null,
options && options.signFlag !== undefined ? options.signFlag : null,
utxo.amount
);
Expand Down
1 change: 1 addition & 0 deletions src/handlers/BCH/BCHHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ export class BCHHandler implements Handler {
{
...options,
signFlag:
// bitcoin.Transaction.SIGHASH_ALL |
// tslint:disable-next-line: no-bitwise
bitcoin.Transaction.SIGHASH_SINGLE |
bitcoin.Transaction.SIGHASH_BITCOINCASHBIP143,
Expand Down
12 changes: 6 additions & 6 deletions src/handlers/ZEC/ZECHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,11 @@ export const _apiFallbacks = {
broadcastTransaction: (testnet: boolean, hex: string) =>
testnet
? [
...shuffleArray(() =>
() =>
Insight.broadcastTransaction(
InsightEndpoints.TestnetZCash
)(hex)
),
() => Sochain.broadcastTransaction("ZECTEST")(hex),
)(hex),
// () => Sochain.broadcastTransaction("ZECTEST")(hex),
]
: [
...shuffleArray(
Expand Down Expand Up @@ -308,7 +307,7 @@ export class ZECHandler implements Handler {

if (this.testnet) {
// tslint:disable-next-line: no-object-mutation
bitcoin.networks.zcashTest.consensusBranchId["4"] = 0xf5b9230b;
bitcoin.networks.zcashTest.consensusBranchId["4"] = 0xe9ff75a6;
}

const built = await BitgoUTXOLib.buildUTXO(
Expand All @@ -323,7 +322,8 @@ export class ZECHandler implements Handler {
{
...options,
version: 4,
versionGroupID: this.testnet ? 0xf5b9230b : 0x892f2085,
versionGroupID: 0x892f2085,
consensusBranchId: 0xe9ff75a6,
}
);

Expand Down
2 changes: 1 addition & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const s = (asset: string | { type: "ERC20"; name: string; address?: string }) =>
token: process.env.FILECOIN_TESTNET_TOKEN,
});
test.skip("send BTC", sendToken, "BTC", 8, "testnet");
test.skip("send ZEC", sendToken, "ZEC", 8, "testnet");
test("send ZEC", sendToken, "ZEC", 8, "testnet");
test.skip("send BCH", sendToken, "BCH", 8, "testnet");
test.serial("send ETH", sendToken, "ETH", 18, "kovan");
test.serial(
Expand Down
Loading

0 comments on commit 2cdec25

Please sign in to comment.