Bit-Wasp / bitcoin-php

Bitcoin implementation in PHP
The Unlicense
1.05k stars 419 forks source link

could I get swgit address #863

Open lijixy opened 3 years ago

lijixy commented 3 years ago

Hi, how could get the swgit address ?

afk11 commented 3 years ago

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

lijixy commented 3 years ago

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.

afk11 commented 3 years ago

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!

atlaschiew commented 3 years ago

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

afk11 commented 3 years ago

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

atlaschiew commented 3 years ago

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

nicolanj commented 3 years ago
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());
afk11 commented 3 years ago

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