11import 'should' ;
2+ import * as assert from 'assert' ;
3+ import nock = require( 'nock' ) ;
24
35import { TestBitGo , TestBitGoAPI } from '@bitgo/sdk-test' ;
46import { BitGoAPI } from '@bitgo/sdk-api' ;
7+ import { common } from '@bitgo/sdk-core' ;
58
69import { Tlnbtc } from '../../src/index' ;
710
811describe ( 'Lightning Bitcoin' , function ( ) {
912 let bitgo : TestBitGoAPI ;
1013 let basecoin : Tlnbtc ;
14+ let bgUrl : string ;
1115
1216 before ( function ( ) {
1317 bitgo = TestBitGo . decorate ( BitGoAPI , { env : 'test' } ) ;
1418 bitgo . safeRegister ( 'tlnbtc' , Tlnbtc . createInstance ) ;
1519 bitgo . initializeTestVars ( ) ;
1620 basecoin = bitgo . coin ( 'tlnbtc' ) as Tlnbtc ;
21+ bgUrl = common . Environments [ bitgo . getEnv ( ) ] . uri ;
1722 } ) ;
1823
1924 it ( 'should instantiate the coin' , function ( ) {
@@ -23,4 +28,110 @@ describe('Lightning Bitcoin', function () {
2328 it ( 'should return full name' , function ( ) {
2429 basecoin . getFullName ( ) . should . equal ( 'Testnet Lightning Bitcoin' ) ;
2530 } ) ;
31+
32+ describe ( 'isValidPub' , function ( ) {
33+ it ( 'should return true for valid xpub' , function ( ) {
34+ assert . strictEqual (
35+ basecoin . isValidPub (
36+ 'xpub661MyMwAqRbcGaE8M1N5i3fdBskDrwgU77TejReywexvb1sqCK1LhC2SETWp8XpPS2WDqyNywdgWo5kUTwkDv7qSe12xp4En7mcogZy95rQ'
37+ ) ,
38+ true
39+ ) ;
40+ } ) ;
41+
42+ it ( 'should return false for private key' , function ( ) {
43+ assert . strictEqual (
44+ basecoin . isValidPub (
45+ 'xprv9s21ZrQH143K469fEyq5LuitdqujTUxcjtY3w3FNPKRwiDYgemh69PhxPBrgBc2s9vn8yfR1YKitAyUEXRTinrjyxxH5Xe38McnJ5rXkeXn'
46+ ) ,
47+ false
48+ ) ;
49+ } ) ;
50+
51+ it ( 'should return false for invalid string' , function ( ) {
52+ assert . strictEqual ( basecoin . isValidPub ( 'not-a-pub' ) , false ) ;
53+ } ) ;
54+ } ) ;
55+
56+ describe ( 'isValidAddress' , function ( ) {
57+ it ( 'should accept valid compressed node pubkeys' , function ( ) {
58+ assert . strictEqual (
59+ basecoin . isValidAddress ( '02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619' ) ,
60+ true
61+ ) ;
62+ assert . strictEqual (
63+ basecoin . isValidAddress ( '03e7156ae33b0a208d0744199163177e909e80176e55d97a2f221ede0f934dd9ad' ) ,
64+ true
65+ ) ;
66+ } ) ;
67+
68+ it ( 'should reject invalid node pubkeys' , function ( ) {
69+ // wrong prefix
70+ assert . strictEqual (
71+ basecoin . isValidAddress ( '04eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619' ) ,
72+ false
73+ ) ;
74+ // too short
75+ assert . strictEqual (
76+ basecoin . isValidAddress ( '02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f28368' ) ,
77+ false
78+ ) ;
79+ // too long
80+ assert . strictEqual (
81+ basecoin . isValidAddress ( '02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f28368661900' ) ,
82+ false
83+ ) ;
84+ } ) ;
85+
86+ it ( 'should accept valid testnet bitcoin addresses' , function ( ) {
87+ // p2sh
88+ assert . strictEqual ( basecoin . isValidAddress ( '2NBSpUjBQUg4BmWUft8m2VePGDEZ2QBFM7X' ) , true ) ;
89+ // bech32
90+ assert . strictEqual ( basecoin . isValidAddress ( 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx' ) , true ) ;
91+ } ) ;
92+
93+ it ( 'should reject invalid addresses' , function ( ) {
94+ assert . strictEqual ( basecoin . isValidAddress ( 'not-an-address' ) , false ) ;
95+ assert . strictEqual ( basecoin . isValidAddress ( '' ) , false ) ;
96+ } ) ;
97+ } ) ;
98+
99+ describe ( 'isWalletAddress' , function ( ) {
100+ const walletId = 'wallet123' ;
101+ const validBitcoinAddress = '2NBSpUjBQUg4BmWUft8m2VePGDEZ2QBFM7X' ;
102+ const nodePubkey = '02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619' ;
103+
104+ afterEach ( function ( ) {
105+ nock . cleanAll ( ) ;
106+ } ) ;
107+
108+ it ( 'should return true when address exists on wallet' , async function ( ) {
109+ nock ( bgUrl )
110+ . get ( `/api/v2/tlnbtc/wallet/${ walletId } /address/${ encodeURIComponent ( validBitcoinAddress ) } ` )
111+ . reply ( 200 , { address : validBitcoinAddress } ) ;
112+
113+ const result = await basecoin . isWalletAddress ( { address : validBitcoinAddress , walletId } ) ;
114+ assert . strictEqual ( result , true ) ;
115+ } ) ;
116+
117+ it ( 'should return false when address does not exist on wallet' , async function ( ) {
118+ nock ( bgUrl )
119+ . get ( `/api/v2/tlnbtc/wallet/${ walletId } /address/${ encodeURIComponent ( validBitcoinAddress ) } ` )
120+ . reply ( 404 ) ;
121+
122+ const result = await basecoin . isWalletAddress ( { address : validBitcoinAddress , walletId } ) ;
123+ assert . strictEqual ( result , false ) ;
124+ } ) ;
125+
126+ it ( 'should return false for node pubkeys without querying API' , async function ( ) {
127+ const result = await basecoin . isWalletAddress ( { address : nodePubkey , walletId } ) ;
128+ assert . strictEqual ( result , false ) ;
129+ } ) ;
130+
131+ it ( 'should throw InvalidAddressError for invalid addresses' , async function ( ) {
132+ await assert . rejects ( ( ) => basecoin . isWalletAddress ( { address : 'invalid' , walletId } ) , {
133+ name : 'InvalidAddressError' ,
134+ } ) ;
135+ } ) ;
136+ } ) ;
26137} ) ;
0 commit comments