Skip to content

Commit

Permalink
Merge pull request #15 from gabidi/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
gabidi authored Dec 10, 2019
2 parents 31c552a + 79cdda5 commit fab1a32
Show file tree
Hide file tree
Showing 10 changed files with 4,420 additions and 60 deletions.
3,994 changes: 3,994 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

46 changes: 26 additions & 20 deletions src/clients/btcClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,6 @@ var cypherNodeHttpTransport_1 = __importDefault(require("../transport/cypherNode
exports.client = function (_a) {
var _b = (_a === void 0 ? {} : _a).transport, transport = _b === void 0 ? cypherNodeHttpTransport_1.default() : _b;
var get = transport.get, post = transport.post;
var parseBtcAddressType = function (address) {
var addressStart = address.substr(0, 4);
switch (addressStart) {
case "upub":
case "zpub":
case "xpub":
return "expub";
default:
switch (addressStart[0]) {
case "1":
return "legacy";
case "3":
return "p2sh-segwit";
case "b":
return "bech32";
default:
return null;
}
}
};
var api = {
/** Core and Spending */
getBlockChainInfo: function () {
Expand Down Expand Up @@ -348,6 +328,32 @@ exports.client = function (_a) {
}
});
});
},
getUnusedAddressesByPub32Label: function (label) {
return __awaiter(this, void 0, void 0, function () {
var label_unused_addresses;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, get("get_unused_addresses_by_watchlabel", label)];
case 1:
label_unused_addresses = (_a.sent()).label_unused_addresses;
return [2 /*return*/, label_unused_addresses];
}
});
});
},
getTransactionsByPub32Label: function (label) {
return __awaiter(this, void 0, void 0, function () {
var label_txns;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, get("get_txns_by_watchlabel", label)];
case 1:
label_txns = (_a.sent()).label_txns;
return [2 /*return*/, label_txns];
}
});
});
}
};
return api;
Expand Down
140 changes: 136 additions & 4 deletions src/clients/btcClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ test("Should be able to get a transactions info", function (t) { return __awaite
switch (_b.label) {
case 0:
_a = t.context, getTxn = _a.getTxn, chain = _a.chain;
if (chain === "test") {
if (chain !== "main") {
t.pass();
return [2 /*return*/];
}
Expand Down Expand Up @@ -240,6 +240,138 @@ test("Should be able to spend (will only run when testnet)", function (t) { retu
}
});
}); });
/* Watch Pub32 tests
Removed from this release pending CN fixes to Pub32 endpoints
*/
/* Watch Pub32 tests */
test("Should be able to get a list of watched Pub32 and their labels", function (t) { return __awaiter(_this, void 0, void 0, function () {
var getWatchedPub32, watchedPub32;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
getWatchedPub32 = t.context.getWatchedPub32;
return [4 /*yield*/, getWatchedPub32()];
case 1:
watchedPub32 = _a.sent();
t.true(Array.isArray(watchedPub32));
if (watchedPub32.length < 1)
t.pass();
else {
t.true(watchedPub32.every(function (_a) {
var pub32 = _a.pub32, label = _a.label;
return pub32.length && label.length;
}));
}
return [2 /*return*/];
}
});
}); });
test("Should be able to watch a pub32", function (t) { return __awaiter(_this, void 0, void 0, function () {
var _a, watchPub32, chain, tpub, pub32label, watchOptions, _b, label, pub32, id;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_a = t.context, watchPub32 = _a.watchPub32, chain = _a.chain;
tpub = chain === "main"
? "xpub6AHA9hZDN11k2ijHMeS5QqHx2KP9aMBRhTDqANMnwVtdyw2TDYRmF8PjpvwUFcL1Et8Hj59S3gTSMcUQ5gAqTz3Wd8EsMTmF3DChhqPQBnU"
: "tpubDAenfwNu5GyCJWv8oqRAckdKMSUoZjgVF5p8WvQwHQeXjDhAHmGrPa4a4y2Fn7HF2nfCLefJanHV3ny1UY25MRVogizB2zRUdAo7Tr9XAjm";
pub32label = "js_sdkpub32_test";
watchOptions = {
label: pub32label,
nstart: 0,
path: "0/n"
};
return [4 /*yield*/, watchPub32(tpub, watchOptions)];
case 1:
_b = _c.sent(), label = _b.label, pub32 = _b.pub32, id = _b.id;
t.false(isNaN(id));
t.is(pub32, tpub);
t.is(label, watchOptions.label);
return [2 /*return*/];
}
});
}); });
test("Should be able to get watched address for 32pub by labe", function (t) { return __awaiter(_this, void 0, void 0, function () {
var getWatchedAddressesByPub32Label, pub32Label, watchedAddresses;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
getWatchedAddressesByPub32Label = t.context.getWatchedAddressesByPub32Label;
pub32Label = "js_sdkpub32_test";
return [4 /*yield*/, getWatchedAddressesByPub32Label(pub32Label)];
case 1:
watchedAddresses = _a.sent();
t.true(watchedAddresses.length > 0);
t.true(watchedAddresses.every(function (_a) {
var address = _a.address;
return address.length === 34;
}));
return [2 /*return*/];
}
});
}); });
test("Should be able to get a watched 32pub's balance by label", function (t) { return __awaiter(_this, void 0, void 0, function () {
var getBalanceByPub32Label, pub32Label, balance;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
getBalanceByPub32Label = t.context.getBalanceByPub32Label;
pub32Label = "js_sdkpub32_test";
return [4 /*yield*/, getBalanceByPub32Label(pub32Label)];
case 1:
balance = _a.sent();
t.true(!isNaN(balance));
return [2 /*return*/];
}
});
}); });
test("Should be able to get a watched 32pub's unused addresses", function (t) { return __awaiter(_this, void 0, void 0, function () {
var getUnusedAddressesByPub32Label, pub32Label, unusedAddressList;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
getUnusedAddressesByPub32Label = t.context.getUnusedAddressesByPub32Label;
pub32Label = "js_sdkpub32_test";
return [4 /*yield*/, getUnusedAddressesByPub32Label(pub32Label)];
case 1:
unusedAddressList = _a.sent();
t.true(Array.isArray(unusedAddressList));
t.true(unusedAddressList.every(function (_a) {
var address = _a.address, address_pub32_index = _a.address_pub32_index;
return !!address.length && !isNaN(address_pub32_index);
}));
return [2 /*return*/];
}
});
}); });
test("Should be able to get transactions for watch label ", function (t) { return __awaiter(_this, void 0, void 0, function () {
var getTransactionsByPub32Label, pub32Label, pub32Txns;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
getTransactionsByPub32Label = t.context.getTransactionsByPub32Label;
pub32Label = "js_sdkpub32_test";
return [4 /*yield*/, getTransactionsByPub32Label(pub32Label)];
case 1:
pub32Txns = _a.sent();
t.true(Array.isArray(pub32Txns));
t.true(pub32Txns.every(function (_a) {
var amount = _a.amount, txid = _a.txid;
return !!txid.length && !isNaN(amount);
}));
return [2 /*return*/];
}
});
}); });
test("Should be able to unwatch by label", function (t) { return __awaiter(_this, void 0, void 0, function () {
var unwatchPub32ByLabel, pub32Label, label;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
unwatchPub32ByLabel = t.context.unwatchPub32ByLabel;
pub32Label = "js_sdkpub32_test";
return [4 /*yield*/, unwatchPub32ByLabel(pub32Label)];
case 1:
label = (_a.sent()).label;
t.is(label, pub32Label);
return [2 /*return*/];
}
});
}); });
89 changes: 84 additions & 5 deletions src/clients/btcClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ test("Should be able to get a transactions info", async t => {
const {
context: { getTxn, chain }
} = t;
if (chain === "test") {
if (chain !== "main") {
t.pass();
return;
}
Expand Down Expand Up @@ -132,7 +132,86 @@ test("Should be able to spend (will only run when testnet)", async t => {
const { status, hash } = await spend(sendToAddress, balance / 1000);
t.is(status, "accepted");
});
/* Watch Pub32 tests
Removed from this release pending CN fixes to Pub32 endpoints
*/

/* Watch Pub32 tests */
test("Should be able to get a list of watched Pub32 and their labels", async t => {
const {
context: { getWatchedPub32 }
} = t;
const watchedPub32 = await getWatchedPub32();
t.true(Array.isArray(watchedPub32));
if (watchedPub32.length < 1) t.pass();
else {
t.true(
watchedPub32.every(({ pub32, label }) => pub32.length && label.length)
);
}
});
test("Should be able to watch a pub32", async t => {
const {
context: { watchPub32, chain }
} = t;
const tpub =
chain === "main"
? "xpub6AHA9hZDN11k2ijHMeS5QqHx2KP9aMBRhTDqANMnwVtdyw2TDYRmF8PjpvwUFcL1Et8Hj59S3gTSMcUQ5gAqTz3Wd8EsMTmF3DChhqPQBnU"
: "tpubDAenfwNu5GyCJWv8oqRAckdKMSUoZjgVF5p8WvQwHQeXjDhAHmGrPa4a4y2Fn7HF2nfCLefJanHV3ny1UY25MRVogizB2zRUdAo7Tr9XAjm";
const pub32label = "js_sdkpub32_test";
const watchOptions: Pub32WatcherOptions = {
label: pub32label,
nstart: 0,
path: "0/n"
};
const { label, pub32, id } = await watchPub32(tpub, watchOptions);
t.false(isNaN(id));
t.is(pub32, tpub);
t.is(label, watchOptions.label);
});
test("Should be able to get watched address for 32pub by labe", async t => {
const {
context: { getWatchedAddressesByPub32Label }
} = t;
const pub32Label = "js_sdkpub32_test";
const watchedAddresses = await getWatchedAddressesByPub32Label(pub32Label);
t.true(watchedAddresses.length > 0);
t.true(watchedAddresses.every(({ address }) => address.length === 34));
});
test("Should be able to get a watched 32pub's balance by label", async t => {
const {
context: { getBalanceByPub32Label }
} = t;
const pub32Label = "js_sdkpub32_test";
const balance = await getBalanceByPub32Label(pub32Label);
t.true(!isNaN(balance));
});
test("Should be able to get a watched 32pub's unused addresses", async t => {
const {
context: { getUnusedAddressesByPub32Label }
} = t;
const pub32Label = "js_sdkpub32_test";
const unusedAddressList = await getUnusedAddressesByPub32Label(pub32Label);
t.true(Array.isArray(unusedAddressList));
t.true(
unusedAddressList.every(
({ address, address_pub32_index }) =>
!!address.length && !isNaN(address_pub32_index)
)
);
});
test("Should be able to get transactions for watch label ", async t => {
const {
context: { getTransactionsByPub32Label }
} = t;
const pub32Label = "js_sdkpub32_test";
const pub32Txns = await getTransactionsByPub32Label(pub32Label);
t.true(Array.isArray(pub32Txns));
t.true(
pub32Txns.every(({ amount, txid }) => !!txid.length && !isNaN(amount))
);
});
test("Should be able to unwatch by label", async t => {
const {
context: { unwatchPub32ByLabel }
} = t;
const pub32Label = "js_sdkpub32_test";
const { label } = await unwatchPub32ByLabel(pub32Label);
t.is(label, pub32Label);
});
40 changes: 17 additions & 23 deletions src/clients/btcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,21 @@ import {
AddressType,
AddressWatchConfirmation,
AddressWatchPayload,
GenericWatchResponse,
WatcherOptions,
Pub32WatcherOptions,
Pub32AddressWatchPayload,
Pub32WatchConfirmation,
WatchPub32UnusedAddress,
WatchedPub32,
WatchPub32Txn,
BlockChainInfo,
SpendConfirmation
} from "../lib/types/btc.d";
export const client = ({
transport = cypherNodeHTTPTransport()
}: ClientConfig = {}): CypherNodeBtcClient => {
const { get, post } = transport;
const parseBtcAddressType = (address: Address): AddressType | null => {
const addressStart = address.substr(0, 4);
switch (addressStart) {
case "upub":
case "zpub":
case "xpub":
return "expub";
default:
switch (addressStart[0]) {
case "1":
return "legacy";
case "3":
return "p2sh-segwit";
case "b":
return "bech32";
default:
return null;
}
}
};
const api = {
/** Core and Spending */
getBlockChainInfo(): Promise<BlockChainInfo> {
Expand Down Expand Up @@ -145,9 +128,7 @@ export const client = ({
const result = await get("unwatchxpubbyxpub", xpub);
return result;
},
async unwatchPub32ByLabel(
label: string
): Promise<AddressWatchConfirmation> {
async unwatchPub32ByLabel(label: string): Promise<GenericWatchResponse> {
const result = await get("unwatchxpubbylabel", label);
return result;
},
Expand All @@ -159,6 +140,19 @@ export const client = ({
async getBalanceByPub32Label(label: string): Promise<string> {
const { balance } = await get("getbalancebyxpublabel", label);
return balance;
},
async getUnusedAddressesByPub32Label(
label: string
): Promise<[WatchPub32UnusedAddress]> {
const { label_unused_addresses } = await get(
"get_unused_addresses_by_watchlabel",
label
);
return label_unused_addresses;
},
async getTransactionsByPub32Label(label: string): Promise<[WatchPub32Txn]> {
const { label_txns } = await get("get_txns_by_watchlabel", label);
return label_txns;
}
};
return api;
Expand Down
Loading

0 comments on commit fab1a32

Please sign in to comment.