bip84 account address getting

This commit is contained in:
timlucmiptev 2020-10-05 14:42:52 +03:00 committed by ixv
parent 2199dbb05c
commit 350c004411
3 changed files with 35 additions and 79 deletions

View File

@ -13,16 +13,25 @@ The below requires norsyr's fix to `decompress-point` in order to work.
## Handling XPubs ## Handling XPubs
**Import lib; optionally set up env** **Import lib; optionally set up env**
XPub is BIP84, mnemonic:
abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about
```
=b -build-file %/lib/btc-scratch/hoon
=xpub "zpub6rFR7y4Q2AijBEqTUquhVz398htDFrtymD9xYYfG1m4wAcvPhXNfE3EfH1r1ADqtfSdVCToUG868RvUUkgDKf31mGDtKsAYz2oz2AGutZYs"
```
### Getting BIP84 Address from `xpub`
```
(~(address bip84:b %main xpub))
```
### with `~norsyr-torryn`'s bip32 library
``` ```
=bip32 -build-file %/lib/bip32/hoon =bip32 -build-file %/lib/bip32/hoon
=ecc secp256k1:secp:crypto =ecc secp256k1:secp:crypto
=zpub "zpub6rFR7y4Q2AijBEqTUquhVz398htDFrtymD9xYYfG1m4wAcvPhXNfE3EfH1r1ADqtfSdVCToUG868RvUUkgDKf31mGDtKsAYz2oz2AGutZYs"
```
**with `~norsyr-torryn`'s bip32 library**
```
:: get 0 index in non-change account :: get 0 index in non-change account
`@ux`(compress-point:ecc pub:(derive-public:(derive-public:(from-extended:bip32 zpub) 0) 0)) `@ux`(compress-point:ecc pub:(derive-public:(derive-public:(from-extended:bip32 xpub) 0) 0))
``` ```
## BIP 173 (Bech32 Addresses) ## BIP 173 (Bech32 Addresses)
@ -51,7 +60,8 @@ Use `@uc` to make the Hash-160 into a BTC P2PKH address
`@uc`(hash-160:btc pubkey)`@uc`(hash-160:btc pubkey) `@uc`(hash-160:btc pubkey)`@uc`(hash-160:btc pubkey)
``` ```
### trailing zeros ### trailing zero
s
Need to test with this because it shows need to input num bytes Need to test with this because it shows need to input num bytes
``` ```
0x3.f3c1.3839.3683.93e7.0caf.4148.4775.b805.312d.58be.d157.1308.3d27.5cf5.6998.0100 0x3.f3c1.3839.3683.93e7.0caf.4148.4775.b805.312d.58be.d157.1308.3d27.5cf5.6998.0100

View File

@ -1,73 +0,0 @@
:: DEPRECATED: use lib/bip32.hoon
|%
+$ pubkey (list @ux)
+$ chaincode (list @ux)
+$ parsed-xpub [cc=chaincode pubk=pubkey]
+$ il-ir [il=(list @ux) ir=(list @ux)]
:: b[ytes]rip: 0x6261 -> ~[98 97]
::
++ big-endian-brip
|= a=@ux
^- (list @ux)
(flop (rip 3 a))
:: b[ytes]rap: ~[98 97] -> 0x6261
::
++ big-endian-brap
|= bytes=(list @ux)
^- @ux
(swp 3 (rap 3 bytes))
::
++ pubkey-to-point
|= =pubkey
^- pont:secp:crypto
~& >> "compressed pubkey length: {<(lent pubkey)>}"
%- decompress-point:secp256k1:secp:crypto
(big-endian-brap pubkey)
::
++ is-point
|= =pubkey ^- ?
-:(mule |.((pubkey-to-point pubkey)))
::
++ parse-xpub
|= xpub=tape
^- (unit parsed-xpub)
=/ as-atom=@ux
(de-base58:mimes:html xpub)
=/ bytes=(list @ux)
(big-endian-brip as-atom)
~& >> "parse-xpub, depth: {<(snag 4 bytes)>}"
=/ pp=parsed-xpub
[(swag [13 32] bytes) (swag [45 33] bytes)]
?: (is-point pubk.pp)
`pp
~
::
++ compute-i
|= [=parsed-xpub index=@ud]
^- il-ir
~| 'Public key cannot use a hardened index'
?> (lth index (bex 31))
:: "append" index to pubkey as 4 bytes
=/ data=@
%+ add
(lsh 3 4 (big-endian-brap pubk.parsed-xpub))
index
=/ chaincode=@
(big-endian-brap cc.parsed-xpub)
=/ i=(list @ux)
(big-endian-brip (hmac-sha512:hmac:crypto chaincode data))
=/ il=(list @ux) (swag [0 32] i)
=/ ir=(list @ux) (swag [32 32] i)
[il ir]
++ child-from-xpub
|= [xpub=tape index=@ud]
=, secp256k1:secp:crypto
=/ upx=(unit parsed-xpub)
(parse-xpub xpub)
?~ upx ~
=/ px=parsed-xpub u.upx
=/ is (compute-i px index)
(compress-point (jc-add (priv-to-pub (big-endian-brap il.is)) (pubkey-to-point pubk.px)))
--
:: `@ux`(compress-point:secp256k1:secp:crypto (jc-add:secp256k1:secp:crypto (pubkey-to-point:btca pubk.u.px) x))

19
lib/btc-scratch.hoon Normal file
View File

@ -0,0 +1,19 @@
:: btc-scratch.hoon
/+ bip32, btc
=+ ecc=secp256k1:secp:crypto
=, bech32:btc
|%
+$ bech32-address $%([%bech32 tape])
+$ address ?(@uc bech32-address)
++ bip84
|_ [network=network:btc xpub=tape]
++ address
|= [change=@ index=@]
^- bech32-address
?> =("zpub" (tape (scag 4 xpub))) :: only for bip84
=/ pubkey=@ux
%- compress-point:ecc
pub:(derive-public:(derive-public:(from-extended:bip32 xpub) change) index)
[%bech32 (need (encode-pubkey network pubkey))]
--
--