multi-scope: use BIP0049 instead of BIP0049Plus #33

Merged
roylee17 merged 1 commit from roylee/use-bip0049-instead-of-bip0049plus into master 2022-09-17 23:39:05 +02:00
8 changed files with 24 additions and 29 deletions

View file

@ -838,7 +838,7 @@ func lookupKeyScope(kind *string) (*waddrmgr.KeyScope, error) {
case "legacy": case "legacy":
return &waddrmgr.KeyScopeBIP0044, nil return &waddrmgr.KeyScopeBIP0044, nil
case "p2sh-segwit": case "p2sh-segwit":
return &waddrmgr.KeyScopeBIP0049Plus, nil return &waddrmgr.KeyScopeBIP0049, nil
case "bech32": case "bech32":
return &waddrmgr.KeyScopeBIP0084, nil return &waddrmgr.KeyScopeBIP0084, nil
} }

View file

@ -175,9 +175,7 @@ type accountInfo struct {
lastInternalAddr ManagedAddress lastInternalAddr ManagedAddress
// addrSchema serves as a way for an account to override its // addrSchema serves as a way for an account to override its
// corresponding address schema with a custom one. For example, this // corresponding address schema with a custom one.
// could be used to import accounts that use the traditional BIP-0049
// derivation scheme into our KeyScopeBIP-0049Plus manager.
addrSchema *ScopeAddrSchema addrSchema *ScopeAddrSchema
// masterKeyFingerprint represents the fingerprint of the root key // masterKeyFingerprint represents the fingerprint of the root key

View file

@ -142,10 +142,9 @@ type ScopeAddrSchema struct {
} }
var ( var (
// KeyScopeBIP0049Plus is the key scope of our modified BIP0049 // KeyScopeBIP0049 is the key scope for BIP0084 derivation. BIP0049
// derivation. We say this is BIP0049 "plus", as we'll actually use // will be used to derive all nested p2wkh addresses.
// p2wkh change all change addresses. KeyScopeBIP0049 = KeyScope{
KeyScopeBIP0049Plus = KeyScope{
Purpose: 49, Purpose: 49,
Coin: 140, Coin: 140,
} }
@ -168,7 +167,7 @@ var (
// DefaultKeyScopes is the set of default key scopes that will be // DefaultKeyScopes is the set of default key scopes that will be
// created by the root manager upon initial creation. // created by the root manager upon initial creation.
DefaultKeyScopes = []KeyScope{ DefaultKeyScopes = []KeyScope{
KeyScopeBIP0049Plus, KeyScopeBIP0049,
KeyScopeBIP0084, KeyScopeBIP0084,
KeyScopeBIP0044, KeyScopeBIP0044,
} }
@ -177,9 +176,9 @@ var (
// address schema for each scope type. This will be consulted during // address schema for each scope type. This will be consulted during
// the initial creation of the root key manager. // the initial creation of the root key manager.
ScopeAddrMap = map[KeyScope]ScopeAddrSchema{ ScopeAddrMap = map[KeyScope]ScopeAddrSchema{
KeyScopeBIP0049Plus: { KeyScopeBIP0049: {
ExternalAddrType: NestedWitnessPubKey, ExternalAddrType: NestedWitnessPubKey,
InternalAddrType: WitnessPubKey, InternalAddrType: NestedWitnessPubKey,
}, },
KeyScopeBIP0084: { KeyScopeBIP0084: {
ExternalAddrType: WitnessPubKey, ExternalAddrType: WitnessPubKey,
@ -192,9 +191,7 @@ var (
} }
// KeyScopeBIP0049AddrSchema is the address schema for the traditional // KeyScopeBIP0049AddrSchema is the address schema for the traditional
// BIP-0049 derivation scheme. This exists in order to support importing // BIP-0049 derivation scheme.
// accounts from other wallets that don't use our modified BIP-0049
// derivation scheme (internal addresses are P2WKH instead of NP2WKH).
KeyScopeBIP0049AddrSchema = ScopeAddrSchema{ KeyScopeBIP0049AddrSchema = ScopeAddrSchema{
ExternalAddrType: NestedWitnessPubKey, ExternalAddrType: NestedWitnessPubKey,
InternalAddrType: NestedWitnessPubKey, InternalAddrType: NestedWitnessPubKey,
@ -2425,7 +2422,7 @@ func (s *ScopedKeyManager) cloneKeyWithVersion(key *hdkeychain.ExtendedKey) (
switch s.scope { switch s.scope {
case KeyScopeBIP0044: case KeyScopeBIP0044:
version = HDVersionMainNetBIP0044 version = HDVersionMainNetBIP0044
case KeyScopeBIP0049Plus: case KeyScopeBIP0049:
version = HDVersionMainNetBIP0049 version = HDVersionMainNetBIP0049
case KeyScopeBIP0084: case KeyScopeBIP0084:
version = HDVersionMainNetBIP0084 version = HDVersionMainNetBIP0084
@ -2439,7 +2436,7 @@ func (s *ScopedKeyManager) cloneKeyWithVersion(key *hdkeychain.ExtendedKey) (
switch s.scope { switch s.scope {
case KeyScopeBIP0044: case KeyScopeBIP0044:
version = HDVersionTestNetBIP0044 version = HDVersionTestNetBIP0044
case KeyScopeBIP0049Plus: case KeyScopeBIP0049:
version = HDVersionTestNetBIP0049 version = HDVersionTestNetBIP0049
case KeyScopeBIP0084: case KeyScopeBIP0084:
version = HDVersionTestNetBIP0084 version = HDVersionTestNetBIP0084
@ -2454,7 +2451,7 @@ func (s *ScopedKeyManager) cloneKeyWithVersion(key *hdkeychain.ExtendedKey) (
// We use the mainnet versions for simnet keys when the keys // We use the mainnet versions for simnet keys when the keys
// belong to a key scope which simnet doesn't have a defined // belong to a key scope which simnet doesn't have a defined
// version for. // version for.
case KeyScopeBIP0049Plus: case KeyScopeBIP0049:
version = HDVersionMainNetBIP0049 version = HDVersionMainNetBIP0049
case KeyScopeBIP0084: case KeyScopeBIP0084:
version = HDVersionMainNetBIP0084 version = HDVersionMainNetBIP0084

View file

@ -38,7 +38,7 @@ func TestTxToOutputsDryRun(t *testing.T) {
defer cleanup() defer cleanup()
// Create an address we can use to send some coins to. // Create an address we can use to send some coins to.
keyScope := waddrmgr.KeyScopeBIP0049Plus keyScope := waddrmgr.KeyScopeBIP0049
addr, err := w.CurrentAddress(0, keyScope) addr, err := w.CurrentAddress(0, keyScope)
if err != nil { if err != nil {
t.Fatalf("unable to get current address: %v", addr) t.Fatalf("unable to get current address: %v", addr)
@ -230,7 +230,7 @@ func TestTxToOutputsRandom(t *testing.T) {
defer cleanup() defer cleanup()
// Create an address we can use to send some coins to. // Create an address we can use to send some coins to.
keyScope := waddrmgr.KeyScopeBIP0049Plus keyScope := waddrmgr.KeyScopeBIP0049
addr, err := w.CurrentAddress(0, keyScope) addr, err := w.CurrentAddress(0, keyScope)
if err != nil { if err != nil {
t.Fatalf("unable to get current address: %v", addr) t.Fatalf("unable to get current address: %v", addr)

View file

@ -49,7 +49,7 @@ func keyScopeFromPubKey(pubKey *hdkeychain.ExtendedKey,
switch *addrType { switch *addrType {
case waddrmgr.NestedWitnessPubKey: case waddrmgr.NestedWitnessPubKey:
return waddrmgr.KeyScopeBIP0049Plus, return waddrmgr.KeyScopeBIP0049,
&waddrmgr.KeyScopeBIP0049AddrSchema, nil &waddrmgr.KeyScopeBIP0049AddrSchema, nil
case waddrmgr.WitnessPubKey: case waddrmgr.WitnessPubKey:
@ -74,11 +74,11 @@ func keyScopeFromPubKey(pubKey *hdkeychain.ExtendedKey,
switch *addrType { switch *addrType {
case waddrmgr.NestedWitnessPubKey: case waddrmgr.NestedWitnessPubKey:
return waddrmgr.KeyScopeBIP0049Plus, return waddrmgr.KeyScopeBIP0049,
&waddrmgr.KeyScopeBIP0049AddrSchema, nil &waddrmgr.KeyScopeBIP0049AddrSchema, nil
case waddrmgr.WitnessPubKey: case waddrmgr.WitnessPubKey:
return waddrmgr.KeyScopeBIP0049Plus, nil, nil return waddrmgr.KeyScopeBIP0049, nil, nil
default: default:
return waddrmgr.KeyScope{}, nil, return waddrmgr.KeyScope{}, nil,
@ -370,7 +370,7 @@ func (w *Wallet) ImportPublicKey(pubKey *btcec.PublicKey,
var keyScope waddrmgr.KeyScope var keyScope waddrmgr.KeyScope
switch addrType { switch addrType {
case waddrmgr.NestedWitnessPubKey: case waddrmgr.NestedWitnessPubKey:
keyScope = waddrmgr.KeyScopeBIP0049Plus keyScope = waddrmgr.KeyScopeBIP0049
case waddrmgr.WitnessPubKey: case waddrmgr.WitnessPubKey:
keyScope = waddrmgr.KeyScopeBIP0084 keyScope = waddrmgr.KeyScopeBIP0084
default: default:

View file

@ -79,7 +79,7 @@ var (
"Y3BZdhdtUcw", "Y3BZdhdtUcw",
accountIndex: 0, accountIndex: 0,
addrType: waddrmgr.NestedWitnessPubKey, addrType: waddrmgr.NestedWitnessPubKey,
expectedScope: waddrmgr.KeyScopeBIP0049Plus, expectedScope: waddrmgr.KeyScopeBIP0049,
expectedAddr: "2N5YTxG9XtGXx1YyhZb7N2pwEjoZLLMHGKj", expectedAddr: "2N5YTxG9XtGXx1YyhZb7N2pwEjoZLLMHGKj",
expectedChangeAddr: "2N7wpz5Gy2zEJTvq2MAuU6BCTEBLXNQ8dUw", expectedChangeAddr: "2N7wpz5Gy2zEJTvq2MAuU6BCTEBLXNQ8dUw",
}, { }, {
@ -99,7 +99,7 @@ var (
"FD2KeY6G9", "FD2KeY6G9",
accountIndex: 9, accountIndex: 9,
addrType: waddrmgr.NestedWitnessPubKey, addrType: waddrmgr.NestedWitnessPubKey,
expectedScope: waddrmgr.KeyScopeBIP0049Plus, expectedScope: waddrmgr.KeyScopeBIP0049,
expectedAddr: "2NBCJ9WzGXZqpLpXGq3Hacybj3c4eHRcqgh", expectedAddr: "2NBCJ9WzGXZqpLpXGq3Hacybj3c4eHRcqgh",
expectedChangeAddr: "2N3bankFu6F3ZNU41iVJQqyS9MXqp9dvn1M", expectedChangeAddr: "2N3bankFu6F3ZNU41iVJQqyS9MXqp9dvn1M",
}, { }, {
@ -109,7 +109,7 @@ var (
"FD2KeY6G9", "FD2KeY6G9",
accountIndex: 9, accountIndex: 9,
addrType: waddrmgr.WitnessPubKey, addrType: waddrmgr.WitnessPubKey,
expectedScope: waddrmgr.KeyScopeBIP0049Plus, expectedScope: waddrmgr.KeyScopeBIP0049,
expectedAddr: "2NBCJ9WzGXZqpLpXGq3Hacybj3c4eHRcqgh", expectedAddr: "2NBCJ9WzGXZqpLpXGq3Hacybj3c4eHRcqgh",
expectedChangeAddr: "tb1qeqn05w2hfq6axpdprhs4y7x65gxkkvfvyxqk4u", expectedChangeAddr: "tb1qeqn05w2hfq6axpdprhs4y7x65gxkkvfvyxqk4u",
}, { }, {

View file

@ -43,7 +43,7 @@ func TestFundPsbt(t *testing.T) {
} }
// Also create a nested P2WKH address we can use to send some coins to. // Also create a nested P2WKH address we can use to send some coins to.
addr, err = w.CurrentAddress(0, waddrmgr.KeyScopeBIP0049Plus) addr, err = w.CurrentAddress(0, waddrmgr.KeyScopeBIP0049)
if err != nil { if err != nil {
t.Fatalf("unable to get current address: %v", addr) t.Fatalf("unable to get current address: %v", addr)
} }
@ -353,7 +353,7 @@ func TestFinalizePsbt(t *testing.T) {
} }
// Also create a nested P2WKH address we can send coins to. // Also create a nested P2WKH address we can send coins to.
addr, err = w.CurrentAddress(0, waddrmgr.KeyScopeBIP0049Plus) addr, err = w.CurrentAddress(0, waddrmgr.KeyScopeBIP0049)
if err != nil { if err != nil {
t.Fatalf("unable to get current address: %v", addr) t.Fatalf("unable to get current address: %v", addr)
} }

View file

@ -26,7 +26,7 @@ func TestComputeInputScript(t *testing.T) {
expectedScriptLen: 0, expectedScriptLen: 0,
}, { }, {
name: "BIP049 nested P2WKH", name: "BIP049 nested P2WKH",
scope: waddrmgr.KeyScopeBIP0049Plus, scope: waddrmgr.KeyScopeBIP0049,
expectedScriptLen: 23, expectedScriptLen: 23,
}} }}