bitcoin-php icon indicating copy to clipboard operation
bitcoin-php copied to clipboard

could I get swgit address

Open lijixy opened this issue 3 years ago • 8 comments

Hi, how could get the swgit address ?

lijixy avatar Jan 06 '21 07:01 lijixy

What type of wallet are you building? It's hard to give an answer without knowing what standard you're following :)

afk11 avatar Jan 06 '21 15:01 afk11

What type of wallet are you building? It's hard to give an answer without knowing what standard you're following :)

I want to build segwit address and use it.

lijixy avatar Jan 07 '21 01:01 lijixy

So here's a snippet to convert a public key to a segwit address https://github.com/Bit-Wasp/bitcoin-php/blob/1.0/examples/addresstypes.php#L21-L24

But really I'm asking if you know about BIP32 already, and are interested in following a standard like BIP84? It's how you'd generate a segwit address that's compatible with real wallets.

bitcoin-php is a library and it doesn't provide a built in wallet interface to use, so you have to put that together yourself. So at least if you're following a standard I can tell you what's needed :)

Normal wallets use BIP39 for a mnemonic, then use BIP84 (or others), to derive multiple keys in a wallet using BIP32.

BIP39: https://github.com/Bit-Wasp/bitcoin-php/blob/1.0/examples/bip39.php

BIP84. This example is almost exactly what you want (https://github.com/Bit-Wasp/bitcoin-php/blob/1.0/examples/trezor.bip32.php) but it's not updated for BIP84 yet, but add this in getScriptPubKey:

case 84:
return ScriptFactory::scriptPubKey()->p2wkh($key->getPublicKey()->getPubKeyHash());

And then change this to 84: https://github.com/Bit-Wasp/bitcoin-php/blob/1.0/examples/trezor.bip32.php#L30

It'll show you some addresses, then look over the examples for signing a transaction.

I really really suggest you run bitcoind in regtest mode (mining blocks is instant, and if you need you can just delete the blockchain and start over), mining some blocks to yourself, sending + receiving funds to yourself, and then pay to an address you create with your software. Then try to spend it!

afk11 avatar Jan 08 '21 18:01 afk11

Hi, how could get the swgit address ?

this is most common way to get segwit address, https://www.btcschools.net/bitwasp/bitwasp_script_p2wpkh.php, hope it helps

atlaschiew avatar Jan 09 '21 02:01 atlaschiew

Omg that site is amazing @atlaschiew! The address generation and transaction examples are super. Well done.

afk11 avatar Jan 11 '21 16:01 afk11

Omg that site is amazing @atlaschiew! The address generation and transaction examples are super. Well done.

Hi, Thanks @afk11 for your hard work too in this richest bitcoin php i ever seen!

If btcschool is helpful, please star me in https://github.com/atlaschiew/btcschools :p

atlaschiew avatar Jan 12 '21 01:01 atlaschiew

case 84:
return new ScriptFactory::scriptPubKey()->p2wkh($key->getPublicKey()->getPubKeyHash());

Adding this addition for "84" causing the error "syntax error, unexpected 'scriptPubKey' (T_STRING), expecting variable (T_VARIABLE) or '$'" This works fine:

case 84:
return ScriptFactory::scriptPubKey()->p2wkh($key->getPublicKey()->getPubKeyHash());

nicolanj avatar Oct 10 '21 11:10 nicolanj

@nicolanj thank you, you are right! I will modify the snippet

afk11 avatar Oct 11 '21 14:10 afk11