witness_proof.js
function validateUnit(objUnit, bRequireDefinitionOrChange, cb2){
		var bFound = false;
		async.eachSeries(
			objUnit.authors,
			function(author, cb3){
				var address = author.address;
			//	if (arrWitnesses.indexOf(address) === -1) // not a witness - skip it
			//		return cb3();
				var definition_chash = assocDefinitionChashes[address];
				if (!definition_chash && arrWitnesses.indexOf(address) === -1) // not a witness - skip it
					return cb3();
				if (!definition_chash)
					throw Error("definition chash not known for address "+address+", unit "+objUnit.unit);
				if (author.definition){
					try{
						if (objectHash.getChash160(author.definition) !== definition_chash)
							return cb3("definition doesn't hash to the expected value");
					}
					catch(e){
						return cb3("failed to calc definition chash: " +e);
					}
					assocDefinitions[definition_chash] = author.definition;
					bFound = true;
				}

				function handleAuthor(){
					// FIX
					validation.validateAuthorSignaturesWithoutReferences(author, objUnit, assocDefinitions[definition_chash], function(err){
						if (err)
							return cb3(err);
						for (var i=0; i<objUnit.messages.length; i++){
							var message = objUnit.messages[i];
							if (message.app === 'address_definition_change' 
									&& (message.payload.address === address || objUnit.authors.length === 1 && objUnit.authors[0].address === address)){
								assocDefinitionChashes[address] = message.payload.definition_chash;
								bFound = true;
							}
						}
						cb3();
					});
				}

				if (assocDefinitions[definition_chash])
					return handleAuthor();
				storage.readDefinition(db, definition_chash, {
					ifFound: function(arrDefinition){
						assocDefinitions[definition_chash] = arrDefinition;
						handleAuthor();
					},
					ifDefinitionNotFound: function(d){
						throw Error("definition "+definition_chash+" not found, address "+address+", my witnesses "+arrWitnesses.join(', ')+", unit "+objUnit.unit);
					}
				});
			},
			function(err){
				if (err)
					return cb2(err);
				if (bRequireDefinitionOrChange && !bFound)
					return cb2("neither definition nor change");
				cb2();
			}
		); // each authors
	}
function validateUnit(objUnit, bRequireDefinitionOrChange, cb2){
		var bFound = false;
		async.eachSeries(
			objUnit.authors,
			function(author, cb3){
				var address = author.address;
			//	if (arrWitnesses.indexOf(address) === -1) // not a witness - skip it
			//		return cb3();
				var definition_chash = assocDefinitionChashes[address];
				if (!definition_chash && arrWitnesses.indexOf(address) === -1) // not a witness - skip it
					return cb3();
				if (!definition_chash)
					throw Error("definition chash not known for address "+address+", unit "+objUnit.unit);
				if (author.definition){
					try{
						if (objectHash.getChash160(author.definition) !== definition_chash)
							return cb3("definition doesn't hash to the expected value");
					}
					catch(e){
						return cb3("failed to calc definition chash: " +e);
					}
					assocDefinitions[definition_chash] = author.definition;
					bFound = true;
				}

				function handleAuthor(){
					// FIX
					validation.validateAuthorSignaturesWithoutReferences(author, objUnit, assocDefinitions[definition_chash], function(err){
						if (err)
							return cb3(err);
						for (var i=0; i<objUnit.messages.length; i++){
							var message = objUnit.messages[i];
							if (message.app === 'address_definition_change' 
									&& (message.payload.address === address || objUnit.authors.length === 1 && objUnit.authors[0].address === address)){
								assocDefinitionChashes[address] = message.payload.definition_chash;
								bFound = true;
							}
						}
						cb3();
					});
				}

				if (assocDefinitions[definition_chash])
					return handleAuthor();
				storage.readDefinition(db, definition_chash, {
					ifFound: function(arrDefinition){
						assocDefinitions[definition_chash] = arrDefinition;
						handleAuthor();
					},
					ifDefinitionNotFound: function(d){
						throw Error("definition "+definition_chash+" not found, address "+address+", my witnesses "+arrWitnesses.join(', ')+", unit "+objUnit.unit);
					}
				});
			},
			function(err){
				if (err)
					return cb2(err);
				if (bRequireDefinitionOrChange && !bFound)
					return cb2("neither definition nor change");
				cb2();
			}
		); // each authors
	}
function validateUnit ( objUnit , bRequireDefinitionOrChange , cb2 ) { var bFound = false ; async . eachSeries ( objUnit . authors , function ( author , cb3 ) { var address = author . address ; //	if (arrWitnesses.indexOf(address) === -1) // not a witness - skip it //		return cb3(); var definition_chash = assocDefinitionChashes [ address ] ; if ( ! definition_chash && arrWitnesses . indexOf ( address ) === - 1 ) // not a witness - skip it return cb3 ( ) ; if ( ! definition_chash ) throw Error ( "definition chash not known for address " + address + ", unit " + objUnit . unit ) ; if ( author . definition ) { try { if ( objectHash . getChash160 ( author . definition ) !== definition_chash ) return cb3 ( "definition doesn't hash to the expected value" ) ; } catch ( e ) { return cb3 ( "failed to calc definition chash: " + e ) ; } assocDefinitions [ definition_chash ] = author . definition ; bFound = true ; } function handleAuthor ( ) { // FIX validation . validateAuthorSignaturesWithoutReferences ( author , objUnit , assocDefinitions [ definition_chash ] , function ( err ) { if ( err ) return cb3 ( err ) ; for ( var i = 0 ; i < objUnit . messages . length ; i ++ ) { var message = objUnit . messages [ i ] ; if ( message . app === 'address_definition_change' && ( message . payload . address === address || objUnit . authors . length === 1 && objUnit . authors [ 0 ] . address === address ) ) { assocDefinitionChashes [ address ] = message . payload . definition_chash ; bFound = true ; } } cb3 ( ) ; } ) ; } if ( assocDefinitions [ definition_chash ] ) return handleAuthor ( ) ; storage . readDefinition ( db , definition_chash , { ifFound : function ( arrDefinition ) { assocDefinitions [ definition_chash ] = arrDefinition ; handleAuthor ( ) ; } , ifDefinitionNotFound : function ( d ) { throw Error ( "definition " + definition_chash + " not found, address " + address + ", my witnesses " + arrWitnesses . join ( ', ' ) + ", unit " + objUnit . unit ) ; } } ) ; } , function ( err ) { if ( err ) return cb2 ( err ) ; if ( bRequireDefinitionOrChange && ! bFound ) return cb2 ( "neither definition nor change" ) ; cb2 ( ) ; } ) ; // each authors }
keyed by address checks signatures and updates definitions
keyed by address checks signatures and updates definitions