@@ -46,14 +46,17 @@ module.exports = (send) => {
4646 return callback ( err )
4747 }
4848
49- const node = new DAGNode ( result . Data , result . Links . map (
50- ( l ) => {
51- return new DAGLink ( l . Name , l . Size , new Buffer ( bs58 . decode ( l . Hash ) ) )
52- } ) )
53-
54- cache . set ( multihash , node )
49+ const links = result . Links . map ( ( l ) => {
50+ return new DAGLink ( l . Name , l . Size , new Buffer ( bs58 . decode ( l . Hash ) ) )
51+ } )
5552
56- callback ( null , node )
53+ DAGNode . create ( result . Data , links , ( err , node ) => {
54+ if ( err ) {
55+ return callback ( err )
56+ }
57+ cache . set ( multihash , node )
58+ callback ( null , node )
59+ } )
5760 } )
5861 } ) ,
5962
@@ -73,12 +76,19 @@ module.exports = (send) => {
7376
7477 if ( Buffer . isBuffer ( obj ) ) {
7578 if ( ! options . enc ) {
76- tmpObj = { Data : obj . toString ( ) , Links : [ ] }
79+ tmpObj = {
80+ Data : obj . toString ( ) ,
81+ Links : [ ]
82+ }
7783 }
7884 } else if ( obj . multihash ) {
7985 tmpObj = {
8086 Data : obj . data . toString ( ) ,
81- Links : obj . links . map ( ( l ) => { return l . toJSON ( ) } )
87+ Links : obj . links . map ( ( l ) => {
88+ const link = l . toJSON ( )
89+ link . hash = link . multihash
90+ return link
91+ } )
8292 }
8393 } else if ( typeof obj === 'object' ) {
8494 tmpObj . Data = obj . Data . toString ( )
@@ -125,23 +135,26 @@ module.exports = (send) => {
125135 } )
126136 return
127137 } else {
128- node = new DAGNode ( obj . Data , obj . Links )
129- }
130- next ( )
131-
132- function next ( ) {
133- node . toJSON ( ( err , nodeJSON ) => {
138+ DAGNode . create ( new Buffer ( obj . Data ) , obj . Links , ( err , _node ) => {
134139 if ( err ) {
135140 return callback ( err )
136141 }
137- if ( nodeJSON . Hash !== result . Hash ) {
138- return callback ( new Error ( 'Stored object was different from constructed object' ) )
139- }
142+ node = _node
143+ next ( )
144+ } )
145+ return
146+ }
147+ next ( )
140148
141- cache . set ( result . Hash , node )
149+ function next ( ) {
150+ const nodeJSON = node . toJSON ( )
151+ if ( nodeJSON . multihash !== result . Hash ) {
152+ const err = new Error ( 'multihashes do not match' )
153+ return callback ( err )
154+ }
142155
143- callback ( null , node )
144- } )
156+ cache . set ( result . Hash , node )
157+ callback ( null , node )
145158 }
146159 } )
147160 } ) ,
@@ -248,14 +261,15 @@ module.exports = (send) => {
248261 return callback ( err )
249262 }
250263
251- const node = new DAGNode ( )
252- node . toJSON ( ( err , nodeJSON ) => {
264+ DAGNode . create ( new Buffer ( 0 ) , ( err , node ) => {
253265 if ( err ) {
254266 return callback ( err )
255267 }
256268
257- if ( nodeJSON . Hash !== result . Hash ) {
258- return callback ( new Error ( 'Stored object was different from constructed object' ) )
269+ if ( node . toJSON ( ) . multihash !== result . Hash ) {
270+ console . log ( node . toJSON ( ) )
271+ console . log ( result )
272+ return callback ( new Error ( 'multihashes do not match' ) )
259273 }
260274
261275 callback ( null , node )
@@ -280,7 +294,11 @@ module.exports = (send) => {
280294
281295 send ( {
282296 path : 'object/patch/add-link' ,
283- args : [ multihash , dLink . name , bs58 . encode ( dLink . hash ) . toString ( ) ]
297+ args : [
298+ multihash ,
299+ dLink . name ,
300+ bs58 . encode ( dLink . multihash ) . toString ( )
301+ ]
284302 } , ( err , result ) => {
285303 if ( err ) {
286304 return callback ( err )
@@ -305,7 +323,10 @@ module.exports = (send) => {
305323
306324 send ( {
307325 path : 'object/patch/rm-link' ,
308- args : [ multihash , dLink . name ]
326+ args : [
327+ multihash ,
328+ dLink . name
329+ ]
309330 } , ( err , result ) => {
310331 if ( err ) {
311332 return callback ( err )
0 commit comments