urbit / urbit-key-generation

Key derivation and HD wallet generation functions for Urbit
MIT License
15 stars 8 forks source link

deriveNetworkSeed is borked #58

Closed philipcmonk closed 5 years ago

philipcmonk commented 5 years ago

deriveNetworkSeed gives the same output for all revision numbers greater than 0. I think it has something to do with the dhash definition. If I swap that line for:

const dhash = revision === 0 ? hash : await sha256(Buffer.from(hash))

then I get different values for different revisions. I don't understand buffers here, but the sha256 function looks like it's calling Buffer.from on the arguments anyway, so I can't figure out why that would be necessary.

@jtobin Could you take a look at this?

jtobin commented 5 years ago

It's because this language is for psychopaths.

> foo
ArrayBuffer { byteLength: 32 }
> Buffer.from(foo)
<Buffer 2c f2 4d ba 5f b0 a3 0e 26 e8 3b 2a c5 b9 e2 9e 1b 16 1e 5c 1f a7 42 5e 73 04 33 62 93 8b 98 24>
> [foo]
[ ArrayBuffer { byteLength: 32 } ]
> [ Buffer.from(foo) ]
[ <Buffer 2c f2 4d ba 5f b0 a3 0e 26 e8 3b 2a c5 b9 e2 9e 1b 16 1e 5c 1f a7 42 5e 73 04 33 62 93 8b 98 24> ]
> [ foo ].map(Buffer.from)
[ <Buffer > ]
jtobin commented 5 years ago

I guess I'll change it to explicitly take an argument:

> [foo].map(x => Buffer.from(x))
[ <Buffer 2c f2 4d ba 5f b0 a3 0e 26 e8 3b 2a c5 b9 e2 9e 1b 16 1e 5c 1f a7 42 5e 73 04 33 62 93 8b 98 24> ]