Skip to content

Commit

Permalink
Fix Rts.utxo(addr) implementaion in BCH-JS backen that was using ol…
Browse files Browse the repository at this point in the history
…d BITBOX REST endpoint.
  • Loading branch information
osoftware committed Nov 21, 2020
1 parent 4547191 commit 34d6c65
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 36 deletions.
4 changes: 4 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Release 0.3.1.1

* fix `Rts.utxo(addr)` implementaion in BCH-JS backen that was using old BITBOX REST endpoint.

# Release 0.3.1.0

This is a minor feature release focusing on SDK improvements. See migration guide in docs for details.
Expand Down
4 changes: 2 additions & 2 deletions spedn-js/rts-bchjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spedn/rts-bchjs",
"version": "3.1.0",
"version": "3.1.1",
"description": "Runtime System for Spedn Contracts with BCH-JS adapter",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -26,7 +26,7 @@
],
"dependencies": {
"@chris.troutner/bch-js": "^3.4.1",
"@spedn/rts": "^3.1.0"
"@spedn/rts": "^3.1.1"
},
"devDependencies": {
"@types/node": "^14.0.27",
Expand Down
15 changes: 12 additions & 3 deletions spedn-js/rts-bchjs/src/rts-bchjs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BCHJS from "@chris.troutner/bch-js";
import { Addresses, Crypto, Rts, RtsECPair, RtsTransactionBuilder, Script } from "@spedn/rts";
import { Addresses, Crypto, Rts, RtsECPair, RtsTransactionBuilder, Script, UtxoResult } from "@spedn/rts";
import Bitcoin from "bitcoincashjs-lib";

const defaultConfigs: { [network: string]: any } = {
Expand All @@ -15,8 +15,17 @@ export class BchJsRts extends Rts {
this.bchjs = bchjs || new BCHJS(defaultConfigs[network]);
}

utxo(addr: any) {
return this.bchjs.Address.utxo(addr);
async utxo(addr: string): Promise<UtxoResult> {
const result = await this.bchjs.Electrumx.utxo(addr);
return {
...result,
utxos: result.utxos.map((o: any) => ({
...o,
txid: o.tx_hash,
vout: o.tx_pos,
satoshis: o.value
}))
};
}

ecPair(ecPair: Bitcoin.ECPair): RtsECPair {
Expand Down
1 change: 1 addition & 0 deletions spedn-js/rts-bchjs/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare module "@chris.troutner/bch-js" {
constructor(conf: any);
Address: any;
Crypto: any;
Electrumx: any;
Script: any;
Mnemonic: any;
HDNode: any;
Expand Down
8 changes: 4 additions & 4 deletions spedn-js/rts-bchjs/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@
resolved "https://registry.yarnpkg.com/@istanbuljs/schema/-/schema-0.1.2.tgz#26520bf09abe4a5644cd5414e37125a8954241dd"
integrity sha512-tsAQNx32a8CoFhjhijUIhI4kccIAgmGhy8LZMZgGfmXcpMbPRUqn5LWmgRttILi6yeGmBJd2xsPkFMs0PzgPCw==

"@spedn/rts@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@spedn/rts/-/rts-3.1.0.tgz#090118f57964a7e2c2ac6d9d61a47637ea10af5a"
integrity sha512-fYAGRiMKPE/UZjNTkwLYv3Nvry740H15yeC5+oNIYAT85SP3E3mY9VFyxjK6EUWFtTB+Px+/N73t0HgqBLs37w==
"@spedn/rts@^3.1.1":
version "3.1.1"
resolved "https://registry.yarnpkg.com/@spedn/rts/-/rts-3.1.1.tgz#b45b2cb49df994f448aca5c490696f942544699d"
integrity sha512-r3YjixGB/zLXq/kcnypx6oKSVm2fri+LW+PrExB3u/5Tu0gorUieHd33KWaKaSWYam3VKTBeyzPADoAq96EAtw==
dependencies:
lodash "^4.17.20"
varuint-bitcoin "1.1.2"
Expand Down
4 changes: 2 additions & 2 deletions spedn-js/rts-bitbox/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spedn/rts-bitbox",
"version": "3.1.0",
"version": "3.1.1",
"description": "Runtime System for Spedn Contracts with BITBOX adapter",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -25,7 +25,7 @@
"bitcoincash:qrc2jhalczuka8q3dvk0g8mnkqx79wxp9gvvqvg7qt"
],
"dependencies": {
"@spedn/rts": "^3.1.0",
"@spedn/rts": "^3.1.1",
"bitbox-sdk": "^8.11.2"
},
"devDependencies": {
Expand Down
7 changes: 4 additions & 3 deletions spedn-js/rts-bitbox/src/rts-bitbox.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Addresses, Crypto, Rts, RtsECPair, RtsTransactionBuilder, Script } from "@spedn/rts";
import { Addresses, Crypto, Rts, RtsECPair, RtsTransactionBuilder, Script, UtxoResult } from "@spedn/rts";
import { BITBOX, TREST_URL, TWS_URL } from "bitbox-sdk";
import { AddressUtxoResult } from "bitcoin-com-rest";
import Bitcoin from "bitcoincashjs-lib";

const defaultConfigs: { [network: string]: any } = {
Expand All @@ -15,8 +16,8 @@ export class BitboxRts extends Rts {
this.bitbox = bitbox || new BITBOX(defaultConfigs[network]);
}

utxo(addr: any) {
return this.bitbox.Address.utxo(addr);
utxo(addr: string): Promise<UtxoResult> {
return this.bitbox.Address.utxo(addr) as any;
}

ecPair(ecPair: Bitcoin.ECPair): RtsECPair {
Expand Down
8 changes: 4 additions & 4 deletions spedn-js/rts-bitbox/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@
resolved "https://registry.yarnpkg.com/@istanbuljs/schema/-/schema-0.1.2.tgz#26520bf09abe4a5644cd5414e37125a8954241dd"
integrity sha512-tsAQNx32a8CoFhjhijUIhI4kccIAgmGhy8LZMZgGfmXcpMbPRUqn5LWmgRttILi6yeGmBJd2xsPkFMs0PzgPCw==

"@spedn/rts@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@spedn/rts/-/rts-3.1.0.tgz#090118f57964a7e2c2ac6d9d61a47637ea10af5a"
integrity sha512-fYAGRiMKPE/UZjNTkwLYv3Nvry740H15yeC5+oNIYAT85SP3E3mY9VFyxjK6EUWFtTB+Px+/N73t0HgqBLs37w==
"@spedn/rts@^3.1.1":
version "3.1.1"
resolved "https://registry.yarnpkg.com/@spedn/rts/-/rts-3.1.1.tgz#b45b2cb49df994f448aca5c490696f942544699d"
integrity sha512-r3YjixGB/zLXq/kcnypx6oKSVm2fri+LW+PrExB3u/5Tu0gorUieHd33KWaKaSWYam3VKTBeyzPADoAq96EAtw==
dependencies:
lodash "^4.17.20"
varuint-bitcoin "1.1.2"
Expand Down
2 changes: 1 addition & 1 deletion spedn-js/rts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spedn/rts",
"version": "3.1.0",
"version": "3.1.1",
"description": "Runtime System for Spedn contracts.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
1 change: 0 additions & 1 deletion spedn-js/rts/src/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export interface Utxo {
amount: number;
satoshis: number;
height: number;
confirmations: number;
}

export interface Coin {
Expand Down
9 changes: 7 additions & 2 deletions spedn-js/rts/src/rts.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Module, ModuleFactory, PortableModule } from "./contracts";
import { Module, ModuleFactory, PortableModule, Utxo } from "./contracts";

export abstract class Rts {
private moduleFactory: ModuleFactory;
constructor(public readonly network: string = "mainnet") {
this.moduleFactory = new ModuleFactory(this);
}
abstract utxo(addr: any): any;
abstract utxo(addr: string): Promise<UtxoResult>;
abstract ecPair(ecPair: any): RtsECPair;
abstract get addresses(): Addresses;
abstract get crypto(): Crypto;
Expand All @@ -18,6 +18,11 @@ export abstract class Rts {
}
}

export interface UtxoResult {
success: boolean;
utxos: Utxo[];
}

export interface Addresses {
fromOutputScript(script: Buffer, network: string): string;
toHash160(addr: string): string;
Expand Down
4 changes: 2 additions & 2 deletions spedn-js/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spedn/sdk",
"version": "3.1.0",
"version": "3.1.1",
"description": "Spedn compiler SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -28,7 +28,7 @@
"author": "{o} Software",
"license": "MIT",
"dependencies": {
"@spedn/rts": "^3.1.0"
"@spedn/rts": "^3.1.1"
},
"devDependencies": {
"@types/node": "^14.0.27",
Expand Down
8 changes: 4 additions & 4 deletions spedn-js/sdk/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# yarn lockfile v1


"@spedn/rts@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@spedn/rts/-/rts-3.1.0.tgz#090118f57964a7e2c2ac6d9d61a47637ea10af5a"
integrity sha512-fYAGRiMKPE/UZjNTkwLYv3Nvry740H15yeC5+oNIYAT85SP3E3mY9VFyxjK6EUWFtTB+Px+/N73t0HgqBLs37w==
"@spedn/rts@^3.1.1":
version "3.1.1"
resolved "https://registry.yarnpkg.com/@spedn/rts/-/rts-3.1.1.tgz#b45b2cb49df994f448aca5c490696f942544699d"
integrity sha512-r3YjixGB/zLXq/kcnypx6oKSVm2fri+LW+PrExB3u/5Tu0gorUieHd33KWaKaSWYam3VKTBeyzPADoAq96EAtw==
dependencies:
lodash "^4.17.20"
varuint-bitcoin "1.1.2"
Expand Down
25 changes: 25 additions & 0 deletions spedn-js/tests/src/rest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import BCHJS from "@chris.troutner/bch-js";
import { P2PKHFactory } from "@spedn/rts";
import { BchJsRts } from "@spedn/rts-bchjs";
import { BitboxRts } from "@spedn/rts-bitbox";
import { BITBOX } from "bitbox-sdk";

const bchjs = new BCHJS({ restURL: "https://testnet3.fullstack.cash/v3/" });
const bitbox = new BITBOX({ restURL: "https://trest.bitcoin.com/v2/" });

describe.each([
new BchJsRts("testnet", bchjs)
// new BitboxRts("testnet", bitbox) // tREST seems to be dysfunctional
])("%s REST API", rts => {
it("can find coins", async () => {
const factory = new P2PKHFactory(rts);
// testnet : draw parade crater busy book swim soldier tragic exit feel top civil : m/44'/145'/0'/0/3
const account = factory.fromAddress("bchtest:qzca95r68adkek2we4jvws5dex6pwg3vt52mkhsqjg");
const [coin] = await account.findCoins("testnet");
expect(coin).toBeDefined();
expect(coin).toHaveProperty("utxo.txid");
expect(coin).toHaveProperty("utxo.vout");
expect(coin).toHaveProperty("utxo.satoshis");
expect(coin).toHaveProperty("utxo.height");
});
});
18 changes: 10 additions & 8 deletions spedn-js/tests/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -564,27 +564,29 @@
"@sinonjs/commons" "^1.7.0"

"@spedn/rts-bchjs@../rts-bchjs":
version "1.0.0"
version "3.1.1"
dependencies:
"@chris.troutner/bch-js" "^3.4.1"
"@spedn/rts" "../rts"
"@spedn/rts" "^3.1.1"

"@spedn/rts-bitbox@../rts-bitbox":
version "1.0.0"
version "3.1.1"
dependencies:
"@spedn/rts" "../rts"
"@spedn/rts" "^3.1.1"
bitbox-sdk "^8.11.2"

"@spedn/rts@../rts":
version "3.1.0"
"@spedn/rts@^3.1.1":
version "3.1.1"
resolved "https://registry.yarnpkg.com/@spedn/rts/-/rts-3.1.1.tgz#b45b2cb49df994f448aca5c490696f942544699d"
integrity sha512-r3YjixGB/zLXq/kcnypx6oKSVm2fri+LW+PrExB3u/5Tu0gorUieHd33KWaKaSWYam3VKTBeyzPADoAq96EAtw==
dependencies:
lodash "^4.17.20"
varuint-bitcoin "1.1.2"

"@spedn/sdk@../sdk":
version "1.0.0"
version "3.1.1"
dependencies:
"@spedn/rts" "../rts"
"@spedn/rts" "^3.1.1"

"@transloadit/[email protected]":
version "0.0.7"
Expand Down

0 comments on commit 34d6c65

Please sign in to comment.