@@ -16,6 +16,9 @@ const WalletDB = require('../lib/wallet/walletdb');
1616const WorkerPool = require ( '../lib/workers/workerpool' ) ;
1717const Address = require ( '../lib/primitives/address' ) ;
1818const MTX = require ( '../lib/primitives/mtx' ) ;
19+ const ChainEntry = require ( '../lib/blockchain/chainentry' ) ;
20+ const { Resource} = require ( '../lib/dns/resource' ) ;
21+ const Block = require ( '../lib/primitives/block' ) ;
1922const Coin = require ( '../lib/primitives/coin' ) ;
2023const KeyRing = require ( '../lib/primitives/keyring' ) ;
2124const Input = require ( '../lib/primitives/input' ) ;
@@ -24,6 +27,7 @@ const Script = require('../lib/script/script');
2427const policy = require ( '../lib/protocol/policy' ) ;
2528const HDPrivateKey = require ( '../lib/hd/private' ) ;
2629const Wallet = require ( '../lib/wallet/wallet' ) ;
30+ const rules = require ( '../lib/covenants/rules' ) ;
2731const { forValue} = require ( './util/common' ) ;
2832
2933const KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt'
@@ -70,10 +74,26 @@ function fakeBlock(height) {
7074 time : 500000000 + ( height * ( 10 * 60 ) ) ,
7175 bits : 0 ,
7276 nonce : 0 ,
73- height : height
77+ height : height ,
78+ version : 0 ,
79+ witnessRoot : Buffer . alloc ( 32 ) ,
80+ treeRoot : Buffer . alloc ( 32 ) ,
81+ reservedRoot : Buffer . alloc ( 32 ) ,
82+ extraNonce : Buffer . alloc ( 24 ) ,
83+ mask : Buffer . alloc ( 32 )
7484 } ;
7585}
7686
87+ function curEntry ( wdb ) {
88+ return new ChainEntry ( curBlock ( wdb ) ) ;
89+ }
90+
91+ function nextEntry ( wdb ) {
92+ const cur = curEntry ( wdb ) ;
93+ const next = new Block ( nextBlock ( wdb ) ) ;
94+ return ChainEntry . fromBlock ( next , cur ) ;
95+ }
96+
7797function dummyInput ( ) {
7898 const hash = random . randomBytes ( 32 ) ;
7999 return Input . fromOutpoint ( new Outpoint ( hash , 0 ) ) ;
@@ -2279,4 +2299,131 @@ describe('Wallet', function() {
22792299 assert . equal ( confirmedCount , 1 ) ;
22802300 } ) ;
22812301 } ) ;
2302+
2303+ describe ( 'Wallet Name Claims' , function ( ) {
2304+ let wallet , update ;
2305+ const network = Network . get ( 'regtest' ) ;
2306+ const workers = new WorkerPool ( { enabled : false } ) ;
2307+ const wdb = new WalletDB ( { network, workers} ) ;
2308+ const lockup = 6800503496047 ;
2309+ const name = 'cloudflare' ;
2310+ const nameHash = rules . hashString ( name ) ;
2311+
2312+ before ( async ( ) => {
2313+ await wdb . open ( ) ;
2314+ wallet = await wdb . create ( ) ;
2315+
2316+ for ( let i = 0 ; i < 3 ; i ++ ) {
2317+ const entry = nextEntry ( wdb ) ;
2318+ await wdb . addBlock ( entry , [ ] ) ;
2319+ }
2320+
2321+ // Use a fresh wallet.
2322+ const bal = await wallet . getBalance ( ) ;
2323+ assert . equal ( bal . tx , 0 ) ;
2324+ assert . equal ( bal . coin , 0 ) ;
2325+ assert . equal ( bal . unconfirmed , 0 ) ;
2326+ assert . equal ( bal . confirmed , 0 ) ;
2327+ assert . equal ( bal . ulocked , 0 ) ;
2328+ assert . equal ( bal . clocked , 0 ) ;
2329+ } ) ;
2330+
2331+ after ( async ( ) => {
2332+ await wdb . close ( ) ;
2333+ } ) ;
2334+
2335+ it ( 'should not have any cloudflare state' , async ( ) => {
2336+ const nameinfo = await wallet . getNameState ( nameHash ) ;
2337+ assert . deepEqual ( nameinfo , null ) ;
2338+ } ) ;
2339+
2340+ it ( 'should confirm cloudflare CLAIM' , async ( ) => {
2341+ const claim = await wallet . sendFakeClaim ( 'cloudflare' ) ;
2342+ assert ( claim ) ;
2343+
2344+ const tx = claim . toTX ( network , wdb . state . height + 1 ) ;
2345+ const entry = nextEntry ( wdb ) ;
2346+ await wdb . addBlock ( entry , [ tx ] ) ;
2347+
2348+ const ns = await wallet . getNameState ( nameHash ) ;
2349+ const json = ns . getJSON ( wdb . state . height , network ) ;
2350+ assert . equal ( json . name , 'cloudflare' ) ;
2351+ assert . equal ( json . state , 'LOCKED' ) ;
2352+
2353+ const bal = await wallet . getBalance ( ) ;
2354+ assert . equal ( bal . tx , 1 ) ;
2355+ assert . equal ( bal . coin , 1 ) ;
2356+ assert . equal ( bal . unconfirmed , lockup ) ;
2357+ assert . equal ( bal . confirmed , lockup ) ;
2358+ assert . equal ( bal . ulocked , lockup ) ;
2359+ assert . equal ( bal . clocked , lockup ) ;
2360+ } ) ;
2361+
2362+ it ( 'should close the auction' , async ( ) => {
2363+ const ns = await wallet . getNameState ( nameHash ) ;
2364+ const json = ns . getJSON ( wdb . state . height , network ) ;
2365+ const { blocksUntilClosed} = json . stats ;
2366+
2367+ for ( let i = 0 ; i < blocksUntilClosed ; i ++ ) {
2368+ const entry = nextEntry ( wdb ) ;
2369+ await wdb . addBlock ( entry , [ ] ) ;
2370+ }
2371+
2372+ {
2373+ const ns = await wallet . getNameState ( nameHash ) ;
2374+ const json = ns . getJSON ( wdb . state . height , network ) ;
2375+ assert . equal ( json . name , 'cloudflare' ) ;
2376+ assert . equal ( json . state , 'CLOSED' ) ;
2377+ }
2378+ } ) ;
2379+
2380+ it ( 'should send an update for cloudflare' , async ( ) => {
2381+ const records = Resource . fromJSON ( {
2382+ records : [ { type : 'NS' , ns : 'ns1.easyhandshake.com.' } ]
2383+ } ) ;
2384+
2385+ update = await wallet . sendUpdate ( 'cloudflare' , records ) ;
2386+ const entry = nextEntry ( wdb ) ;
2387+ await wdb . addBlock ( entry , [ update ] ) ;
2388+
2389+ const ns = await wallet . getNameState ( nameHash ) ;
2390+ const json = ns . getJSON ( wdb . state . height , network ) ;
2391+ assert . equal ( json . name , 'cloudflare' ) ;
2392+
2393+ const resource = Resource . decode ( ns . data ) ;
2394+ assert . deepEqual ( records . toJSON ( ) , resource . toJSON ( ) ) ;
2395+
2396+ // The unconfirmed and confirmed values should
2397+ // take into account the transaction fee. Assert
2398+ // against the value of the newly created output.
2399+ const val = update . output ( 1 ) . value ;
2400+ const bal = await wallet . getBalance ( ) ;
2401+ assert . equal ( bal . tx , 2 ) ;
2402+ assert . equal ( bal . coin , 2 ) ;
2403+ assert . equal ( bal . unconfirmed , val ) ;
2404+ assert . equal ( bal . confirmed , val ) ;
2405+ assert . equal ( bal . ulocked , 0 ) ;
2406+ assert . equal ( bal . clocked , 0 ) ;
2407+ } ) ;
2408+
2409+ it ( 'should remove a block and update balances correctly' , async ( ) => {
2410+ const cur = curEntry ( wdb ) ;
2411+ await wdb . removeBlock ( cur ) ;
2412+
2413+ const bal = await wallet . getBalance ( ) ;
2414+ const val = update . output ( 1 ) . value ;
2415+ assert . equal ( bal . tx , 2 ) ;
2416+ assert . equal ( bal . coin , 2 ) ;
2417+
2418+ // The unconfirmed balance includes value in the mempool
2419+ // and the chain itself. The reorg'd tx can be included
2420+ // in another block so the unconfirmed total does not
2421+ // include the tx fee. That value has been effectively
2422+ // spent already.
2423+ assert . equal ( bal . unconfirmed , val ) ;
2424+ assert . equal ( bal . confirmed , lockup ) ;
2425+ assert . equal ( bal . ulocked , 0 ) ;
2426+ assert . equal ( bal . clocked , lockup ) ;
2427+ } ) ;
2428+ } ) ;
22822429} ) ;
0 commit comments