mirror of
https://github.com/ACINQ/eclair.git
synced 2025-03-12 19:01:39 +01:00
Add method to get address from pubkey script
This commit is contained in:
parent
72f0993a31
commit
f9521b001e
3 changed files with 65 additions and 3 deletions
|
@ -83,13 +83,13 @@ package object eclair {
|
|||
* with a conservative minimum weight of 400, we get a minimum feerate_per-kw of 253
|
||||
*
|
||||
* see https://github.com/ElementsProject/lightning/pull/1251
|
||||
**/
|
||||
* */
|
||||
val MinimumFeeratePerKw = 253
|
||||
|
||||
/**
|
||||
* minimum relay fee rate, in satoshi per kilo
|
||||
* bitcoin core uses virtual size and not the actual size in bytes, see above
|
||||
**/
|
||||
* */
|
||||
val MinimumRelayFeeRate = 1000
|
||||
|
||||
/**
|
||||
|
@ -150,6 +150,52 @@ package object eclair {
|
|||
}
|
||||
}
|
||||
|
||||
def addressFromPublicKeyScript(chainHash: ByteVector32, pubkeyScript: List[ScriptElt]): String = {
|
||||
val p2pkhPrefix = chainHash match {
|
||||
case Block.LivenetGenesisBlock.hash => Base58.Prefix.PubkeyAddress
|
||||
case Block.TestnetGenesisBlock.hash | Block.RegtestGenesisBlock.hash => Base58.Prefix.PubkeyAddressTestnet
|
||||
}
|
||||
val p2shPrefix = chainHash match {
|
||||
case Block.LivenetGenesisBlock.hash => Base58.Prefix.ScriptAddress
|
||||
case Block.TestnetGenesisBlock.hash | Block.RegtestGenesisBlock.hash => Base58.Prefix.ScriptAddressTestnet
|
||||
}
|
||||
val hrp = chainHash match {
|
||||
case Block.TestnetGenesisBlock.hash => "tb"
|
||||
case Block.RegtestGenesisBlock.hash => "bcrt"
|
||||
case Block.LivenetGenesisBlock.hash => "bc"
|
||||
}
|
||||
pubkeyScript match {
|
||||
case OP_DUP :: OP_HASH160 :: OP_PUSHDATA(pubkeyHash, _) :: OP_EQUALVERIFY :: OP_CHECKSIG :: Nil if pubkeyHash.size == 20 =>
|
||||
Base58Check.encode(p2pkhPrefix, pubkeyHash)
|
||||
case OP_HASH160 :: OP_PUSHDATA(scriptHash, _) :: OP_EQUAL :: Nil if scriptHash.size == 20 =>
|
||||
Base58Check.encode(p2shPrefix, scriptHash)
|
||||
case op :: OP_PUSHDATA(witnessProgram, _) :: Nil if witnessProgram.length >= 2 && witnessProgram.length <= 40 =>
|
||||
op match {
|
||||
case OP_0 =>
|
||||
require(witnessProgram.length == 20 || witnessProgram.length == 32, "witness v0 program length must be 20 or 32")
|
||||
Bech32.encodeWitnessAddress(hrp, 0, witnessProgram)
|
||||
case OP_1 => Bech32.encodeWitnessAddress(hrp, 1, witnessProgram)
|
||||
case OP_2 => Bech32.encodeWitnessAddress(hrp, 2, witnessProgram)
|
||||
case OP_3 => Bech32.encodeWitnessAddress(hrp, 3, witnessProgram)
|
||||
case OP_4 => Bech32.encodeWitnessAddress(hrp, 4, witnessProgram)
|
||||
case OP_5 => Bech32.encodeWitnessAddress(hrp, 5, witnessProgram)
|
||||
case OP_6 => Bech32.encodeWitnessAddress(hrp, 6, witnessProgram)
|
||||
case OP_7 => Bech32.encodeWitnessAddress(hrp, 7, witnessProgram)
|
||||
case OP_8 => Bech32.encodeWitnessAddress(hrp, 8, witnessProgram)
|
||||
case OP_9 => Bech32.encodeWitnessAddress(hrp, 9, witnessProgram)
|
||||
case OP_10 => Bech32.encodeWitnessAddress(hrp, 10, witnessProgram)
|
||||
case OP_11 => Bech32.encodeWitnessAddress(hrp, 11, witnessProgram)
|
||||
case OP_12 => Bech32.encodeWitnessAddress(hrp, 12, witnessProgram)
|
||||
case OP_13 => Bech32.encodeWitnessAddress(hrp, 13, witnessProgram)
|
||||
case OP_14 => Bech32.encodeWitnessAddress(hrp, 14, witnessProgram)
|
||||
case OP_15 => Bech32.encodeWitnessAddress(hrp, 15, witnessProgram)
|
||||
case OP_16 => Bech32.encodeWitnessAddress(hrp, 16, witnessProgram)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def addressFromPublicKeyScript(chainHash: ByteVector32, pubkeyScript: ByteVector): String = addressFromPublicKeyScript(chainHash, Script.parse(pubkeyScript))
|
||||
|
||||
implicit class LongToBtcAmount(l: Long) {
|
||||
// @formatter:off
|
||||
def msat: MilliSatoshi = MilliSatoshi(l)
|
||||
|
|
|
@ -114,4 +114,20 @@ class PackageSpec extends AnyFunSuite {
|
|||
assert(ShortChannelId(Long.MaxValue) < ShortChannelId(Long.MaxValue + 1))
|
||||
}
|
||||
|
||||
test("compute addresses from pubkey scripts") {
|
||||
Seq(
|
||||
("0014d0b19277b0f76c9512f26d77573fd31a8fd15fc7", Block.TestnetGenesisBlock.hash, "tb1q6zceyaas7akf2yhjd4m4w07nr28azh78gw79kk"),
|
||||
("00203287047df2aa7aade3f394790a9c9d6f9235943f48a012e8a9f2c3300ca4f2d1", Block.TestnetGenesisBlock.hash, "tb1qx2rsgl0j4fa2mclnj3us48yad7frt9plfzsp969f7tpnqr9y7tgsyprxej"),
|
||||
("76a914b17deefe2feab87fef7221cf806bb8ca61f00fa188ac", Block.TestnetGenesisBlock.hash, "mwhSm2SHhRhd19KZyaQLgJyAtCLnkbzWbf"),
|
||||
("a914d3cf9d04f4ecc36df8207b300e46bc6775fc84c087", Block.TestnetGenesisBlock.hash, "2NCZBGzKadAnLv1ijAqhrKavMuqvxqu18yY"),
|
||||
("00145cb882efd643b7d63ae133e4d5e88e10bd5a20d7", Block.LivenetGenesisBlock.hash, "bc1qtjug9m7kgwmavwhpx0jdt6ywzz745gxhxwyn8u"),
|
||||
("00208c2865c87ffd33fc5d698c7df9cf2d0fb39d93103c637a06dea32c848ebc3e1d", Block.LivenetGenesisBlock.hash, "bc1q3s5xtjrll5elchtf337lnnedp7eemycs833h5pk75vkgfr4u8cws3ytg02"),
|
||||
("76a914536ffa992491508dca0354e52f32a3a7a679a53a88ac", Block.LivenetGenesisBlock.hash, "18cBEMRxXHqzWWCxZNtU91F5sbUNKhL5PX"),
|
||||
("a91481b9ac6a59b53927da7277b5ad5460d781b365d987", Block.LivenetGenesisBlock.hash, "3DWwX7NYjnav66qygrm4mBCpiByjammaWy"),
|
||||
("5120eceb3f3cdbad5bda395a32910ff6195a89cc5ec7fbf34705512da6f81963b156", Block.RegtestGenesisBlock.hash, "bcrt1pan4n70xm44da5w26x2gslaset2yuchk8l0e5wp239kn0sxtrk9tql9wc8z"),
|
||||
("00144cae0f71665150b3ac0e0f2990222e99a0e470df", Block.RegtestGenesisBlock.hash, "bcrt1qfjhq7utx29gt8tqwpu5eqg3wnxswguxl57xpge")
|
||||
).foreach { case (script, blockHash, address) =>
|
||||
assert(addressFromPublicKeyScript(blockHash, ByteVector.fromValidHex(script)) == address)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
2
pom.xml
2
pom.xml
|
@ -68,7 +68,7 @@
|
|||
<akka.version>2.3.14</akka.version>
|
||||
<akka.http.version>10.0.11</akka.http.version>
|
||||
<sttp.version>1.3.9</sttp.version>
|
||||
<bitcoinlib.version>0.18.2</bitcoinlib.version>
|
||||
<bitcoinlib.version>0.18.3</bitcoinlib.version>
|
||||
<guava.version>24.0-android</guava.version>
|
||||
<kamon.version>2.1.0</kamon.version>
|
||||
</properties>
|
||||
|
|
Loading…
Add table
Reference in a new issue