33const util = require ( 'util' )
44const EventEmitter = require ( 'events' ) . EventEmitter
55const path = require ( 'path' )
6- const extend = require ( 'xtend/mutable' )
7- const clone = require ( 'xtend' )
86const reemit = require ( 're-emitter' )
97const map = require ( 'map-stream' )
108const mutexify = require ( 'mutexify' )
@@ -93,7 +91,8 @@ function Tradle (opts) {
9391 typeforce ( {
9492 network : types . network ,
9593 dir : typeforce . String ,
96- blockchain : typeforce . Object ,
94+ blockchain : typeforce . maybe ( types . blockchain ) ,
95+ getBlockchainAdapter : typeforce . maybe ( typeforce . Function ) ,
9796 identity : types . identity ,
9897 keeper : types . keeper ,
9998 keys : typeforce . Array ,
@@ -109,8 +108,8 @@ function Tradle (opts) {
109108 // }), opts.merkle)
110109 } , opts )
111110
112- extend ( this , DEFAULT_OPTS , opts )
113111 utils . bindFunctions ( this )
112+ utils . extend ( this , DEFAULT_OPTS , opts )
114113
115114 this . opts = opts
116115 this . _levelOpts = { db : this . leveldown }
@@ -284,6 +283,21 @@ Tradle.prototype._init = function _init () {
284283 } )
285284}
286285
286+ /**
287+ * Overwritten by constructor opt getBlockchainAdapter
288+ * @param {{ blockchain, networkName } } opts [description]
289+ */
290+ Tradle . prototype . getBlockchainAdapter = function ( opts ) {
291+ if ( this . blockchain ) {
292+ const { blockchain, networkName } = opts
293+ if ( blockchain === this . network . blockchain && networkName === this . network . name ) {
294+ return this . blockchain
295+ }
296+
297+ this . _debug ( 'missing blockchain adapter' , { blockchain, networkName } )
298+ }
299+ }
300+
287301/**
288302 * Log with a prefix
289303 * @private
@@ -464,7 +478,7 @@ Tradle.prototype.updateIdentity = function (opts, cb) {
464478 // })
465479
466480 let { identity, keys } = opts
467- identity = extend (
481+ identity = utils . extend (
468482 protocol . nextVersion ( this . identity ) ,
469483 identity ,
470484 { [ VERSION ] : ( this . identity [ VERSION ] || 0 ) + 1 }
@@ -857,9 +871,10 @@ Tradle.prototype._queueSend = function _queueSend (opts, cb) {
857871
858872 self . seals . findOne ( 'link' , oLink , function ( err , seal ) {
859873 if ( ! err && seal . txId ) {
874+ // formatted per tradle.Seal model
860875 done ( null , {
861- blockchain : self . network . blockchain ,
862- network : self . network . name ,
876+ blockchain : seal . blockchain ,
877+ network : seal . networkName ,
863878 basePubKey : seal . basePubKey . pub ,
864879 link : seal . link ,
865880 headerHash : seal . headerHash ,
@@ -906,7 +921,7 @@ Tradle.prototype._queueSend = function _queueSend (opts, cb) {
906921 }
907922 } )
908923
909- const msg = utils . clone ( opts . other || { } , meta , base )
924+ const msg = utils . extend ( { } , opts . other || { } , meta , base )
910925 const seal = results . maybeGetSeal
911926 if ( seal ) msg . seal = seal
912927
@@ -1100,6 +1115,10 @@ Tradle.prototype._receive = function _receive (msg, from, cb) {
11001115 if ( err ) return cb ( err )
11011116 if ( seal ) {
11021117 self . watchSeal ( {
1118+ chain : {
1119+ blockchain : seal . blockchain ,
1120+ networkName : seal . network || seal . networkName
1121+ } ,
11031122 headerHash : seal . headerHash ,
11041123 basePubKey : utils . getSealPubKey ( seal )
11051124 } )
@@ -1156,6 +1175,8 @@ Tradle.prototype.seal = function seal (opts, cb) {
11561175 // const sealPrevAddress = sealPrevPubKey &&
11571176 // utils.sealPrevAddress(basePubKey, link, self.networkName)
11581177
1178+ const blockchain = network . blockchain
1179+ const networkName = network . name
11591180 const sealProps = {
11601181 link,
11611182 prevLink : object [ PREVLINK ] ,
@@ -1167,8 +1188,8 @@ Tradle.prototype.seal = function seal (opts, cb) {
11671188 sealPubKey,
11681189 sealPrevPubKey,
11691190 amount : opts . amount ,
1170- blockchain : network . blockchain ,
1171- networkName : network . name
1191+ blockchain,
1192+ networkName
11721193 }
11731194
11741195 self . actions . writeSeal ( sealProps , err => {
@@ -1177,7 +1198,14 @@ Tradle.prototype.seal = function seal (opts, cb) {
11771198 cb ( null , sealProps )
11781199 } )
11791200
1180- self . watchSeal ( { object, basePubKey } )
1201+ self . watchSeal ( {
1202+ chain : {
1203+ blockchain,
1204+ networkName
1205+ } ,
1206+ object,
1207+ basePubKey
1208+ } )
11811209 } )
11821210 } )
11831211}
@@ -1191,21 +1219,22 @@ Tradle.prototype.seal = function seal (opts, cb) {
11911219 */
11921220Tradle . prototype . watchNextVersion = function ( opts , cb ) {
11931221 typeforce ( {
1222+ chain : types . blockchainIdentifier ,
11941223 link : typeforce . maybe ( typeforce . String ) ,
11951224 object : typeforce . maybe ( typeforce . Object ) ,
11961225 headerHash : typeforce . maybe ( typeforce . String ) ,
11971226 basePubKey : types . chainPubKey
11981227 } , opts )
11991228
1200- let { link, object, headerHash, basePubKey } = opts
1229+ let { chain , link, object, headerHash, basePubKey } = opts
12011230 if ( ! headerHash ) headerHash = protocol . headerHash ( object )
12021231 if ( ! link ) link = utils . hexLink ( object )
12031232
1204- const { network } = this
1205- const address = utils . sealPrevAddress ( { network, basePubKey, headerHash } )
1233+ const address = utils . sealPrevAddress ( { network : this . network , basePubKey, headerHash } )
12061234 this . _watch ( {
12071235 // the next version's previous is the current version
12081236 // the tx for next version will have a predictable seal based on the current version's link
1237+ chain,
12091238 link,
12101239 address,
12111240 basePubKey,
@@ -1224,19 +1253,21 @@ Tradle.prototype.watchNextVersion = function (opts, cb) {
12241253 */
12251254Tradle . prototype . watchSeal = function ( opts , cb ) {
12261255 typeforce ( {
1256+ chain : types . blockchainIdentifier ,
12271257 link : typeforce . maybe ( typeforce . String ) ,
12281258 object : typeforce . maybe ( typeforce . Object ) ,
12291259 headerHash : typeforce . maybe ( typeforce . String ) ,
12301260 basePubKey : types . chainPubKey
12311261 } , opts )
12321262
1233- let { link, object, headerHash, basePubKey } = opts
1263+ let { chain , link, object, headerHash, basePubKey } = opts
12341264 if ( ! headerHash ) headerHash = protocol . headerHash ( object )
12351265 if ( ! link ) link = utils . hexLink ( object )
12361266
12371267 const { network } = this
12381268 const address = utils . sealAddress ( { network, basePubKey, headerHash } )
12391269 this . _watch ( {
1270+ chain,
12401271 link,
12411272 address,
12421273 basePubKey,
@@ -1249,6 +1280,8 @@ Tradle.prototype.watchSeal = function (opts, cb) {
12491280 * watch an address for a seal for an object's current or next version
12501281 * @private
12511282 * @param {Object } opts
1283+ * @param {string } opts.blockchain blockchain to monitor
1284+ * @param {string } opts.networkName blockchain network to monitor
12521285 * @param {string } opts.headerHash headerHash to monitor
12531286 * @param {Object } opts.basePubKey [sealer's blockchain pubKey]{@link types#chainPubKey}
12541287 * @param {string } opts.watchType [watch type]{@link constants#watchType}
@@ -1259,6 +1292,7 @@ Tradle.prototype._watch = function (opts, cb) {
12591292 const self = this
12601293
12611294 typeforce ( {
1295+ chain : types . blockchainIdentifier ,
12621296 link : typeforce . String ,
12631297 headerHash : typeforce . String ,
12641298 basePubKey : types . chainPubKey ,
@@ -1295,6 +1329,7 @@ Tradle.prototype._watch = function (opts, cb) {
12951329 ] , function ( err ) {
12961330 if ( err ) return cb ( err )
12971331
1332+ opts = inlineChain ( opts )
12981333 self . actions . createWatch ( opts , cb )
12991334 } )
13001335}
@@ -1460,3 +1495,10 @@ function getLockId (identifier) {
14601495
14611496 throw new Error ( 'invalid lock id' )
14621497}
1498+
1499+ function inlineChain ( opts ) {
1500+ opts = utils . clone ( opts )
1501+ utils . extend ( opts , opts . chain )
1502+ delete opts . chain
1503+ return opts
1504+ }
0 commit comments