diff --git a/examples/sandbox/index.ts b/examples/sandbox/index.ts index 0e1e91325..162c88983 100644 --- a/examples/sandbox/index.ts +++ b/examples/sandbox/index.ts @@ -667,19 +667,19 @@ $getXpubs.each(function () { coin: "Bitcoin", addressNList: [2147483732, 2147483648, 2147483648], curve: "secp256k1", - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, }, { coin: "Bitcoin", addressNList: [2147483697, 2147483648, 2147483648], curve: "secp256k1", - scriptType: core.BTCInputScriptType.SpendP2SHWitness, + scriptType: core.BTCScriptType.Segwit, }, { coin: "Bitcoin", addressNList: [2147483692, 2147483648, 2147483648], curve: "secp256k1", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }, ]; const bchGetPublicKeysInput = [ @@ -688,7 +688,7 @@ $getXpubs.each(function () { curve: "secp256k1", showDisplay: true, // Not supported by TrezorConnect or Ledger, but KeepKey should do it coin: "BitcoinCash", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }, ]; const ltcGetPublicKeysInput = [ @@ -698,7 +698,7 @@ $getXpubs.each(function () { curve: "secp256k1", showDisplay: true, // Not supported by TrezorConnect or Ledger, but KeepKey should do it coin: "Litecoin", - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, }, // SegwitP2sh { @@ -706,7 +706,7 @@ $getXpubs.each(function () { curve: "secp256k1", showDisplay: true, // Not supported by TrezorConnect or Ledger, but KeepKey should do it coin: "Litecoin", - scriptType: core.BTCInputScriptType.SpendP2SHWitness, + scriptType: core.BTCScriptType.Segwit, }, // P2Pksh { @@ -714,7 +714,7 @@ $getXpubs.each(function () { curve: "secp256k1", showDisplay: true, // Not supported by TrezorConnect or Ledger, but KeepKey should do it coin: "Litecoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }, ]; const dogeGetPublicKeysInput = [ @@ -723,7 +723,7 @@ $getXpubs.each(function () { curve: "secp256k1", showDisplay: true, // Not supported by TrezorConnect or Ledger, but KeepKey should do it coin: "Dogecoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }, ]; const hardenedPathGetPublicKeysInput = [ @@ -1380,7 +1380,7 @@ $thorchainSignSwap.on("click", async (e) => { const inputs = [ { addressNList: [0x80000000 + 44, 0x80000000 + 0, 0x80000000 + 0, 0, 0], - scriptType: core.BTCInputScriptType.SpendAddress as const, + scriptType: core.BTCScriptType.Legacy as const, amount: String(10000), vout: 1, txid: txid, @@ -1532,7 +1532,7 @@ $thorchainSignAddLiquidity.on("click", async (e) => { const inputs = [ { addressNList: [0x80000000 + 44, 0x80000000 + 0, 0x80000000 + 0, 0, 0], - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(10000), vout: 1, txid: txid, @@ -2940,7 +2940,7 @@ $btcAddr.on("click", async (e) => { const res = await wallet.btcGetAddress({ addressNList: [0x80000000 + 44, 0x80000000 + 0, 0x80000000 + 0, 0, 0], coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, showDisplay: true, }); $btcResults.val(res); @@ -2965,7 +2965,7 @@ $btcTx.on("click", async (e) => { const inputs = [ { addressNList: [0x80000000 + 44, 0x80000000 + 0, 0x80000000 + 0, 0, 0], - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(10000), vout: 1, txid: txid, @@ -2978,7 +2978,7 @@ $btcTx.on("click", async (e) => { { address: "1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1", addressType: core.BTCOutputAddressType.Spend, - scriptType: core.BTCOutputScriptType.PayToAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(10000 - 1000), isChange: false, }, @@ -3009,7 +3009,7 @@ $btcSign.on("click", async (e) => { const res = await wallet.btcSignMessage({ addressNList: core.bip32ToAddressNList("m/44'/0'/0'/0/0"), coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, message: "Hello World", }); $btcResults.val(res.address + " " + res.signature); @@ -3052,7 +3052,7 @@ const $ltcSign = $("#ltcSign"); const $ltcResults = $("#ltcResults"); const ltcBip44 = { - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, addressNList: [0x80000000 + 44, 0x80000000 + 2, 0x80000000 + 0, 0, 0], }; @@ -3090,7 +3090,7 @@ $ltcTx.on("click", async (e) => { const inputs = [ { addressNList: ltcBip44.addressNList, - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(2160258), vout: 0, txid, @@ -3104,7 +3104,7 @@ $ltcTx.on("click", async (e) => { { address: "LLe4PciAJgMMJSAtQQ5nkC13t6SSMmERJ3", addressType: core.BTCOutputAddressType.Spend, - scriptType: core.BTCOutputScriptType.PayToAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(261614), isChange: false, }, @@ -3134,7 +3134,7 @@ $ltcSign.on("click", async (e) => { const res = await wallet.btcSignMessage({ addressNList: ltcBip44.addressNList, coin: "Litecoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, message: "Hello World", }); $ltcResults.val(res.address + " " + res.signature); @@ -3158,7 +3158,7 @@ const $dogeTx = $("#dogeTx"); const $dogeResults = $("#dogeResults"); const dogeBip44 = { - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, addressNList: [0x80000000 + 44, 0x80000000 + 3, 0x80000000 + 0], }; $dogeAddr.on("click", async (e) => { @@ -3195,7 +3195,7 @@ $dogeTx.on("click", async (e) => { const inputs = [ { addressNList: dogeBip44.addressNList.concat([0, 0]), - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(35577380098), vout: 1, txid: txid, @@ -3209,7 +3209,7 @@ $dogeTx.on("click", async (e) => { { address: "DMEHVGRsELY5zyYbfgta3pAhedKGeaDeJd", addressType: core.BTCOutputAddressType.Spend, - scriptType: core.BTCOutputScriptType.PayToAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(35577380098), isChange: false, }, @@ -3241,7 +3241,7 @@ const $bchTx = $("#bchTx"); const $bchResults = $("#bchResults"); const bchBip44 = { - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, addressNList: [0x80000000 + 44, 0x80000000 + 145, 0x80000000 + 0], }; @@ -3279,7 +3279,7 @@ $bchTx.on("click", async (e) => { const inputs = [ { addressNList: bchBip44.addressNList.concat([0, 0]), - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(13813640), vout: 0, txid: txid, @@ -3290,11 +3290,9 @@ $bchTx.on("click", async (e) => { const outputs = [ { - address: (await wallet.btcSupportsScriptType("BitcoinCash", core.BTCInputScriptType.CashAddr)) - ? "bitcoincash:qq5mg2xtp9y5pvvgy7m4k2af5a7s5suulueyywgvnf" - : "14oWXZFPhgP9DA3ggPzhHpUUaikDSjAuMC", + address: "bitcoincash:qq5mg2xtp9y5pvvgy7m4k2af5a7s5suulueyywgvnf", addressType: core.BTCOutputAddressType.Spend, - scriptType: core.BTCOutputScriptType.PayToAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(13813640), isChange: false, }, @@ -3326,7 +3324,7 @@ const $dashTx = $("#dashTx"); const $dashResults = $("#dashResults"); const dashBip44 = { - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, addressNList: [0x80000000 + 44, 0x80000000 + 5, 0x80000000 + 0], }; @@ -3364,7 +3362,7 @@ $dashTx.on("click", async (e) => { const inputs = [ { addressNList: dashBip44.addressNList.concat([0, 0]), - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(4954359), vout: 0, txid: txid, @@ -3378,7 +3376,7 @@ $dashTx.on("click", async (e) => { { address: "XexybzTUtH9V9eY4UJN2aCcBT3utan5C8N", addressType: core.BTCOutputAddressType.Spend, - scriptType: core.BTCOutputScriptType.PayToAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(4000000), isChange: false, }, @@ -3409,7 +3407,7 @@ const $dgbTx = $("#dgbTx"); const $dgbResults = $("#dgbResults"); const dgbBip44 = { - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, addressNList: [0x80000000 + 44, 0x80000000 + 20, 0x80000000 + 0], }; @@ -3449,7 +3447,7 @@ $dgbTx.on("click", async (e) => { const inputs = [ { addressNList: dgbBip44.addressNList.concat([0, 0]), - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(480000000), vout: 15, txid: "be150359df4123b379f1f12de978bfced92644645da17b97c7613879f4306a90", @@ -3458,7 +3456,7 @@ $dgbTx.on("click", async (e) => { }, { addressNList: dgbBip44.addressNList.concat([0, 0]), - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(10000000), vout: 0, txid: "528ec23eaf123282e9bce297ebb3edfb05e8b4d5875cbc9c271a98d72a202340", @@ -3471,14 +3469,14 @@ $dgbTx.on("click", async (e) => { { address: "SWpe93hQL2pLUDLy7swsDPWQJGCHSsgmun", addressType: null, - scriptType: core.BTCOutputScriptType.PayToMultisig, + scriptType: core.BTCScriptType.LegacyMultisig, amount: String(400000000), isChange: false, }, { address: "DNLcBry65dHehGExGYjBkM8kxDYr7mZ3BT", addressType: null, - scriptType: core.BTCOutputScriptType.PayToAddress, + scriptType: core.BTCScriptType.Legacy, relpath: "1/9", amount: String(90000000), isChange: true, @@ -3527,7 +3525,7 @@ $btcAddrSegWit.on("click", async (e) => { const res = await wallet.btcGetAddress({ addressNList: [0x80000000 + 49, 0x80000000 + 0, 0x80000000 + 0, 0, 0], coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendP2SHWitness, + scriptType: core.BTCScriptType.Segwit, showDisplay: true, }); @@ -3550,7 +3548,7 @@ $btcAddrSegWitNative.on("click", async (e) => { const res = await wallet.btcGetAddress({ addressNList: [0x80000000 + 84, 0x80000000 + 0, 0x80000000 + 0, 0, 0], coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, showDisplay: true, }); $btcResultsSegWit.val(res); @@ -3577,7 +3575,7 @@ $btcTxSegWit.on("click", async (e) => { amount: String(100000), vout: 0, txid: txid, - scriptType: core.BTCInputScriptType.SpendP2SHWitness, + scriptType: core.BTCScriptType.Segwit, tx: btcSegWitTxJson, hex, }, @@ -3587,7 +3585,7 @@ $btcTxSegWit.on("click", async (e) => { { address: "3Eq3agTHEhMCC8sZHnJJcCcZFB7BBSJKWr", addressType: core.BTCOutputAddressType.Spend, - scriptType: core.BTCOutputScriptType.PayToAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(89869), isChange: false, }, @@ -3629,7 +3627,7 @@ $btcTxSegWitNative.on("click", async (e) => { amount: String(9426), vout: 0, txid: txid, - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, tx: btcBech32TxJson, hex, }, @@ -3639,7 +3637,7 @@ $btcTxSegWitNative.on("click", async (e) => { { address: "bc1qc5dgazasye0yrzdavnw6wau5up8td8gdqh7t6m", addressType: core.BTCOutputAddressType.Spend, - scriptType: core.BTCOutputScriptType.PayToAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(1337), isChange: false, }, diff --git a/integration/src/bitcoin/bitcoin.ts b/integration/src/bitcoin/bitcoin.ts index 1a38b4a31..c83824c29 100644 --- a/integration/src/bitcoin/bitcoin.ts +++ b/integration/src/bitcoin/bitcoin.ts @@ -113,42 +113,43 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa coin: "Bitcoin", addressNList: core.bip32ToAddressNList(`m/44'/0'/0'`), curve: "secp256k1", + scriptType: core.BTCScriptType.Legacy, }, { coin: "Bitcoin", addressNList: core.bip32ToAddressNList(`m/49'/0'/0'`), curve: "secp256k1", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }, { coin: "Bitcoin", addressNList: core.bip32ToAddressNList(`m/49'/0'/0'`), curve: "secp256k1", - scriptType: core.BTCInputScriptType.SpendP2SHWitness, + scriptType: core.BTCScriptType.Segwit, }, { coin: "Bitcoin", addressNList: core.bip32ToAddressNList(`m/49'/0'/0'`), curve: "secp256k1", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }, { coin: "Bitcoin", addressNList: core.bip32ToAddressNList(`m/84'/0'/0'`), curve: "secp256k1", - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, }, { coin: "Bitcoin", addressNList: core.bip32ToAddressNList(`m/0'/0'/0'`), curve: "secp256k1", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }, { coin: "Litecoin", addressNList: core.bip32ToAddressNList(`m/0'/0'/0'`), curve: "secp256k1", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }, ]) ).toEqual([ @@ -182,47 +183,17 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa if (!wallet || portis.isPortis(wallet)) return; await each( [ - [ - "Show", - "Bitcoin", - "m/44'/0'/0'/0/0", - core.BTCInputScriptType.SpendAddress, - "1FH6ehAd5ZFXCM1cLGzHxK1s4dGdq1JusM", - ], - [ - "Show", - "Bitcoin", - "m/49'/0'/0'/0/0", - core.BTCInputScriptType.SpendP2SHWitness, - "3AnYTd2FGxJLNKL1AzxfW3FJMntp9D2KKX", - ], - [ - "Tell", - "Bitcoin", - "m/49'/0'/0'/0/0", - core.BTCInputScriptType.SpendP2SHWitness, - "3AnYTd2FGxJLNKL1AzxfW3FJMntp9D2KKX", - ], - [ - "Tell", - "Litecoin", - "m/49'/2'/0'/0/0", - core.BTCInputScriptType.SpendP2SHWitness, - "MFoQRU1KQq365Sy3cXhix3ygycEU4YWB1V", - ], - [ - "Tell", - "Dash", - "m/44'/5'/0'/0/0", - core.BTCInputScriptType.SpendAddress, - "XxKhGNv6ECbqVswm9KYcLPQnyWgZ86jJ6Q", - ], + ["Show", "Bitcoin", "m/44'/0'/0'/0/0", core.BTCScriptType.Legacy, "1FH6ehAd5ZFXCM1cLGzHxK1s4dGdq1JusM"], + ["Show", "Bitcoin", "m/49'/0'/0'/0/0", core.BTCScriptType.Segwit, "3AnYTd2FGxJLNKL1AzxfW3FJMntp9D2KKX"], + ["Tell", "Bitcoin", "m/49'/0'/0'/0/0", core.BTCScriptType.Segwit, "3AnYTd2FGxJLNKL1AzxfW3FJMntp9D2KKX"], + ["Tell", "Litecoin", "m/49'/2'/0'/0/0", core.BTCScriptType.Segwit, "MFoQRU1KQq365Sy3cXhix3ygycEU4YWB1V"], + ["Tell", "Dash", "m/44'/5'/0'/0/0", core.BTCScriptType.Legacy, "XxKhGNv6ECbqVswm9KYcLPQnyWgZ86jJ6Q"], ], async (args) => { const mode = args[0] as string; const coin = args[1] as core.Coin; const path = args[2] as string; - const scriptType = args[3] as core.BTCInputScriptType; + const scriptType = args[3] as core.BTCScriptType; const expected = args[4] as string; // Non-EVM things are a pain to test with snaps on test env, this wasn't tested before and still isn't @@ -284,7 +255,7 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa const inputs: core.BTCSignTxInputUnguarded[] = [ { addressNList: core.bip32ToAddressNList("m/0"), - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(390000), vout: 0, txid: "d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882", @@ -296,7 +267,7 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa { address: "1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1", addressType: core.BTCOutputAddressType.Spend, - // scriptType: core.BTCOutputScriptType.PayToAddress, + // scriptType: core.BTCScriptType.Legacy, amount: String(390000 - 10000), isChange: false, }, @@ -360,7 +331,7 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa const inputs: core.BTCSignTxInputUnguarded[] = [ { addressNList: core.bip32ToAddressNList("m/0"), - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(390000), vout: 0, txid: "d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882", @@ -378,7 +349,7 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa { addressNList: core.bip32ToAddressNList("m/44'/0'/0'/0/0"), addressType: core.BTCOutputAddressType.Change, - scriptType: core.BTCOutputScriptType.PayToAddress, + scriptType: core.BTCScriptType.Legacy, amount: String(9000), isChange: true, }, @@ -426,7 +397,7 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa const res = wallet.btcSignMessage({ addressNList: core.bip32ToAddressNList("m/44'/0'/0'/0/0"), coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, message: "Hello World", }); @@ -534,20 +505,20 @@ export function bitcoinTests(get: () => { wallet: core.HDWallet; info: core.HDWa await each( [ ["Bitcoin", 0, undefined], - ["Bitcoin", 1, core.BTCInputScriptType.SpendAddress], - ["Bitcoin", 3, core.BTCInputScriptType.SpendP2SHWitness], - ["Bitcoin", 2, core.BTCInputScriptType.SpendWitness], - ["Litecoin", 1, core.BTCInputScriptType.SpendAddress], - ["Litecoin", 1, core.BTCInputScriptType.SpendP2SHWitness], - ["Dash", 0, core.BTCInputScriptType.SpendAddress], - ["Dogecoin", 0, core.BTCInputScriptType.SpendAddress], - ["BitcoinCash", 0, core.BTCInputScriptType.SpendAddress], - ["BitcoinGold", 0, core.BTCInputScriptType.SpendAddress], + ["Bitcoin", 1, core.BTCScriptType.Legacy], + ["Bitcoin", 3, core.BTCScriptType.Segwit], + ["Bitcoin", 2, core.BTCScriptType.SegwitNative], + ["Litecoin", 1, core.BTCScriptType.Legacy], + ["Litecoin", 1, core.BTCScriptType.Segwit], + ["Dash", 0, core.BTCScriptType.Legacy], + ["Dogecoin", 0, core.BTCScriptType.Legacy], + ["BitcoinCash", 0, core.BTCScriptType.Legacy], + ["BitcoinGold", 0, core.BTCScriptType.Legacy], ], async (args) => { const coin = args[0] as core.Coin; const accountIdx = args[1] as number; - const scriptType = args[2] as core.BTCInputScriptType; + const scriptType = args[2] as core.BTCScriptType; if (!wallet) return; // Non-EVM things are a pain to test with snaps on test env, this wasn't tested before and still isn't if (metamask.isMetaMask(wallet)) return; diff --git a/integration/src/bitcoin/litecoin.ts b/integration/src/bitcoin/litecoin.ts index 19a80ece0..904e05e1f 100644 --- a/integration/src/bitcoin/litecoin.ts +++ b/integration/src/bitcoin/litecoin.ts @@ -38,25 +38,13 @@ export function litecoinTests(get: () => { wallet: core.HDWallet; info: core.HDW if (!(await wallet.btcSupportsCoin("Litecoin"))) return; await each( [ - [ - "Show", - "Litecoin", - "m/44'/2'/0'/0/0", - core.BTCInputScriptType.SpendAddress, - "LYXTv5RdsPYKC4qGmb6x6SuKoFMxUdSjLQ", - ], - [ - "Tell", - "Litecoin", - "m/49'/2'/0'/0/0", - core.BTCInputScriptType.SpendP2SHWitness, - "MFoQRU1KQq365Sy3cXhix3ygycEU4YWB1V", - ], + ["Show", "Litecoin", "m/44'/2'/0'/0/0", core.BTCScriptType.Legacy, "LYXTv5RdsPYKC4qGmb6x6SuKoFMxUdSjLQ"], + ["Tell", "Litecoin", "m/49'/2'/0'/0/0", core.BTCScriptType.Segwit, "MFoQRU1KQq365Sy3cXhix3ygycEU4YWB1V"], [ "Tell", "Litecoin", "m/84'/2'/0'/0/0", - core.BTCInputScriptType.SpendWitness, + core.BTCScriptType.SegwitNative, "ltc1qf6pwfkw4wd0fetq2pfrwzlfknskjg6nyvt6ngv", ], ], @@ -64,7 +52,7 @@ export function litecoinTests(get: () => { wallet: core.HDWallet; info: core.HDW const mode = args[0] as string; const coin = args[1] as core.Coin; const path = args[2] as string; - const scriptType = args[3] as core.BTCInputScriptType; + const scriptType = args[3] as core.BTCScriptType; const expected = args[4] as string; if (!(await wallet.btcSupportsScriptType(coin, scriptType))) return; @@ -86,14 +74,14 @@ export function litecoinTests(get: () => { wallet: core.HDWallet; info: core.HDW async () => { await each( [ - ["Litecoin", 1, core.BTCInputScriptType.SpendAddress], - ["Litecoin", 1, core.BTCInputScriptType.SpendP2SHWitness], - ["Litecoin", 1, core.BTCInputScriptType.SpendWitness], + ["Litecoin", 1, core.BTCScriptType.Legacy], + ["Litecoin", 1, core.BTCScriptType.Segwit], + ["Litecoin", 1, core.BTCScriptType.SegwitNative], ], async (args) => { const coin = args[0] as core.Coin; const accountIdx = args[1] as number; - const scriptType = args[2] as core.BTCInputScriptType; + const scriptType = args[2] as core.BTCScriptType; if (!wallet) return; if (!(await wallet.btcSupportsCoin(coin))) return; if (!(await wallet.btcSupportsScriptType(coin, scriptType))) return; diff --git a/integration/src/bitcoin/testnet.ts b/integration/src/bitcoin/testnet.ts index 4e21a2d9b..b317af354 100644 --- a/integration/src/bitcoin/testnet.ts +++ b/integration/src/bitcoin/testnet.ts @@ -40,7 +40,7 @@ export function testnetTests(get: () => { wallet: core.HDWallet; info: core.HDWa const inputs: core.BTCSignTxInputUnguarded[] = [ { addressNList: core.bip32ToAddressNList("m/49'/1'/0'/1/0"), - scriptType: core.BTCInputScriptType.SpendP2SHWitness, + scriptType: core.BTCScriptType.Segwit, amount: String(123456789), vout: 0, txid: "20912f98ea3ed849042efed0fdac8cb4fc301961c5988cba56902d8ffb61c337", @@ -56,7 +56,7 @@ export function testnetTests(get: () => { wallet: core.HDWallet; info: core.HDWa }, { addressNList: core.bip32ToAddressNList("m/49'/1'/0'/1/0"), - scriptType: core.BTCOutputScriptType.PayToP2SHWitness, + scriptType: core.BTCScriptType.Segwit, addressType: core.BTCOutputAddressType.Change, amount: String(123456789 - 11000 - 12300000), isChange: true, @@ -98,7 +98,7 @@ export function testnetTests(get: () => { wallet: core.HDWallet; info: core.HDWa const inputs: core.BTCSignTxInputUnguarded[] = [ { addressNList: core.bip32ToAddressNList("m/84'/1'/0'/0/0"), - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, amount: String(100000), vout: 0, txid: "e4b5b24159856ea18ab5819832da3b4a6330f9c3c0a46d96674e632df504b56b", @@ -115,7 +115,7 @@ export function testnetTests(get: () => { wallet: core.HDWallet; info: core.HDWa }, { addressNList: core.bip32ToAddressNList("m/84'/1'/0'/1/0"), - scriptType: core.BTCOutputScriptType.PayToWitness, + scriptType: core.BTCScriptType.SegwitNative, addressType: core.BTCOutputAddressType.Change, amount: String(100000 - 1000 - 50000), isChange: true, diff --git a/integration/src/wallets/keepkey.ts b/integration/src/wallets/keepkey.ts index 8dd8f1c2f..991e6eb61 100644 --- a/integration/src/wallets/keepkey.ts +++ b/integration/src/wallets/keepkey.ts @@ -149,6 +149,7 @@ export function selfTest(get: () => core.HDWallet): void { coin: "Bitcoin", addressNList: core.bip32ToAddressNList("m/44'/0'/0'/0/0"), showDisplay: true, + scriptType: core.BTCScriptType.Legacy, }) .then((address) => { addrs.push(address); @@ -159,6 +160,7 @@ export function selfTest(get: () => core.HDWallet): void { coin: "Bitcoin", addressNList: core.bip32ToAddressNList("m/44'/0'/0'/0/1"), showDisplay: true, + scriptType: core.BTCScriptType.Legacy, }) .then((address) => { addrs.push(address); @@ -169,6 +171,7 @@ export function selfTest(get: () => core.HDWallet): void { coin: "Bitcoin", addressNList: core.bip32ToAddressNList("m/44'/0'/0'/0/2"), showDisplay: true, + scriptType: core.BTCScriptType.Legacy, }) .then((address) => { addrs.push(address); @@ -199,6 +202,7 @@ export function selfTest(get: () => core.HDWallet): void { coin: "Bitcoin", addressNList: core.bip32ToAddressNList("m/44'/0'/0'/0/0"), showDisplay: true, + scriptType: core.BTCScriptType.Legacy, }); await wallet.cancel(); @@ -222,6 +226,7 @@ export function selfTest(get: () => core.HDWallet): void { coin: "Bitcoin", addressNList: core.bip32ToAddressNList("m/44'/0'/0'/0/0"), showDisplay: true, + scriptType: core.BTCScriptType.Legacy, }) ).resolves.toEqual("1JAd7XCBzGudGpJQSDSfpmJhiygtLQWaGL"); }, @@ -239,17 +244,17 @@ export function selfTest(get: () => core.HDWallet): void { expect(paths).toEqual([ { addressNList: [2147483692, 2147483650, 2147483651], - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, coin: "Litecoin", }, { addressNList: [2147483697, 2147483650, 2147483651], - scriptType: core.BTCInputScriptType.SpendP2SHWitness, + scriptType: core.BTCScriptType.Segwit, coin: "Litecoin", }, { addressNList: [2147483732, 2147483650, 2147483651], - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, coin: "Litecoin", }, ]); @@ -317,7 +322,7 @@ export function selfTest(get: () => core.HDWallet): void { coin: "Litecoin", isKnown: true, scriptType: "p2sh-p2wpkh", - verbose: "Litecoin Account #4", + verbose: "Litecoin Account #4 (Segwit)", wholeAccount: true, isPrefork: false, }, @@ -338,13 +343,13 @@ export function selfTest(get: () => core.HDWallet): void { wallet.info.describePath({ path: core.bip32ToAddressNList("m/44'/0'/0'/0/0"), coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toEqual({ verbose: "Bitcoin Account #0, Address #0 (Legacy)", coin: "Bitcoin", isKnown: true, - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, accountIdx: 0, addressIdx: 0, wholeAccount: false, @@ -358,13 +363,13 @@ export function selfTest(get: () => core.HDWallet): void { wallet.info.describePath({ path: core.bip32ToAddressNList("m/44'/0'/0'/0/0"), coin: "BitcoinCash", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toEqual({ verbose: "BitcoinCash Account #0, Address #0 (Prefork)", coin: "BitcoinCash", isKnown: true, - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, accountIdx: 0, addressIdx: 0, wholeAccount: false, @@ -378,13 +383,13 @@ export function selfTest(get: () => core.HDWallet): void { wallet.info.describePath({ path: core.bip32ToAddressNList("m/84'/0'/0'/0/0"), coin: "BitcoinGold", - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, }) ).toEqual({ verbose: "BitcoinGold Account #0, Address #0 (Prefork, Segwit Native)", coin: "BitcoinGold", isKnown: true, - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, accountIdx: 0, addressIdx: 0, wholeAccount: false, @@ -398,13 +403,13 @@ export function selfTest(get: () => core.HDWallet): void { wallet.info.describePath({ path: core.bip32ToAddressNList("m/44'/0'/7'/1/5"), coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toEqual({ verbose: "Bitcoin Account #7, Change Address #5 (Legacy)", coin: "Bitcoin", isKnown: true, - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, accountIdx: 7, addressIdx: 5, wholeAccount: false, @@ -418,7 +423,7 @@ export function selfTest(get: () => core.HDWallet): void { wallet.info.describePath({ path: core.bip32ToAddressNList("m/44'/0'/7'/1/5"), coin: "BitcoinCash", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toEqual({ accountIdx: 7, diff --git a/integration/src/wallets/ledger.ts b/integration/src/wallets/ledger.ts index fbe8bc689..d076da40a 100644 --- a/integration/src/wallets/ledger.ts +++ b/integration/src/wallets/ledger.ts @@ -545,17 +545,17 @@ export function selfTest(get: () => core.HDWallet): void { expect(paths).toEqual([ { addressNList: [2147483697, 2147483650, 2147483651], - scriptType: core.BTCInputScriptType.SpendP2SHWitness, + scriptType: core.BTCScriptType.Segwit, coin: "Litecoin", }, { addressNList: [2147483692, 2147483650, 2147483651], - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, coin: "Litecoin", }, { addressNList: [2147483732, 2147483650, 2147483651], - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, coin: "Litecoin", }, ]); @@ -586,7 +586,7 @@ export function selfTest(get: () => core.HDWallet): void { coin: "Litecoin", isKnown: true, scriptType: "p2sh-p2wpkh", - verbose: "Litecoin Account #4", + verbose: "Litecoin Account #4 (Segwit)", wholeAccount: true, isPrefork: false, }, @@ -616,12 +616,12 @@ export function selfTest(get: () => core.HDWallet): void { wallet.info.describePath({ path: core.bip32ToAddressNList("m/44'/0'/0'/0/0"), coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toEqual({ verbose: "Bitcoin Account #0, Address #0 (Legacy)", coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, isKnown: true, accountIdx: 0, addressIdx: 0, @@ -634,12 +634,12 @@ export function selfTest(get: () => core.HDWallet): void { wallet.info.describePath({ path: core.bip32ToAddressNList("m/44'/0'/7'/1/5"), coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toEqual({ verbose: "Bitcoin Account #7, Change Address #5 (Legacy)", coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, isKnown: true, accountIdx: 7, addressIdx: 5, @@ -652,13 +652,18 @@ export function selfTest(get: () => core.HDWallet): void { wallet.info.describePath({ path: core.bip32ToAddressNList("m/44'/0'/7'/1/5"), coin: "BitcoinCash", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toEqual({ - verbose: "m/44'/0'/7'/1/5", + verbose: "BitcoinCash Account #7, Change Address #5 (Prefork)", coin: "BitcoinCash", - scriptType: core.BTCInputScriptType.SpendAddress, - isKnown: false, + scriptType: core.BTCScriptType.Legacy, + isKnown: true, + accountIdx: 7, + addressIdx: 5, + wholeAccount: false, + isChange: true, + isPrefork: true, }); expect( diff --git a/integration/src/wallets/native.ts b/integration/src/wallets/native.ts index 00ed86fa5..336e80c58 100644 --- a/integration/src/wallets/native.ts +++ b/integration/src/wallets/native.ts @@ -142,17 +142,17 @@ export function selfTest(get: () => core.HDWallet): void { expect(paths).toEqual([ { addressNList: [2147483692, 2147483650, 2147483651], - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, coin: "Litecoin", }, { addressNList: [2147483697, 2147483650, 2147483651], - scriptType: core.BTCInputScriptType.SpendP2SHWitness, + scriptType: core.BTCScriptType.Segwit, coin: "Litecoin", }, { addressNList: [2147483732, 2147483650, 2147483651], - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, coin: "Litecoin", }, ]); @@ -225,7 +225,7 @@ export function selfTest(get: () => core.HDWallet): void { coin: "Litecoin", isKnown: true, scriptType: "p2sh-p2wpkh", - verbose: "Litecoin Account #4", + verbose: "Litecoin Account #4 (Segwit)", wholeAccount: true, isPrefork: false, }, @@ -234,7 +234,7 @@ export function selfTest(get: () => core.HDWallet): void { coin: "Litecoin", isKnown: true, scriptType: "p2wpkh", - verbose: "Litecoin Account #4 (Segwit)", + verbose: "Litecoin Account #4 (Segwit Native)", wholeAccount: true, isPrefork: false, }, @@ -250,13 +250,13 @@ export function selfTest(get: () => core.HDWallet): void { wallet.describePath({ path: core.bip32ToAddressNList("m/44'/0'/0'/0/0"), coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toEqual({ verbose: "Bitcoin Account #0, Address #0 (Legacy)", coin: "Bitcoin", isKnown: true, - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, accountIdx: 0, addressIdx: 0, wholeAccount: false, @@ -265,18 +265,18 @@ export function selfTest(get: () => core.HDWallet): void { }); }); - it("can describe a Bitcoin bech32 path", () => { + it("can describe a Bitcoin segwit native path", () => { expect( wallet.describePath({ path: core.bip32ToAddressNList("m/84'/0'/0'/0/0"), coin: "Bitcoin", - scriptType: core.BTCInputScriptType.Bech32, + scriptType: core.BTCScriptType.SegwitNative, }) ).toEqual({ verbose: "Bitcoin Account #0, Address #0 (Segwit Native)", coin: "Bitcoin", isKnown: true, - scriptType: core.BTCInputScriptType.Bech32, + scriptType: core.BTCScriptType.SegwitNative, accountIdx: 0, addressIdx: 0, wholeAccount: false, @@ -290,13 +290,13 @@ export function selfTest(get: () => core.HDWallet): void { wallet.describePath({ path: core.bip32ToAddressNList("m/44'/0'/7'/1/5"), coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toEqual({ verbose: "Bitcoin Account #7, Change Address #5 (Legacy)", coin: "Bitcoin", isKnown: true, - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, accountIdx: 7, addressIdx: 5, wholeAccount: false, @@ -311,13 +311,13 @@ export function selfTest(get: () => core.HDWallet): void { wallet.describePath({ path: core.bip32ToAddressNList("m/44'/0'/0'/0/0"), coin: "BitcoinCash", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toEqual({ verbose: "BitcoinCash Account #0, Address #0 (Prefork)", coin: "BitcoinCash", isKnown: true, - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, accountIdx: 0, addressIdx: 0, wholeAccount: false, @@ -332,13 +332,13 @@ export function selfTest(get: () => core.HDWallet): void { wallet.describePath({ path: core.bip32ToAddressNList("m/84'/0'/0'/0/0"), coin: "BitcoinGold", - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, }) ).toEqual({ verbose: "BitcoinGold Account #0, Address #0 (Prefork, Segwit Native)", coin: "BitcoinGold", isKnown: true, - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, accountIdx: 0, addressIdx: 0, wholeAccount: false, @@ -353,7 +353,7 @@ export function selfTest(get: () => core.HDWallet): void { wallet.describePath({ path: core.bip32ToAddressNList("m/44'/0'/7'/1/5"), coin: "BitcoinCash", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toEqual({ accountIdx: 7, diff --git a/integration/src/wallets/phantom.ts b/integration/src/wallets/phantom.ts index 1004b0141..6d8907ff5 100644 --- a/integration/src/wallets/phantom.ts +++ b/integration/src/wallets/phantom.ts @@ -190,7 +190,7 @@ export function selfTest(get: () => core.HDWallet): void { expect(paths).toEqual([ { addressNList: [2147483732, 2147483648, 2147483651], - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, coin: "Bitcoin", }, ]); @@ -201,13 +201,13 @@ export function selfTest(get: () => core.HDWallet): void { wallet.describePath({ path: core.bip32ToAddressNList("m/44'/0'/0'/0/0"), coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toEqual({ verbose: "Bitcoin Account #0, Address #0 (Legacy)", coin: "Bitcoin", isKnown: true, - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, accountIdx: 0, addressIdx: 0, wholeAccount: false, @@ -216,18 +216,18 @@ export function selfTest(get: () => core.HDWallet): void { }); }); - it("can describe a Bitcoin bech32 path", () => { + it("can describe a Bitcoin Segwit Native path", () => { expect( wallet.describePath({ path: core.bip32ToAddressNList("m/84'/0'/0'/0/0"), coin: "Bitcoin", - scriptType: core.BTCInputScriptType.Bech32, + scriptType: core.BTCScriptType.SegwitNative, }) ).toEqual({ verbose: "Bitcoin Account #0, Address #0 (Segwit Native)", coin: "Bitcoin", isKnown: true, - scriptType: core.BTCInputScriptType.Bech32, + scriptType: core.BTCScriptType.SegwitNative, accountIdx: 0, addressIdx: 0, wholeAccount: false, @@ -241,13 +241,13 @@ export function selfTest(get: () => core.HDWallet): void { wallet.describePath({ path: core.bip32ToAddressNList("m/44'/0'/7'/1/5"), coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toEqual({ verbose: "Bitcoin Account #7, Change Address #5 (Legacy)", coin: "Bitcoin", isKnown: true, - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, accountIdx: 7, addressIdx: 5, wholeAccount: false, diff --git a/integration/src/wallets/trezor.ts b/integration/src/wallets/trezor.ts index d641b04f1..3153f2d5c 100644 --- a/integration/src/wallets/trezor.ts +++ b/integration/src/wallets/trezor.ts @@ -439,17 +439,17 @@ export function selfTest(get: () => core.HDWallet): void { expect(paths).toEqual([ { addressNList: [2147483697, 2147483650, 2147483651], - scriptType: core.BTCInputScriptType.SpendP2SHWitness, + scriptType: core.BTCScriptType.Segwit, coin: "Litecoin", }, { addressNList: [2147483692, 2147483650, 2147483651], - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, coin: "Litecoin", }, { addressNList: [2147483732, 2147483650, 2147483651], - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, coin: "Litecoin", }, ]); @@ -480,7 +480,7 @@ export function selfTest(get: () => core.HDWallet): void { coin: "Litecoin", isKnown: true, scriptType: "p2sh-p2wpkh", - verbose: "Litecoin Account #4", + verbose: "Litecoin Account #4 (Segwit)", wholeAccount: true, isPrefork: false, }, @@ -510,12 +510,12 @@ export function selfTest(get: () => core.HDWallet): void { wallet.info.describePath({ path: core.bip32ToAddressNList("m/44'/0'/0'/0/0"), coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toEqual({ verbose: "Bitcoin Account #0, Address #0 (Legacy)", coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, isKnown: true, accountIdx: 0, addressIdx: 0, @@ -528,12 +528,12 @@ export function selfTest(get: () => core.HDWallet): void { wallet.info.describePath({ path: core.bip32ToAddressNList("m/44'/0'/7'/1/5"), coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toEqual({ verbose: "Bitcoin Account #7, Change Address #5 (Legacy)", coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, isKnown: true, accountIdx: 7, addressIdx: 5, @@ -546,13 +546,18 @@ export function selfTest(get: () => core.HDWallet): void { wallet.info.describePath({ path: core.bip32ToAddressNList("m/44'/0'/7'/1/5"), coin: "BitcoinCash", - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toEqual({ - verbose: "m/44'/0'/7'/1/5", + verbose: "BitcoinCash Account #7, Change Address #5 (Prefork)", coin: "BitcoinCash", - scriptType: core.BTCInputScriptType.SpendAddress, - isKnown: false, + scriptType: core.BTCScriptType.Legacy, + isKnown: true, + accountIdx: 7, + addressIdx: 5, + wholeAccount: false, + isChange: true, + isPrefork: true, }); expect( diff --git a/integration/src/wallets/vultisig.ts b/integration/src/wallets/vultisig.ts index b59f16ae9..c353445f6 100644 --- a/integration/src/wallets/vultisig.ts +++ b/integration/src/wallets/vultisig.ts @@ -213,7 +213,7 @@ export function selfTest(get: () => core.HDWallet): void { it("supports correct script types", async () => { if (!wallet) return; - expect(await wallet.btcSupportsScriptType("Bitcoin", core.BTCInputScriptType.SpendWitness)).toEqual(true); + expect(await wallet.btcSupportsScriptType("Bitcoin", core.BTCScriptType.SegwitNative)).toEqual(true); }); it("uses correct paths for Bitcoin (BIP84)", () => { @@ -225,7 +225,7 @@ export function selfTest(get: () => core.HDWallet): void { expect(paths).toEqual([ { addressNList: [2147483732, 2147483648, 2147483651], // m/84'/0'/3' - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, coin: "Bitcoin", }, ]); @@ -237,13 +237,13 @@ export function selfTest(get: () => core.HDWallet): void { wallet.describePath({ path: core.bip32ToAddressNList("m/84'/0'/0'/0/0"), coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, }) ).toEqual({ - verbose: "Bitcoin Account #0, Address #0 (Segwit)", + verbose: "Bitcoin Account #0, Address #0 (Segwit Native)", coin: "Bitcoin", isKnown: true, - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, accountIdx: 0, addressIdx: 0, wholeAccount: false, diff --git a/packages/hdwallet-core/src/bitcoin.ts b/packages/hdwallet-core/src/bitcoin.ts index 10840593f..709001010 100644 --- a/packages/hdwallet-core/src/bitcoin.ts +++ b/packages/hdwallet-core/src/bitcoin.ts @@ -1,7 +1,6 @@ import * as bitcoin from "@shapeshiftoss/bitcoinjs-lib"; import * as ta from "type-assertions"; -import { BTCScriptType } from "./networks"; import { addressNListToBIP32, slip44ByCoin } from "./utils"; import { BIP32Path, Coin, HDWallet, HDWalletInfo, PathDescription } from "./wallet"; @@ -20,7 +19,7 @@ type GuardedUnion = GuardedUnionInner; export type BTCGetAddress = { coin: Coin; addressNList: BIP32Path; - scriptType?: BTCInputScriptType; // Defaults to BTCInputScriptType.SpendAddress + scriptType: BTCScriptType; showDisplay?: boolean; }; @@ -78,7 +77,7 @@ type BTCSignTxInputNativeBase = BTCSignTxInputBase & { }; type BTCSignTxInputNativeSegwitBase = BTCSignTxInputNativeBase & { - scriptType: BTCInputScriptType.SpendWitness | BTCInputScriptType.SpendP2SHWitness; + scriptType: BTCScriptType.SegwitNative | BTCScriptType.Segwit; }; type BTCSignTxInputNativeSegwitWithHex = BTCSignTxInputNativeSegwitBase & { @@ -94,7 +93,7 @@ type BTCSignTxInputNativeSegwitWithTx = BTCSignTxInputNativeSegwitBase & { type BTCSignTxInputNativeSegwit = BTCSignTxInputNativeSegwitWithHex | BTCSignTxInputNativeSegwitWithTx; type BTCSignTxInputNativeNonSegwit = BTCSignTxInputNativeBase & { - scriptType: Exclude; + scriptType: Exclude; hex: string; }; @@ -108,12 +107,12 @@ type BTCSignTxInputKKBase = BTCSignTxInputBase & { }; type BTCSignTxInputKKSegwit = BTCSignTxInputKKBase & { - scriptType: BTCInputScriptType.SpendWitness | BTCInputScriptType.SpendP2SHWitness | BTCInputScriptType.External; + scriptType: BTCScriptType.SegwitNative | BTCScriptType.Segwit; hex?: string; }; type BTCSignTxInputKKNonSegwit = BTCSignTxInputKKBase & { - scriptType: Exclude; + scriptType: Exclude; } & ( | { tx: BitcoinTx; @@ -129,12 +128,12 @@ export type BTCSignTxInputKK = GuardedUnion; export type BTCSignTxInputTrezor = BTCSignTxInputBase & { txid: string; amount: string; - scriptType: BTCInputScriptType; + scriptType: BTCScriptType; }; export type BTCSignTxInputLedger = BTCSignTxInputBase & { addressNList: BIP32Path; - scriptType: BTCInputScriptType; + scriptType: BTCScriptType; hex: string; }; @@ -147,7 +146,7 @@ export type BTCSignTxInputUnguarded = BTCSignTxInputNativeUnguarded & // Stick to this common subset of input fields to avoid type hell. export type BTCSignTxInputSafe = { addressNList: BIP32Path; - scriptType: BTCInputScriptType; + scriptType: BTCScriptType; hex: string; txid: string; amount: string; @@ -169,21 +168,21 @@ export type BTCSignTxOutputSpendP2PKH = { addressType?: BTCOutputAddressType.Spend; amount: string; address: string; - scriptType: BTCOutputScriptType.PayToAddress; + scriptType: BTCScriptType.Legacy; }; export type BTCSignTxOutputSpendP2SH = { addressType?: BTCOutputAddressType.Spend; amount: string; address: string; - scriptType: BTCOutputScriptType.PayToMultisig | BTCOutputScriptType.PayToP2SHWitness; + scriptType: BTCScriptType.LegacyMultisig | BTCScriptType.Segwit; }; export type BTCSignTxOutputSpendP2WPKH = { addressType?: BTCOutputAddressType.Spend; amount: string; address: string; - scriptType: BTCOutputScriptType.PayToWitness; + scriptType: BTCScriptType.SegwitNative; }; export type BTCSignTxOutputTransfer = { @@ -191,7 +190,7 @@ export type BTCSignTxOutputTransfer = { amount: string; /** bip32 path for destination (device must `btcSupportsSecureTransfer()`) */ addressNList: BIP32Path; - scriptType: BTCOutputScriptType; + scriptType: BTCScriptType; }; export type BTCSignTxOutputChange = { @@ -199,7 +198,7 @@ export type BTCSignTxOutputChange = { amount: string; /** bip32 path for destination (device must `btcSupportsSecureTransfer()`) */ addressNList: BIP32Path; - scriptType: BTCOutputScriptType; + scriptType: BTCScriptType; isChange: true; }; @@ -241,23 +240,11 @@ export interface BTCSignedTx { serializedTx: string; } -// Bech32 info https://en.bitcoin.it/wiki/BIP_0173 -export enum BTCInputScriptType { - CashAddr = "cashaddr", // for Bitcoin Cash - Bech32 = "bech32", - SpendAddress = "p2pkh", - SpendMultisig = "p2sh", - External = "external", - SpendWitness = "p2wpkh", - SpendP2SHWitness = "p2sh-p2wpkh", -} - -export enum BTCOutputScriptType { - PayToAddress = "p2pkh", - PayToMultisig = "p2sh", - Bech32 = "bech32", - PayToWitness = "p2wpkh", - PayToP2SHWitness = "p2sh-p2wpkh", +export enum BTCScriptType { + Legacy = "p2pkh", + LegacyMultisig = "p2sh", + Segwit = "p2sh-p2wpkh", + SegwitNative = "p2wpkh", } export enum BTCOutputAddressType { @@ -269,7 +256,7 @@ export enum BTCOutputAddressType { export interface BTCSignMessage { addressNList: BIP32Path; coin: Coin; - scriptType?: BTCInputScriptType; + scriptType?: BTCScriptType; message: string; } @@ -288,12 +275,12 @@ export interface BTCVerifyMessage { export interface BTCGetAccountPaths { coin: Coin; accountIdx: number; - scriptType?: BTCInputScriptType; + scriptType?: BTCScriptType; } export interface BTCAccountPath { coin: Coin; - scriptType: BTCInputScriptType; + scriptType: BTCScriptType; addressNList: BIP32Path; } @@ -309,7 +296,7 @@ export interface BTCWalletInfo extends HDWalletInfo { * Does the device support the given script type for the given coin? * Assumes that `btcSupportsCoin(coin)` for the given coin. */ - btcSupportsScriptType(coin: Coin, scriptType?: BTCInputScriptType): Promise; + btcSupportsScriptType(coin: Coin, scriptType?: BTCScriptType): Promise; /** * Does the device support internal transfers without the user needing to @@ -357,7 +344,7 @@ export interface BTCWallet extends BTCWalletInfo, HDWallet { btcVerifyMessage(msg: BTCVerifyMessage): Promise; } -export function unknownUTXOPath(path: BIP32Path, coin: Coin, scriptType?: BTCInputScriptType): PathDescription { +export function unknownUTXOPath(path: BIP32Path, coin: Coin, scriptType?: BTCScriptType): PathDescription { return { verbose: addressNListToBIP32(path), coin, @@ -366,30 +353,27 @@ export function unknownUTXOPath(path: BIP32Path, coin: Coin, scriptType?: BTCInp }; } -export function describeUTXOPath(path: BIP32Path, coin: Coin, scriptType: BTCInputScriptType): PathDescription { +export function describeUTXOPath(path: BIP32Path, coin: Coin, scriptType: BTCScriptType): PathDescription { const unknown = unknownUTXOPath(path, coin, scriptType); if (path.length !== 3 && path.length !== 5) return unknown; - if ((path[0] & 0x80000000) >>> 0 !== 0x80000000) return unknown; const purpose = path[0] & 0x7fffffff; - if (![44, 49, 84].includes(purpose)) return unknown; - if (purpose === 44 && scriptType !== BTCInputScriptType.SpendAddress) return unknown; - - if (purpose === 49 && scriptType !== BTCInputScriptType.SpendP2SHWitness) return unknown; + if (purpose === 44 && scriptType !== BTCScriptType.Legacy) return unknown; + if (purpose === 49 && scriptType !== BTCScriptType.Segwit) return unknown; + if (purpose === 84 && scriptType !== BTCScriptType.SegwitNative) return unknown; const wholeAccount = path.length === 3; const script = ( { - [BTCInputScriptType.SpendAddress]: ["Legacy"], - [BTCInputScriptType.SpendP2SHWitness]: [], - [BTCInputScriptType.SpendWitness]: ["Segwit"], - [BTCInputScriptType.Bech32]: ["Segwit Native"], - } as Partial> + [BTCScriptType.Legacy]: ["Legacy"], + [BTCScriptType.Segwit]: ["Segwit"], + [BTCScriptType.SegwitNative]: ["Segwit Native"], + } as Partial> )[scriptType]; let isPrefork = false; @@ -464,7 +448,7 @@ export function describeUTXOPath(path: BIP32Path, coin: Coin, scriptType: BTCInp export function legacyAccount(coin: Coin, slip44: number, accountIdx: number): BTCAccountPath { return { coin, - scriptType: BTCInputScriptType.SpendAddress, + scriptType: BTCScriptType.Legacy, addressNList: [0x80000000 + 44, 0x80000000 + slip44, 0x80000000 + accountIdx], }; } @@ -472,7 +456,7 @@ export function legacyAccount(coin: Coin, slip44: number, accountIdx: number): B export function segwitAccount(coin: Coin, slip44: number, accountIdx: number): BTCAccountPath { return { coin, - scriptType: BTCInputScriptType.SpendP2SHWitness, + scriptType: BTCScriptType.Segwit, addressNList: [0x80000000 + 49, 0x80000000 + slip44, 0x80000000 + accountIdx], }; } @@ -480,7 +464,7 @@ export function segwitAccount(coin: Coin, slip44: number, accountIdx: number): B export function segwitNativeAccount(coin: Coin, slip44: number, accountIdx: number): BTCAccountPath { return { coin, - scriptType: BTCInputScriptType.SpendWitness, + scriptType: BTCScriptType.SegwitNative, addressNList: [0x80000000 + 84, 0x80000000 + slip44, 0x80000000 + accountIdx], }; } @@ -537,7 +521,6 @@ export function createPayment(pubkey: Buffer, network: bitcoin.Network, scriptTy case "p2pkh": return bitcoin.payments.p2pkh({ pubkey, network }); case "p2wpkh": - case "bech32": return bitcoin.payments.p2wpkh({ pubkey, network }); case "p2sh-p2wpkh": return bitcoin.payments.p2sh({ diff --git a/packages/hdwallet-core/src/networks.ts b/packages/hdwallet-core/src/networks.ts index 5d90cebff..ceed1c0ff 100644 --- a/packages/hdwallet-core/src/networks.ts +++ b/packages/hdwallet-core/src/networks.ts @@ -1,160 +1,123 @@ import * as bitcoin from "@shapeshiftoss/bitcoinjs-lib"; -import { BTCInputScriptType, BTCOutputScriptType } from "./bitcoin"; - -export type BTCScriptType = BTCInputScriptType | BTCOutputScriptType; +import { BTCScriptType } from "./bitcoin"; type BIP32 = { - bip32: { - public: number; - private: number; - }; + public: number; + private: number; }; -type BIP32ByScriptType = Partial>; +type BIP32ByScriptType = Partial>; -const bip32BTC: BIP32ByScriptType = { - p2sh: { - bip32: { - public: 0x0488b21e, - private: 0x0488ade4, - }, +const bip32: BIP32ByScriptType = { + [BTCScriptType.Legacy]: { + public: 0x0488b21e, + private: 0x0488ade4, }, - p2pkh: { - bip32: { - public: 0x0488b21e, - private: 0x0488ade4, - }, + [BTCScriptType.LegacyMultisig]: { + public: 0x0488b21e, + private: 0x0488ade4, }, - "p2sh-p2wpkh": { - bip32: { - public: 0x049d7cb2, - private: 0x049d7878, - }, + [BTCScriptType.Segwit]: { + public: 0x049d7cb2, + private: 0x049d7878, }, - p2wpkh: { - bip32: { - public: 0x04b24746, - private: 0x04b2430c, - }, + [BTCScriptType.SegwitNative]: { + public: 0x04b24746, + private: 0x04b2430c, }, }; -type NetworkDescription = { - base: Omit; -} & BIP32ByScriptType; - -type Networks = Record; +type Network = Omit & { bip32: BIP32ByScriptType }; +type Networks = Record; const networks: Networks = { bitcoin: { - base: { - messagePrefix: "\x18Bitcoin Signed Message:\n", - bech32: "bc", - pubKeyHash: 0x00, - scriptHash: 0x05, - wif: 0x80, - }, - ...bip32BTC, + messagePrefix: "\x18Bitcoin Signed Message:\n", + bech32: "bc", + pubKeyHash: 0x00, + scriptHash: 0x05, + wif: 0x80, + bip32, }, dash: { - base: { - messagePrefix: "unused", - bech32: "", - pubKeyHash: 0x4c, - scriptHash: 0x10, - wif: 0xcc, + messagePrefix: "unused", + bech32: "", + pubKeyHash: 0x4c, + scriptHash: 0x10, + wif: 0xcc, + bip32: { + [BTCScriptType.Legacy]: bip32[BTCScriptType.Legacy], + [BTCScriptType.LegacyMultisig]: bip32[BTCScriptType.LegacyMultisig], }, - p2sh: bip32BTC.p2sh, - p2pkh: bip32BTC.p2pkh, }, digibyte: { - base: { - messagePrefix: "\x19Digibyte Signed Message:\n", - bech32: "dgb", - pubKeyHash: 0x1e, - scriptHash: 0x3f, - wif: 0x80, - }, - ...bip32BTC, + messagePrefix: "\x19Digibyte Signed Message:\n", + bech32: "dgb", + pubKeyHash: 0x1e, + scriptHash: 0x3f, + wif: 0x80, + bip32, }, dogecoin: { - base: { - messagePrefix: "\x19Dogecoin Signed Message:\n", - bech32: "", - pubKeyHash: 0x1e, - scriptHash: 0x16, - wif: 0x9e, - }, - p2sh: { - bip32: { + messagePrefix: "\x19Dogecoin Signed Message:\n", + bech32: "", + pubKeyHash: 0x1e, + scriptHash: 0x16, + wif: 0x9e, + bip32: { + [BTCScriptType.Legacy]: { public: 0x02facafd, private: 0x02fac398, }, - }, - p2pkh: { - bip32: { + [BTCScriptType.LegacyMultisig]: { public: 0x02facafd, private: 0x02fac398, }, }, }, litecoin: { - base: { - messagePrefix: "\x19Litecoin Signed Message:\n", - bech32: "ltc", - pubKeyHash: 0x30, - scriptHash: 0x32, - wif: 0xb0, - }, - p2sh: { - bip32: { + messagePrefix: "\x19Litecoin Signed Message:\n", + bech32: "ltc", + pubKeyHash: 0x30, + scriptHash: 0x32, + wif: 0xb0, + bip32: { + [BTCScriptType.LegacyMultisig]: { public: 0x019da462, private: 0x019d9cfe, }, - }, - p2pkh: { - bip32: { + [BTCScriptType.Legacy]: { public: 0x019da462, private: 0x019d9cfe, }, - }, - "p2sh-p2wpkh": { - bip32: { + [BTCScriptType.Segwit]: { public: 0x01b26ef6, private: 0x01b26792, }, + [BTCScriptType.SegwitNative]: bip32[BTCScriptType.SegwitNative], }, - p2wpkh: bip32BTC.p2wpkh, }, testnet: { - base: { - messagePrefix: "\x18Bitcoin Signed Message:\n", - bech32: "tb", - pubKeyHash: 0x6f, - scriptHash: 0xc4, - wif: 0xef, - }, - p2sh: { - bip32: { + messagePrefix: "\x18Bitcoin Signed Message:\n", + bech32: "tb", + pubKeyHash: 0x6f, + scriptHash: 0xc4, + wif: 0xef, + bip32: { + [BTCScriptType.LegacyMultisig]: { public: 0x043587cf, private: 0x04358394, }, - }, - p2pkh: { - bip32: { + [BTCScriptType.Legacy]: { public: 0x043587cf, private: 0x04358394, }, - }, - "p2sh-p2wpkh": { - bip32: { + [BTCScriptType.Segwit]: { public: 0x044a5262, private: 0x044a4e28, }, - }, - p2wpkh: { - bip32: { + [BTCScriptType.SegwitNative]: { public: 0x045f1cf6, private: 0x045f18bc, }, @@ -179,17 +142,14 @@ for (const coin of [ ]) networks[coin] = networks.bitcoin; -export function getNetwork(coin: string, scriptType: BTCScriptType): bitcoin.Network { +export function getNetwork(coin: string, scriptType = BTCScriptType.Legacy): bitcoin.Network { coin = coin.toLowerCase(); if (!(coin in networks)) throw new Error(`${coin} network not supported`); const network = networks[coin]; - const bip32 = network[scriptType as BTCOutputScriptType]; - if (!bip32) throw new Error(`${scriptType} not supported for ${coin} network`); + const _bip32 = network.bip32[scriptType]; + if (!_bip32) throw new Error(`${scriptType} not supported for ${coin} network`); - return { - ...network.base, - ...bip32, - }; + return { ...network, bip32: _bip32 }; } diff --git a/packages/hdwallet-core/src/utxoUtils.ts b/packages/hdwallet-core/src/utxoUtils.ts index b8508a4d1..69f9df5cf 100644 --- a/packages/hdwallet-core/src/utxoUtils.ts +++ b/packages/hdwallet-core/src/utxoUtils.ts @@ -1,6 +1,6 @@ import { decode, encode } from "bs58check"; -import { BTCInputScriptType } from "./bitcoin"; +import { BTCScriptType } from "./bitcoin"; enum UtxoAccountType { SegwitNative = "SegwitNative", @@ -8,14 +8,11 @@ enum UtxoAccountType { P2pkh = "P2pkh", } -export const scriptTypeToAccountType: Record = Object.freeze({ - [BTCInputScriptType.SpendAddress]: UtxoAccountType.P2pkh, - [BTCInputScriptType.SpendP2SHWitness]: UtxoAccountType.SegwitP2sh, - [BTCInputScriptType.SpendWitness]: UtxoAccountType.SegwitNative, - [BTCInputScriptType.SpendMultisig]: undefined, - [BTCInputScriptType.Bech32]: undefined, - [BTCInputScriptType.CashAddr]: undefined, - [BTCInputScriptType.External]: undefined, +export const scriptTypeToAccountType: Record = Object.freeze({ + [BTCScriptType.Legacy]: UtxoAccountType.P2pkh, + [BTCScriptType.Segwit]: UtxoAccountType.SegwitP2sh, + [BTCScriptType.SegwitNative]: UtxoAccountType.SegwitNative, + [BTCScriptType.LegacyMultisig]: undefined, }); /** diff --git a/packages/hdwallet-core/src/wallet.ts b/packages/hdwallet-core/src/wallet.ts index c66d4bc60..edceee98e 100644 --- a/packages/hdwallet-core/src/wallet.ts +++ b/packages/hdwallet-core/src/wallet.ts @@ -2,7 +2,7 @@ import isObject from "lodash/isObject"; import { ArkeoWallet, ArkeoWalletInfo } from "./arkeo"; import { BinanceWallet, BinanceWalletInfo } from "./binance"; -import { BTCInputScriptType, BTCWallet, BTCWalletInfo } from "./bitcoin"; +import { BTCScriptType, BTCWallet, BTCWalletInfo } from "./bitcoin"; import { CosmosWallet, CosmosWalletInfo } from "./cosmos"; import { DebugLinkWallet } from "./debuglink"; import { EosWallet, EosWalletInfo } from "./eos"; @@ -22,7 +22,7 @@ export type BIP32Path = Array; export interface GetPublicKey { addressNList: BIP32Path; showDisplay?: boolean; - scriptType?: BTCInputScriptType; // Defaults to BTCInputScriptType.SpendAddress + scriptType: BTCScriptType; curve: "secp256k1" | "ed25519"; coin: Coin; } @@ -79,14 +79,14 @@ export interface LoadDevice { export interface DescribePath { path: BIP32Path; coin: Coin; - scriptType?: BTCInputScriptType; + scriptType?: BTCScriptType; } export interface PathDescription { isKnown: boolean; verbose: string; coin: Coin; - scriptType?: BTCInputScriptType; + scriptType?: BTCScriptType; accountIdx?: number; addressIdx?: number; isChange?: boolean; diff --git a/packages/hdwallet-gridplus/src/bitcoin.ts b/packages/hdwallet-gridplus/src/bitcoin.ts index a240249e2..a6a1b4cbb 100644 --- a/packages/hdwallet-gridplus/src/bitcoin.ts +++ b/packages/hdwallet-gridplus/src/bitcoin.ts @@ -8,7 +8,7 @@ import { getCompressedPubkey } from "./utils"; export function deriveAddressFromPubkey( pubkey: Buffer, coin: string, - scriptType: core.BTCScriptType = core.BTCInputScriptType.SpendAddress + scriptType: core.BTCScriptType ): string | undefined { const network = core.getNetwork(coin, scriptType); return core.createPayment(pubkey, network, scriptType).address; @@ -49,7 +49,7 @@ export async function btcSignTx(client: Client, msg: core.BTCSignTx): Promise s.toString("hex")), serializedTx: tx }; } else { const psbt = new bitcoin.Psbt({ - network: core.getNetwork(msg.coin, core.BTCOutputScriptType.PayToMultisig), + network: core.getNetwork(msg.coin), forkCoin: msg.coin.toLowerCase() === "bitcoincash" ? "bch" : "none", }); diff --git a/packages/hdwallet-gridplus/src/constants.ts b/packages/hdwallet-gridplus/src/constants.ts deleted file mode 100644 index d1da89759..000000000 --- a/packages/hdwallet-gridplus/src/constants.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { UtxoAccountType } from "@shapeshiftoss/types"; -export { UtxoAccountType }; - -export enum PublicKeyType { - xpub = "0488b21e", - ypub = "049d7cb2", - zpub = "04b24746", - dgub = "02facafd", - Ltub = "019da462", - Mtub = "01b26ef6", -} - -export const accountTypeToVersion = (() => { - const Litecoin = { - [UtxoAccountType.P2pkh]: Buffer.from(PublicKeyType.Ltub, "hex"), - [UtxoAccountType.SegwitP2sh]: Buffer.from(PublicKeyType.Mtub, "hex"), - [UtxoAccountType.SegwitNative]: Buffer.from(PublicKeyType.zpub, "hex"), - }; - - const Dogecoin = { - [UtxoAccountType.P2pkh]: Buffer.from(PublicKeyType.dgub, "hex"), - }; - - const Bitcoin = { - [UtxoAccountType.P2pkh]: Buffer.from(PublicKeyType.xpub, "hex"), - [UtxoAccountType.SegwitP2sh]: Buffer.from(PublicKeyType.ypub, "hex"), - [UtxoAccountType.SegwitNative]: Buffer.from(PublicKeyType.zpub, "hex"), - }; - - return (coin: string, type: UtxoAccountType) => { - switch (coin) { - case "Litecoin": - return Litecoin[type]; - case "Bitcoin": - return Bitcoin[type]; - case "Dogecoin": - if (type !== UtxoAccountType.P2pkh) throw new Error("Unsupported account type"); - return Dogecoin[type]; - default: - return Bitcoin[type]; - } - }; -})(); - -export const convertVersions = ["Ltub", "xpub", "dgub"]; - -export const UTXO_NETWORK_PARAMS: Record = { - Bitcoin: { pubKeyHash: 0x00, scriptHash: 0x05, bech32: "bc" }, - Dogecoin: { pubKeyHash: 0x1e, scriptHash: 0x16 }, - Litecoin: { pubKeyHash: 0x30, scriptHash: 0x32, bech32: "ltc" }, - BitcoinCash: { pubKeyHash: 0x00, scriptHash: 0x05 }, -}; diff --git a/packages/hdwallet-gridplus/src/gridplus.ts b/packages/hdwallet-gridplus/src/gridplus.ts index cb88abdd6..4aa8b2592 100644 --- a/packages/hdwallet-gridplus/src/gridplus.ts +++ b/packages/hdwallet-gridplus/src/gridplus.ts @@ -84,13 +84,13 @@ export class GridPlusWalletInfo return supportedCoins.includes(coin); } - async btcSupportsScriptType(coin: core.Coin, scriptType: core.BTCInputScriptType): Promise { + async btcSupportsScriptType(coin: core.Coin, scriptType: core.BTCScriptType): Promise { switch (scriptType) { - case core.BTCInputScriptType.SpendAddress: + case core.BTCScriptType.Legacy: return ["Bitcoin", "BitcoinCash", "Litecoin", "Dogecoin"].includes(coin); - case core.BTCInputScriptType.SpendP2SHWitness: + case core.BTCScriptType.Segwit: return ["Bitcoin", "BitcoinCash", "Litecoin"].includes(coin); - case core.BTCInputScriptType.SpendWitness: + case core.BTCScriptType.SegwitNative: return ["Bitcoin", "BitcoinCash", "Litecoin"].includes(coin); default: return false; @@ -137,9 +137,9 @@ export class GridPlusWalletInfo const addressNList = msg.addressNList; if ( - (addressNList[0] === 0x80000000 + 44 && msg.scriptType == core.BTCInputScriptType.SpendAddress) || - (addressNList[0] === 0x80000000 + 49 && msg.scriptType == core.BTCInputScriptType.SpendP2SHWitness) || - (addressNList[0] === 0x80000000 + 84 && msg.scriptType == core.BTCInputScriptType.SpendWitness) + (addressNList[0] === 0x80000000 + 44 && msg.scriptType == core.BTCScriptType.Legacy) || + (addressNList[0] === 0x80000000 + 49 && msg.scriptType == core.BTCScriptType.Segwit) || + (addressNList[0] === 0x80000000 + 84 && msg.scriptType == core.BTCScriptType.SegwitNative) ) { addressNList[2] += 1; return { ...msg, addressNList }; diff --git a/packages/hdwallet-keepkey/src/bitcoin.ts b/packages/hdwallet-keepkey/src/bitcoin.ts index 147347b64..39264d553 100644 --- a/packages/hdwallet-keepkey/src/bitcoin.ts +++ b/packages/hdwallet-keepkey/src/bitcoin.ts @@ -18,7 +18,7 @@ const segwitCoins = ["Bitcoin", "Testnet", "BitcoinGold", "Litecoin"]; function legacyAccount(coin: core.Coin, slip44: number, accountIdx: number): core.BTCAccountPath { return { coin, - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, addressNList: [0x80000000 + 44, 0x80000000 + slip44, 0x80000000 + accountIdx], }; } @@ -26,7 +26,7 @@ function legacyAccount(coin: core.Coin, slip44: number, accountIdx: number): cor function segwitAccount(coin: core.Coin, slip44: number, accountIdx: number): core.BTCAccountPath { return { coin, - scriptType: core.BTCInputScriptType.SpendP2SHWitness, + scriptType: core.BTCScriptType.Segwit, addressNList: [0x80000000 + 49, 0x80000000 + slip44, 0x80000000 + accountIdx], }; } @@ -34,7 +34,7 @@ function segwitAccount(coin: core.Coin, slip44: number, accountIdx: number): cor function segwitNativeAccount(coin: core.Coin, slip44: number, accountIdx: number): core.BTCAccountPath { return { coin, - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, addressNList: [0x80000000 + 84, 0x80000000 + slip44, 0x80000000 + accountIdx], }; } @@ -105,11 +105,7 @@ function prepareSignTx( inputs.forEach((inputTx) => { if (inputTx.txid in txmap) return; - if ( - inputTx.scriptType === core.BTCInputScriptType.SpendP2SHWitness || - inputTx.scriptType === core.BTCInputScriptType.SpendWitness || - inputTx.scriptType === core.BTCInputScriptType.External - ) + if (inputTx.scriptType === core.BTCScriptType.Segwit || inputTx.scriptType === core.BTCScriptType.SegwitNative) return; const prevTx = ((): core.BitcoinTx => { @@ -232,10 +228,10 @@ export async function btcSupportsCoin(coin: core.Coin): Promise { return supportedCoins.includes(coin); } -export async function btcSupportsScriptType(coin: core.Coin, scriptType?: core.BTCInputScriptType): Promise { +export async function btcSupportsScriptType(coin: core.Coin, scriptType?: core.BTCScriptType): Promise { if (!supportedCoins.includes(coin)) return false; - if (!segwitCoins.includes(coin) && scriptType === core.BTCInputScriptType.SpendP2SHWitness) return false; - if (!segwitCoins.includes(coin) && scriptType === core.BTCInputScriptType.SpendWitness) return false; + if (!segwitCoins.includes(coin) && scriptType === core.BTCScriptType.Segwit) return false; + if (!segwitCoins.includes(coin) && scriptType === core.BTCScriptType.SegwitNative) return false; return true; } @@ -250,7 +246,7 @@ export async function btcGetAddress( addr.setAddressNList(msg.addressNList); addr.setCoinName(msg.coin); addr.setShowDisplay(msg.showDisplay || false); - addr.setScriptType(translateInputScriptType(msg.scriptType || core.BTCInputScriptType.SpendAddress)); + addr.setScriptType(translateInputScriptType(msg.scriptType || core.BTCScriptType.Legacy)); const response = await transport.call(Messages.MessageType.MESSAGETYPE_GETADDRESS, addr, { msgTimeout: core.LONG_TIMEOUT, @@ -485,7 +481,7 @@ export async function btcSignMessage( sign.setAddressNList(msg.addressNList); sign.setMessage(toUTF8Array(msg.message)); sign.setCoinName(msg.coin || "Bitcoin"); - sign.setScriptType(translateInputScriptType(msg.scriptType ?? core.BTCInputScriptType.SpendAddress)); + sign.setScriptType(translateInputScriptType(msg.scriptType ?? core.BTCScriptType.Legacy)); const event = await transport.call(Messages.MessageType.MESSAGETYPE_SIGNMESSAGE, sign, { msgTimeout: core.LONG_TIMEOUT, }); diff --git a/packages/hdwallet-keepkey/src/keepkey.ts b/packages/hdwallet-keepkey/src/keepkey.ts index 8d94404c7..81e6b1ac3 100644 --- a/packages/hdwallet-keepkey/src/keepkey.ts +++ b/packages/hdwallet-keepkey/src/keepkey.ts @@ -25,117 +25,15 @@ export function isKeepKey(wallet: core.HDWallet): wallet is KeepKeyHDWallet { function describeUTXOPath( path: core.BIP32Path, coin: core.Coin, - scriptType?: core.BTCInputScriptType + scriptType?: core.BTCScriptType ): core.PathDescription { - const pathStr = core.addressNListToBIP32(path); - const unknown: core.PathDescription = { - verbose: pathStr, - coin, - scriptType, - isKnown: false, - }; - if (!scriptType) return unknown; + const unknown = core.unknownUTXOPath(path, coin, scriptType); + if (!scriptType) return unknown; if (!Btc.btcSupportsCoin(coin)) return unknown; - if (!Btc.btcSupportsScriptType(coin, scriptType)) return unknown; - if (path.length !== 3 && path.length !== 5) return unknown; - - if ((path[0] & 0x80000000) >>> 0 !== 0x80000000) return unknown; - - const purpose = path[0] & 0x7fffffff; - - if (![44, 49, 84].includes(purpose)) return unknown; - - if (purpose === 44 && scriptType !== core.BTCInputScriptType.SpendAddress) return unknown; - - if (purpose === 49 && scriptType !== core.BTCInputScriptType.SpendP2SHWitness) return unknown; - - if (purpose === 84 && scriptType !== core.BTCInputScriptType.SpendWitness) return unknown; - - const wholeAccount = path.length === 3; - - const script = scriptType - ? ( - { - [core.BTCInputScriptType.SpendAddress]: ["Legacy"], - [core.BTCInputScriptType.SpendP2SHWitness]: [], - [core.BTCInputScriptType.SpendWitness]: ["Segwit Native"], - } as Partial> - )[scriptType] ?? [] - : []; - - let isPrefork = false; - const slip44 = core.slip44ByCoin(coin); - if (slip44 === undefined) return unknown; - if (path[1] !== 0x80000000 + slip44) { - switch (coin) { - case "BitcoinCash": - case "BitcoinGold": { - if (path[1] === 0x80000000 + core.slip44ByCoin("Bitcoin")) { - isPrefork = true; - break; - } - return unknown; - } - case "BitcoinSV": { - if ( - path[1] === 0x80000000 + core.slip44ByCoin("Bitcoin") || - path[1] === 0x80000000 + core.slip44ByCoin("BitcoinCash") - ) { - isPrefork = true; - break; - } - return unknown; - } - default: - return unknown; - } - } - - let attributes = isPrefork ? ["Prefork"] : []; - switch (coin) { - case "Bitcoin": - case "Litecoin": - case "BitcoinGold": - case "Testnet": { - attributes = attributes.concat(script); - break; - } - default: - break; - } - - const attr = attributes.length ? ` (${attributes.join(", ")})` : ""; - - const accountIdx = path[2] & 0x7fffffff; - - if (wholeAccount) { - return { - coin, - verbose: `${coin} Account #${accountIdx}${attr}`, - accountIdx, - wholeAccount: true, - isKnown: true, - scriptType, - isPrefork, - }; - } else { - const change = path[3] === 1 ? "Change " : ""; - const addressIdx = path[4]; - return { - coin, - verbose: `${coin} Account #${accountIdx}, ${change}Address #${addressIdx}${attr}`, - accountIdx, - addressIdx, - wholeAccount: false, - isKnown: true, - isChange: path[3] === 1, - scriptType, - isPrefork, - }; - } + return core.describeUTXOPath(path, coin, scriptType); } function describeEosPath(path: core.BIP32Path): core.PathDescription { @@ -247,7 +145,7 @@ export class KeepKeyHDWalletInfo return Btc.btcSupportsCoin(coin); } - public async btcSupportsScriptType(coin: core.Coin, scriptType: core.BTCInputScriptType): Promise { + public async btcSupportsScriptType(coin: core.Coin, scriptType: core.BTCScriptType): Promise { return Btc.btcSupportsScriptType(coin, scriptType); } @@ -613,7 +511,7 @@ export class KeepKeyHDWallet GPK.setAddressNList(addressNList); GPK.setShowDisplay(showDisplay || false); GPK.setEcdsaCurveName(curve || "secp256k1"); - GPK.setScriptType(translateInputScriptType(scriptType || core.BTCInputScriptType.SpendAddress)); + GPK.setScriptType(translateInputScriptType(scriptType || core.BTCScriptType.Legacy)); const event = await this.transport.call(Messages.MessageType.MESSAGETYPE_GETPUBLICKEY, GPK, { msgTimeout: showDisplay ? core.LONG_TIMEOUT : core.DEFAULT_TIMEOUT, diff --git a/packages/hdwallet-keepkey/src/utils.ts b/packages/hdwallet-keepkey/src/utils.ts index e6d249003..c96313597 100644 --- a/packages/hdwallet-keepkey/src/utils.ts +++ b/packages/hdwallet-keepkey/src/utils.ts @@ -34,30 +34,29 @@ export function toUTF8Array(str: string): Uint8Array { return new Uint8Array(utf8); } -export function translateInputScriptType(scriptType: core.BTCInputScriptType): any { +export function translateInputScriptType(scriptType: core.BTCScriptType): any { switch (scriptType) { - case core.BTCInputScriptType.CashAddr: - case core.BTCInputScriptType.SpendAddress: + case core.BTCScriptType.Legacy: return Types.InputScriptType.SPENDADDRESS; - case core.BTCInputScriptType.SpendMultisig: + case core.BTCScriptType.LegacyMultisig: return Types.InputScriptType.SPENDMULTISIG; - case core.BTCInputScriptType.SpendP2SHWitness: + case core.BTCScriptType.Segwit: return Types.InputScriptType.SPENDP2SHWITNESS; - case core.BTCInputScriptType.SpendWitness: + case core.BTCScriptType.SegwitNative: return Types.InputScriptType.SPENDWITNESS; } throw new Error("unhandled InputSriptType enum: " + scriptType); } -export function translateOutputScriptType(scriptType: core.BTCOutputScriptType): any { +export function translateOutputScriptType(scriptType: core.BTCScriptType): any { switch (scriptType) { - case core.BTCOutputScriptType.PayToAddress: + case core.BTCScriptType.Legacy: return Types.OutputScriptType.PAYTOADDRESS; - case core.BTCOutputScriptType.PayToMultisig: + case core.BTCScriptType.LegacyMultisig: return Types.OutputScriptType.PAYTOMULTISIG; - case core.BTCOutputScriptType.PayToP2SHWitness: + case core.BTCScriptType.Segwit: return Types.OutputScriptType.PAYTOP2SHWITNESS; - case core.BTCOutputScriptType.PayToWitness: + case core.BTCScriptType.SegwitNative: return Types.OutputScriptType.PAYTOWITNESS; } throw new Error("unhandled OutputScriptType enum: " + scriptType); diff --git a/packages/hdwallet-ledger/src/bitcoin.ts b/packages/hdwallet-ledger/src/bitcoin.ts index 0c8a902a7..ce2fddfe2 100644 --- a/packages/hdwallet-ledger/src/bitcoin.ts +++ b/packages/hdwallet-ledger/src/bitcoin.ts @@ -1,9 +1,9 @@ import ecc from "@bitcoinerlab/secp256k1"; -import { CreateTransactionArg } from "@ledgerhq/hw-app-btc/lib/createTransaction"; +import { AddressFormat, CreateTransactionArg } from "@ledgerhq/hw-app-btc/lib/createTransaction"; import { Transaction } from "@ledgerhq/hw-app-btc/lib/types"; import * as bitcoin from "@shapeshiftoss/bitcoinjs-lib"; import * as core from "@shapeshiftoss/hdwallet-core"; -import { BTCInputScriptType, convertXpubVersion, scriptTypeToAccountType } from "@shapeshiftoss/hdwallet-core"; +import { BTCScriptType, convertXpubVersion, scriptTypeToAccountType } from "@shapeshiftoss/hdwallet-core"; import Base64 from "base64-js"; import * as bchAddr from "bchaddrjs"; import * as bitcoinMsg from "bitcoinjs-message"; @@ -11,7 +11,7 @@ import zip from "lodash/zip"; import { currencies } from "./currencies"; import { LedgerTransport } from "./transport"; -import { handleError, networksUtil, translateScriptType } from "./utils"; +import { handleError, networksUtil } from "./utils"; export const supportedCoins = ["Testnet", "Bitcoin", "BitcoinCash", "Litecoin", "Dash", "DigiByte", "Dogecoin"]; @@ -21,15 +21,11 @@ export async function btcSupportsCoin(coin: core.Coin): Promise { return supportedCoins.includes(coin); } -export async function btcSupportsScriptType(coin: core.Coin, scriptType?: core.BTCInputScriptType): Promise { +export async function btcSupportsScriptType(coin: core.Coin, scriptType?: core.BTCScriptType): Promise { const supported = { - Bitcoin: [ - core.BTCInputScriptType.SpendAddress, - core.BTCInputScriptType.SpendWitness, - core.BTCInputScriptType.SpendP2SHWitness, - ], - BitcoinCash: [core.BTCInputScriptType.SpendAddress], - } as Partial>>; + Bitcoin: [core.BTCScriptType.Legacy, core.BTCScriptType.SegwitNative, core.BTCScriptType.Segwit], + BitcoinCash: [core.BTCScriptType.Legacy], + } as Partial>>; const scriptTypes = supported[coin]; return !!scriptTypes && !!scriptType && scriptTypes.includes(scriptType); @@ -38,15 +34,23 @@ export async function btcSupportsScriptType(coin: core.Coin, scriptType?: core.B export async function btcGetAddress(transport: LedgerTransport, msg: core.BTCGetAddress): Promise { const bip32path = core.addressNListToBIP32(msg.addressNList); - const scriptTypeish = (() => { - if (msg.coin === "BitcoinCash") return core.BTCInputScriptType.CashAddr; - if (msg.scriptType) return msg.scriptType; - return core.BTCInputScriptType.SpendAddress; + const addressFormat = ((): AddressFormat => { + if (msg.coin === "BitcoinCash") return "cashaddr"; + switch (msg.scriptType) { + case core.BTCScriptType.Legacy: + return "legacy"; + case core.BTCScriptType.SegwitNative: + return "bech32"; + case core.BTCScriptType.Segwit: + return "p2sh"; + default: + return "legacy"; + } })(); const opts = { verify: !!msg.showDisplay, - format: translateScriptType(scriptTypeish), + format: addressFormat, }; const res = await transport.call("Btc", "getWalletPublicKey", bip32path, opts); @@ -156,7 +160,7 @@ export async function btcSignTx( if (output.addressNList !== undefined && output.isChange) { const maybeOutputAddress = await wallet.btcGetAddress({ addressNList: output.addressNList, - scriptType: output.scriptType as unknown as BTCInputScriptType, + scriptType: output.scriptType as unknown as BTCScriptType, coin: msg.coin, }); if (!maybeOutputAddress) throw new Error("could not determine output address from addressNList"); @@ -197,8 +201,8 @@ export async function btcSignTx( for (let i = 0; i < msg.inputs.length; i++) { if ( - msg.inputs[i].scriptType === core.BTCInputScriptType.SpendWitness || - msg.inputs[i].scriptType === core.BTCInputScriptType.SpendP2SHWitness + msg.inputs[i].scriptType === core.BTCScriptType.SegwitNative || + msg.inputs[i].scriptType === core.BTCScriptType.Segwit ) segwit = true; @@ -229,7 +233,7 @@ export async function btcSignTx( outputScriptHex, additionals: (() => { if (msg.coin === "BitcoinCash") return ["abc"]; - if (msg.inputs.some((input) => input.scriptType === core.BTCInputScriptType.SpendWitness)) return ["bech32"]; + if (msg.inputs.some((input) => input.scriptType === core.BTCScriptType.SegwitNative)) return ["bech32"]; return []; })(), @@ -278,7 +282,7 @@ export async function btcSignMessage( addressNList: msg.addressNList, coin, showDisplay: false, - scriptType: msg.scriptType ?? core.BTCInputScriptType.SpendAddress, + scriptType: msg.scriptType ?? core.BTCScriptType.Legacy, }); return { @@ -297,17 +301,17 @@ export function btcGetAccountPaths(msg: core.BTCGetAccountPaths): Array>> 0 !== 0x80000000) return unknown; - - const purpose = path[0] & 0x7fffffff; - - if (![44, 49, 84].includes(purpose)) return unknown; - - if (purpose === 44 && scriptType !== core.BTCInputScriptType.SpendAddress) return unknown; - - if (purpose === 49 && scriptType !== core.BTCInputScriptType.SpendP2SHWitness) return unknown; - - if (purpose === 84 && scriptType !== core.BTCInputScriptType.SpendWitness) return unknown; - - const slip44 = core.slip44ByCoin(coin); - if (slip44 === undefined || path[1] !== 0x80000000 + slip44) return unknown; - - const wholeAccount = path.length === 3; - - let script = scriptType - ? ( - { - [core.BTCInputScriptType.SpendAddress]: " (Legacy)", - [core.BTCInputScriptType.SpendP2SHWitness]: "", - [core.BTCInputScriptType.SpendWitness]: " (Segwit Native)", - } as Partial> - )[scriptType] - : undefined; - - switch (coin) { - case "Bitcoin": - case "Litecoin": - case "BitcoinGold": - case "Testnet": - break; - default: - script = ""; - } - - const accountIdx = path[2] & 0x7fffffff; - - if (wholeAccount) { - return { - verbose: `${coin} Account #${accountIdx}${script}`, - accountIdx, - coin, - scriptType, - wholeAccount: true, - isKnown: true, - isPrefork: false, - }; - } else { - const change = path[3] == 1 ? "Change " : ""; - const addressIdx = path[4]; - return { - verbose: `${coin} Account #${accountIdx}, ${change}Address #${addressIdx}${script}`, - coin, - scriptType, - accountIdx, - addressIdx, - wholeAccount: false, - isChange: path[3] == 1, - isKnown: true, - isPrefork: false, - }; - } + return core.describeUTXOPath(path, coin, scriptType); } export class LedgerHDWalletInfo @@ -163,7 +92,7 @@ export class LedgerHDWalletInfo return btc.btcSupportsCoin(coin); } - public async btcSupportsScriptType(coin: core.Coin, scriptType: core.BTCInputScriptType): Promise { + public async btcSupportsScriptType(coin: core.Coin, scriptType: core.BTCScriptType): Promise { return btc.btcSupportsScriptType(coin, scriptType); } diff --git a/packages/hdwallet-ledger/src/utils.ts b/packages/hdwallet-ledger/src/utils.ts index 0e1550fba..8d6b6d0d6 100644 --- a/packages/hdwallet-ledger/src/utils.ts +++ b/packages/hdwallet-ledger/src/utils.ts @@ -1,4 +1,3 @@ -import type { AddressFormat } from "@ledgerhq/hw-app-btc"; import * as core from "@shapeshiftoss/hdwallet-core"; import bs58check from "bs58check"; @@ -57,16 +56,6 @@ export function handleError>( } } -export function translateScriptType(scriptType: core.BTCInputScriptType): AddressFormat { - const scriptTypeMap: Partial> = { - [core.BTCInputScriptType.SpendAddress]: "legacy", - [core.BTCInputScriptType.CashAddr]: "cashaddr", - [core.BTCInputScriptType.SpendWitness]: "bech32", - [core.BTCInputScriptType.SpendP2SHWitness]: "p2sh", - }; - return core.mustBeDefined(scriptTypeMap[scriptType]); -} - export const compressPublicKey = (publicKey: Uint8Array) => { if ([0x02, 0x03].includes(publicKey[0]) && publicKey.length === 33) return Buffer.from(publicKey); if (!(publicKey[0] === 0x04 && publicKey.length === 65)) throw new Error("Invalid public key format"); @@ -104,7 +93,7 @@ type NetworkMagic = { bech32?: string; bip32: { private?: number; - public: Partial>; + public: Partial>; }; messagePrefix: string; pubKeyHash?: number; diff --git a/packages/hdwallet-metamask-multichain/src/shapeshift-multichain.ts b/packages/hdwallet-metamask-multichain/src/shapeshift-multichain.ts index 5149a6f67..2ba6c48f6 100644 --- a/packages/hdwallet-metamask-multichain/src/shapeshift-multichain.ts +++ b/packages/hdwallet-metamask-multichain/src/shapeshift-multichain.ts @@ -544,7 +544,7 @@ export class MetaMaskMultiChainHDWallet throw new Error("Method not implemented."); } - public async btcSupportsScriptType(coin: string, scriptType?: core.BTCInputScriptType | undefined): Promise { + public async btcSupportsScriptType(coin: string, scriptType?: core.BTCScriptType | undefined): Promise { return utxo.utxoSupportsScriptType(coin, scriptType); } diff --git a/packages/hdwallet-metamask-multichain/src/utxo.ts b/packages/hdwallet-metamask-multichain/src/utxo.ts index 35f1e8f02..c5d57ac00 100644 --- a/packages/hdwallet-metamask-multichain/src/utxo.ts +++ b/packages/hdwallet-metamask-multichain/src/utxo.ts @@ -2,44 +2,19 @@ import * as core from "@shapeshiftoss/hdwallet-core"; const supportedCoins = ["Bitcoin", "BitcoinCash", "Litecoin", "Dogecoin"]; -function legacyAccount(coin: core.Coin, slip44: number, accountIdx: number): core.BTCAccountPath { - return { - coin, - scriptType: core.BTCInputScriptType.SpendAddress, - addressNList: [0x80000000 + 44, 0x80000000 + slip44, 0x80000000 + accountIdx], - }; -} - -function segwitAccount(coin: core.Coin, slip44: number, accountIdx: number): core.BTCAccountPath { - return { - coin, - scriptType: core.BTCInputScriptType.SpendP2SHWitness, - addressNList: [0x80000000 + 49, 0x80000000 + slip44, 0x80000000 + accountIdx], - }; -} - -function segwitNativeAccount(coin: core.Coin, slip44: number, accountIdx: number): core.BTCAccountPath { - return { - coin, - scriptType: core.BTCInputScriptType.SpendWitness, - addressNList: [0x80000000 + 84, 0x80000000 + slip44, 0x80000000 + accountIdx], - }; -} - export function utxoSupportsCoin(coin: core.Coin): boolean { // FIXME: inspect the CoinTable to determine which coins are actually supported by the device. return supportedCoins.includes(coin); } -export function utxoSupportsScriptType(coin: core.Coin, scriptType?: core.BTCInputScriptType): boolean { +export function utxoSupportsScriptType(coin: core.Coin, scriptType?: core.BTCScriptType): boolean { if (!utxoSupportsCoin(coin)) return false; switch (scriptType) { - case core.BTCInputScriptType.SpendMultisig: - case core.BTCInputScriptType.SpendAddress: - case core.BTCInputScriptType.SpendWitness: - case core.BTCInputScriptType.Bech32: - case core.BTCInputScriptType.SpendP2SHWitness: + case core.BTCScriptType.LegacyMultisig: + case core.BTCScriptType.Legacy: + case core.BTCScriptType.SegwitNative: + case core.BTCScriptType.Segwit: return true; default: return false; @@ -49,133 +24,31 @@ export function utxoSupportsScriptType(coin: core.Coin, scriptType?: core.BTCInp export function describeUTXOPath( path: core.BIP32Path, coin: core.Coin, - scriptType?: core.BTCInputScriptType + scriptType?: core.BTCScriptType ): core.PathDescription { - const pathStr = core.addressNListToBIP32(path); - const unknown: core.PathDescription = { - verbose: pathStr, - coin, - scriptType, - isKnown: false, - }; - if (!scriptType) return unknown; + const unknown = core.unknownUTXOPath(path, coin, scriptType); + if (!scriptType) return unknown; if (!utxoSupportsCoin(coin)) return unknown; - if (!utxoSupportsScriptType(coin, scriptType)) return unknown; - if (path.length !== 3 && path.length !== 5) return unknown; - - if ((path[0] & 0x80000000) >>> 0 !== 0x80000000) return unknown; - - const purpose = path[0] & 0x7fffffff; - - if (![44, 49, 84].includes(purpose)) return unknown; - - if (purpose === 44 && scriptType !== core.BTCInputScriptType.SpendAddress) return unknown; - - if (purpose === 49 && scriptType !== core.BTCInputScriptType.SpendP2SHWitness) return unknown; - - if (purpose === 84 && scriptType !== core.BTCInputScriptType.SpendWitness) return unknown; - - const wholeAccount = path.length === 3; - - const script = scriptType - ? ( - { - [core.BTCInputScriptType.SpendAddress]: ["Legacy"], - [core.BTCInputScriptType.SpendP2SHWitness]: [], - [core.BTCInputScriptType.SpendWitness]: ["Segwit Native"], - } as Partial> - )[scriptType] ?? [] - : []; - - let isPrefork = false; - const slip44 = core.slip44ByCoin(coin); - if (slip44 === undefined) return unknown; - if (path[1] !== 0x80000000 + slip44) { - switch (coin) { - case "BitcoinCash": - case "BitcoinGold": { - if (path[1] === 0x80000000 + core.slip44ByCoin("Bitcoin")) { - isPrefork = true; - break; - } - return unknown; - } - case "BitcoinSV": { - if ( - path[1] === 0x80000000 + core.slip44ByCoin("Bitcoin") || - path[1] === 0x80000000 + core.slip44ByCoin("BitcoinCash") - ) { - isPrefork = true; - break; - } - return unknown; - } - default: - return unknown; - } - } - - let attributes = isPrefork ? ["Prefork"] : []; - switch (coin) { - case "Bitcoin": - case "Litecoin": - case "BitcoinGold": - case "Testnet": { - attributes = attributes.concat(script); - break; - } - default: - break; - } - - const attr = attributes.length ? ` (${attributes.join(", ")})` : ""; - - const accountIdx = path[2] & 0x7fffffff; - - if (wholeAccount) { - return { - coin, - verbose: `${coin} Account #${accountIdx}${attr}`, - accountIdx, - wholeAccount: true, - isKnown: true, - scriptType, - isPrefork, - }; - } else { - const change = path[3] === 1 ? "Change " : ""; - const addressIdx = path[4]; - return { - coin, - verbose: `${coin} Account #${accountIdx}, ${change}Address #${addressIdx}${attr}`, - accountIdx, - addressIdx, - wholeAccount: false, - isKnown: true, - isChange: path[3] === 1, - scriptType, - isPrefork, - }; - } + return core.describeUTXOPath(path, coin, scriptType); } export function utxoGetAccountPaths(msg: core.BTCGetAccountPaths): Array { const slip44 = core.slip44ByCoin(msg.coin); if (slip44 === undefined) return []; - const bip44 = legacyAccount(msg.coin, slip44, msg.accountIdx); - const bip49 = segwitAccount(msg.coin, slip44, msg.accountIdx); - const bip84 = segwitNativeAccount(msg.coin, slip44, msg.accountIdx); + const bip44 = core.legacyAccount(msg.coin, slip44, msg.accountIdx); + const bip49 = core.segwitAccount(msg.coin, slip44, msg.accountIdx); + const bip84 = core.segwitNativeAccount(msg.coin, slip44, msg.accountIdx); // For BTC Forks - const btcLegacy = legacyAccount(msg.coin, core.slip44ByCoin("Bitcoin"), msg.accountIdx); - const btcSegwit = segwitAccount(msg.coin, core.slip44ByCoin("Bitcoin"), msg.accountIdx); - const btcSegwitNative = segwitNativeAccount(msg.coin, core.slip44ByCoin("Bitcoin"), msg.accountIdx); + const btcLegacy = core.legacyAccount(msg.coin, core.slip44ByCoin("Bitcoin"), msg.accountIdx); + const btcSegwit = core.segwitAccount(msg.coin, core.slip44ByCoin("Bitcoin"), msg.accountIdx); + const btcSegwitNative = core.segwitNativeAccount(msg.coin, core.slip44ByCoin("Bitcoin"), msg.accountIdx); // For BCH Forks - const bchLegacy = legacyAccount(msg.coin, core.slip44ByCoin("BitcoinCash"), msg.accountIdx); + const bchLegacy = core.legacyAccount(msg.coin, core.slip44ByCoin("BitcoinCash"), msg.accountIdx); let paths: Array = ( diff --git a/packages/hdwallet-native/src/bitcoin.test.ts b/packages/hdwallet-native/src/bitcoin.test.ts index 8066d7c91..88b9ec53e 100644 --- a/packages/hdwallet-native/src/bitcoin.test.ts +++ b/packages/hdwallet-native/src/bitcoin.test.ts @@ -150,9 +150,6 @@ describe("NativeBTCWalletInfo", () => { expect(await info.btcSupportsScriptType("bitcoin", "p2sh" as any)).toBe(false); expect(await info.btcSupportsScriptType("bitcoin", "p2wpkh" as any)).toBe(true); expect(await info.btcSupportsScriptType("bitcoin", "p2sh-p2wpkh" as any)).toBe(true); - expect(await info.btcSupportsScriptType("bitcoin", "bech32" as any)).toBe(true); - expect(await info.btcSupportsScriptType("bitcoin", "cashaddr" as any)).toBe(false); - expect(await info.btcSupportsScriptType("bitcoincash", "cashaddr" as any)).toBe(false); expect(await info.btcSupportsScriptType("bitcoin", "foobar" as any)).toBe(false); expect(await info.btcSupportsScriptType("foobar", "p2pkh" as any)).toBe(false); }); @@ -335,7 +332,6 @@ describe("NativeBTCWallet", () => { [ "Bitcoin Cash", "BitcoinCash", - // maybe "cashaddr" should work here, but it doesn't. "p2pkh", [ ["m/44'/145'/0'/0/0", "bitcoincash:qr08q88p9etk89wgv05nwlrkm4l0urz4cyl36hh9sv"], @@ -431,7 +427,7 @@ describe("NativeBTCWallet", () => { outputs: [ { addressNList: core.bip32ToAddressNList("m/44'/0'/0'/0/1"), - scriptType: core.BTCOutputScriptType.PayToAddress, + scriptType: core.BTCScriptType.Legacy, amount: "99000", isChange: false, }, diff --git a/packages/hdwallet-native/src/bitcoin.ts b/packages/hdwallet-native/src/bitcoin.ts index ac14c3664..345cd4d85 100644 --- a/packages/hdwallet-native/src/bitcoin.ts +++ b/packages/hdwallet-native/src/bitcoin.ts @@ -7,8 +7,7 @@ import { NativeHDWalletBase } from "./native"; import * as util from "./util"; const supportedCoins = ["bitcoin", "dash", "digibyte", "dogecoin", "litecoin", "bitcoincash", "testnet"]; - -const segwit = ["p2wpkh", "p2sh-p2wpkh", "bech32"]; +const segwit = ["p2wpkh", "p2sh-p2wpkh"]; type NonWitnessUtxo = Buffer; @@ -39,21 +38,20 @@ export function MixinNativeBTCWalletInfo { + async btcSupportsScriptType(coin: core.Coin, scriptType: core.BTCScriptType): Promise { return this.btcSupportsScriptTypeSync(coin, scriptType); } @@ -103,9 +101,9 @@ export function MixinNativeBTCWalletInfo { msg: { coin: "bitcoin", path: [44 + 0x80000000, 0 + 0x80000000, 0 + 0x80000000, 0, 0], - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }, out: { coin: "bitcoin", verbose: "m/44'/0'/0'/0/0", isKnown: false }, }, @@ -46,7 +46,7 @@ describe("NativeHDWalletInfo", () => { msg: { coin: "Bitcoin", path: [44 + 0x80000000, 0 + 0x80000000, 0 + 0x80000000], - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }, out: { coin: "Bitcoin", verbose: "Bitcoin Account #0 (Legacy)", isKnown: true, wholeAccount: true }, }, @@ -54,13 +54,13 @@ describe("NativeHDWalletInfo", () => { msg: { coin: "Bitcoin", path: [44 + 0x80000000, 0 + 0x80000000, 0 + 0x80000000, 0, 0], - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }, out: { coin: "Bitcoin", verbose: "Bitcoin Account #0, Address #0 (Legacy)", isKnown: true }, }, { - msg: { coin: "dash", path: [1, 2, 3], scriptType: core.BTCInputScriptType.SpendWitness }, - out: { coin: "dash", verbose: "m/1/2/3", scriptType: core.BTCInputScriptType.SpendWitness, isKnown: false }, + msg: { coin: "dash", path: [1, 2, 3], scriptType: core.BTCScriptType.SegwitNative }, + out: { coin: "dash", verbose: "m/1/2/3", scriptType: core.BTCScriptType.SegwitNative, isKnown: false }, }, { msg: { coin: "bitcoincash", path: [1, 2, 3] }, @@ -259,7 +259,7 @@ describe("NativeHDWallet", () => { await wallet.btcGetAddress({ coin: "bitcoin", addressNList: [44 + 0x80000000, 0 + 0x80000000], - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toStrictEqual("1Hvzdx2kSLHT93aTnEeDNDSo4DS1Wn3CML"); }); @@ -287,7 +287,7 @@ describe("NativeHDWallet", () => { await wallet.btcGetAddress({ coin: "bitcoin", addressNList: [44 + 0x80000000, 0 + 0x80000000, 0 + 0x80000000, 0, 0], - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, }) ).toStrictEqual("1FH6ehAd5ZFXCM1cLGzHxK1s4dGdq1JusM"); }); diff --git a/packages/hdwallet-native/src/native.ts b/packages/hdwallet-native/src/native.ts index 730fd73bf..97e13ce03 100644 --- a/packages/hdwallet-native/src/native.ts +++ b/packages/hdwallet-native/src/native.ts @@ -311,7 +311,7 @@ export class NativeHDWallet // TODO: return the xpub that's actually asked for, not the key of the hardened path // It's done this way for hilarious historical reasons and will break ETH if fixed const hardenedPath = core.hardenedPath(addressNList); - const network = core.getNetwork(coin, scriptType ?? core.BTCOutputScriptType.PayToMultisig); + const network = core.getNetwork(coin, scriptType); let node = await Isolation.Adapters.BIP32.create(secp256k1MasterKey, network); if (hardenedPath.length > 0) node = await node.derivePath(core.addressNListToBIP32(hardenedPath)); diff --git a/packages/hdwallet-native/src/util.ts b/packages/hdwallet-native/src/util.ts index f23acf43c..724eb1007 100644 --- a/packages/hdwallet-native/src/util.ts +++ b/packages/hdwallet-native/src/util.ts @@ -8,7 +8,7 @@ export async function getKeyPair( coin: core.Coin, scriptType?: core.BTCScriptType ): Promise { - const network = core.getNetwork(coin, scriptType ?? core.BTCOutputScriptType.PayToMultisig); + const network = core.getNetwork(coin, scriptType); const wallet = await Isolation.Adapters.BIP32.create(node, network); const path = core.addressNListToBIP32(addressNList); return await wallet.derivePath(path); diff --git a/packages/hdwallet-phantom/src/bitcoin.ts b/packages/hdwallet-phantom/src/bitcoin.ts index 191b0c349..2c3a7a5e3 100644 --- a/packages/hdwallet-phantom/src/bitcoin.ts +++ b/packages/hdwallet-phantom/src/bitcoin.ts @@ -1,7 +1,7 @@ import ecc from "@bitcoinerlab/secp256k1"; import * as bitcoin from "@shapeshiftoss/bitcoinjs-lib"; import * as core from "@shapeshiftoss/hdwallet-core"; -import { BTCInputScriptType } from "@shapeshiftoss/hdwallet-core"; +import { BTCScriptType } from "@shapeshiftoss/hdwallet-core"; import { PhantomUtxoProvider } from "./types"; @@ -9,7 +9,7 @@ export type BtcAccount = { address: string; // Phantom supposedly supports more scriptTypes but in effect, doesn't (currently) // https://github.com/orgs/phantom/discussions/173 - addressType: BTCInputScriptType.SpendWitness; + addressType: BTCScriptType.SegwitNative; publicKey: string; purpose: "payment" | "ordinals"; }; @@ -55,7 +55,7 @@ async function addInput(psbt: bitcoin.Psbt, input: core.BTCSignTxInput): Promise switch (input.scriptType) { // Phantom supposedly supports more scriptTypes but in effect, doesn't (currently) // https://github.com/orgs/phantom/discussions/173 - case BTCInputScriptType.SpendWitness: { + case BTCScriptType.SegwitNative: { psbt.addInput({ hash: input.txid, index: input.vout, @@ -81,7 +81,12 @@ async function addOutput( if (output.address) return output.address; if (output.addressNList) { - const outputAddress = await wallet.btcGetAddress({ addressNList: output.addressNList, coin, showDisplay: false }); + const outputAddress = await wallet.btcGetAddress({ + addressNList: output.addressNList, + coin, + scriptType: output.scriptType, + showDisplay: false, + }); if (!outputAddress) throw new Error("Could not get address from wallet"); return outputAddress; } @@ -131,6 +136,7 @@ export async function bitcoinSignTx( const address = await wallet.btcGetAddress({ addressNList: input.addressNList, coin: msg.coin, + scriptType: input.scriptType, showDisplay: false, }); diff --git a/packages/hdwallet-phantom/src/phantom.ts b/packages/hdwallet-phantom/src/phantom.ts index a78eee83e..18d24193f 100644 --- a/packages/hdwallet-phantom/src/phantom.ts +++ b/packages/hdwallet-phantom/src/phantom.ts @@ -1,5 +1,5 @@ import * as core from "@shapeshiftoss/hdwallet-core"; -import { Address, BTCInputScriptType } from "@shapeshiftoss/hdwallet-core"; +import { Address, BTCScriptType } from "@shapeshiftoss/hdwallet-core"; import Base64 from "base64-js"; import * as bitcoinMsg from "bitcoinjs-message"; import { keccak256, recoverAddress } from "ethers/lib/utils.js"; @@ -130,11 +130,11 @@ export class PhantomHDWalletInfo return coin.toLowerCase() === "bitcoin"; } - public async btcSupportsScriptType(coin: string, scriptType?: core.BTCInputScriptType | undefined): Promise { + public async btcSupportsScriptType(coin: string, scriptType?: core.BTCScriptType | undefined): Promise { if (!this.btcSupportsCoin(coin)) return false; switch (scriptType) { - case core.BTCInputScriptType.SpendWitness: + case core.BTCScriptType.SegwitNative: return true; default: return false; @@ -278,7 +278,7 @@ export class PhantomHDWallet const { coin, scriptType } = getPublicKey; // Only p2wpkh effectively supported for now - if (coin === "Bitcoin" && scriptType === BTCInputScriptType.SpendWitness) { + if (coin === "Bitcoin" && scriptType === BTCScriptType.SegwitNative) { // Note this is a pubKey, not an xpub, however phantom does not support utxo derivation, // so this functions as an account (xpub) for all intents and purposes const pubKey = await this.btcGetAddress({ coin: "Bitcoin" } as core.BTCGetAddress); diff --git a/packages/hdwallet-portis/src/bitcoin.ts b/packages/hdwallet-portis/src/bitcoin.ts index d9f50927d..46a6d05da 100644 --- a/packages/hdwallet-portis/src/bitcoin.ts +++ b/packages/hdwallet-portis/src/bitcoin.ts @@ -4,122 +4,43 @@ import Base64 from "base64-js"; import * as bip32 from "bip32"; import * as bitcoinMsg from "bitcoinjs-message"; +export async function btcSupportsScriptType(coin: core.Coin, scriptType?: core.BTCScriptType): Promise { + if (coin !== "Bitcoin") return Promise.resolve(false); + + switch (scriptType) { + case core.BTCScriptType.Legacy: + case core.BTCScriptType.SegwitNative: + case core.BTCScriptType.Segwit: + return true; + default: + return false; + } +} + +export async function btcSupportsCoin(coin: core.Coin): Promise { + if (coin === "Bitcoin") return true; + else return false; +} + export function describeUTXOPath( path: core.BIP32Path, coin: core.Coin, - scriptType?: core.BTCInputScriptType + scriptType?: core.BTCScriptType ): core.PathDescription { - const pathStr = core.addressNListToBIP32(path); - const unknown: core.PathDescription = { - verbose: pathStr, - coin, - scriptType, - isKnown: false, - }; + const unknown = core.unknownUTXOPath(path, coin, scriptType); - if (path.length !== 3 && path.length !== 5) return unknown; - - if ((path[0] & 0x80000000) >>> 0 !== 0x80000000) return unknown; - - const purpose = path[0] & 0x7fffffff; - - if (![44, 49, 84].includes(purpose)) return unknown; - - if (purpose === 44 && scriptType !== core.BTCInputScriptType.SpendAddress) return unknown; - - if (purpose === 49 && scriptType !== core.BTCInputScriptType.SpendP2SHWitness) return unknown; - - if (purpose === 84 && scriptType !== core.BTCInputScriptType.SpendWitness) return unknown; - - const wholeAccount = path.length === 3; - - const script = scriptType - ? ( - { - [core.BTCInputScriptType.SpendAddress]: ["Legacy"], - [core.BTCInputScriptType.SpendP2SHWitness]: [], - [core.BTCInputScriptType.SpendWitness]: ["Segwit Native"], - } as Partial> - )[scriptType] ?? ([] as string[]) - : ([] as string[]); - - let isPrefork = false; - const slip44 = core.slip44ByCoin(coin); - if (slip44 === undefined) return unknown; - if (path[1] !== 0x80000000 + slip44) { - switch (coin) { - case "BitcoinCash": - case "BitcoinGold": { - if (path[1] === 0x80000000 + core.slip44ByCoin("Bitcoin")) { - isPrefork = true; - break; - } - return unknown; - } - case "BitcoinSV": { - if ( - path[1] === 0x80000000 + core.slip44ByCoin("Bitcoin") || - path[1] === 0x80000000 + core.slip44ByCoin("BitcoinCash") - ) { - isPrefork = true; - break; - } - return unknown; - } - default: - return unknown; - } - } + if (!scriptType) return unknown; + if (!btcSupportsCoin(coin)) return unknown; + if (!btcSupportsScriptType(coin, scriptType)) return unknown; - let attributes = isPrefork ? ["Prefork"] : []; - switch (coin) { - case "Bitcoin": - case "Litecoin": - case "BitcoinGold": - case "Testnet": { - attributes = attributes.concat(script); - break; - } - default: - break; - } - - const attr = attributes.length ? ` (${attributes.join(", ")})` : ""; - - const accountIdx = path[2] & 0x7fffffff; - - if (wholeAccount) { - return { - coin, - verbose: `${coin} Account #${accountIdx}${attr}`, - accountIdx, - wholeAccount: true, - isKnown: true, - scriptType, - isPrefork, - }; - } else { - const change = path[3] === 1 ? "Change " : ""; - const addressIdx = path[4]; - return { - coin, - verbose: `${coin} Account #${accountIdx}, ${change}Address #${addressIdx}${attr}`, - accountIdx, - addressIdx, - wholeAccount: false, - isKnown: true, - isChange: path[3] === 1, - scriptType, - isPrefork, - }; - } + return core.describeUTXOPath(path, coin, scriptType); } -export function verifyScriptTypePurpose(scriptType: core.BTCInputScriptType, purpose: number): boolean { +export function verifyScriptTypePurpose(scriptType: core.BTCScriptType, purpose: number): boolean { return ( - (purpose === 0x80000000 + 44 && scriptType === core.BTCInputScriptType.SpendAddress) || - (purpose === 0x80000000 + 49 && scriptType === core.BTCInputScriptType.SpendP2SHWitness) || - (purpose === 0x80000000 + 84 && scriptType === core.BTCInputScriptType.SpendWitness) + (purpose === 0x80000000 + 44 && scriptType === core.BTCScriptType.Legacy) || + (purpose === 0x80000000 + 49 && scriptType === core.BTCScriptType.Segwit) || + (purpose === 0x80000000 + 84 && scriptType === core.BTCScriptType.SegwitNative) ); } @@ -141,13 +62,13 @@ export async function btcGetAddress(msg: core.BTCGetAddress, portis: any): Promi let result: bitcoin.payments.Payment; switch (scriptType) { - case core.BTCInputScriptType.SpendAddress: + case core.BTCScriptType.Legacy: result = bitcoin.payments.p2pkh(args); break; - case core.BTCInputScriptType.SpendWitness: + case core.BTCScriptType.SegwitNative: result = bitcoin.payments.p2wpkh(args); break; - case core.BTCInputScriptType.SpendP2SHWitness: + case core.BTCScriptType.Segwit: result = bitcoin.payments.p2sh({ redeem: bitcoin.payments.p2wpkh(args), }); @@ -170,7 +91,7 @@ export async function btcGetAddress(msg: core.BTCGetAddress, portis: any): Promi export function legacyAccount(coin: core.Coin, slip44: number, accountIdx: number): core.BTCAccountPath { return { coin, - scriptType: core.BTCInputScriptType.SpendAddress, + scriptType: core.BTCScriptType.Legacy, addressNList: [0x80000000 + 44, 0x80000000 + slip44, 0x80000000 + accountIdx], }; } @@ -178,7 +99,7 @@ export function legacyAccount(coin: core.Coin, slip44: number, accountIdx: numbe export function segwitAccount(coin: core.Coin, slip44: number, accountIdx: number): core.BTCAccountPath { return { coin, - scriptType: core.BTCInputScriptType.SpendP2SHWitness, + scriptType: core.BTCScriptType.Segwit, addressNList: [0x80000000 + 49, 0x80000000 + slip44, 0x80000000 + accountIdx], }; } @@ -186,7 +107,7 @@ export function segwitAccount(coin: core.Coin, slip44: number, accountIdx: numbe export function segwitNativeAccount(coin: core.Coin, slip44: number, accountIdx: number): core.BTCAccountPath { return { coin, - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, addressNList: [0x80000000 + 84, 0x80000000 + slip44, 0x80000000 + accountIdx], }; } @@ -218,24 +139,6 @@ export function btcGetAccountPaths(msg: core.BTCGetAccountPaths): Array { - if (coin !== "Bitcoin") return Promise.resolve(false); - - switch (scriptType) { - case core.BTCInputScriptType.SpendAddress: - case core.BTCInputScriptType.SpendWitness: - case core.BTCInputScriptType.SpendP2SHWitness: - return true; - default: - return false; - } -} - -export async function btcSupportsCoin(coin: core.Coin): Promise { - if (coin === "Bitcoin") return true; - else return false; -} - export async function btcSignTx(msg: core.BTCSignTx, portis: any): Promise { const { result } = await portis.signBitcoinTransaction(msg); return { diff --git a/packages/hdwallet-portis/src/portis.ts b/packages/hdwallet-portis/src/portis.ts index 2b7f9c3aa..38a77324c 100644 --- a/packages/hdwallet-portis/src/portis.ts +++ b/packages/hdwallet-portis/src/portis.ts @@ -73,7 +73,7 @@ export class PortisHDWalletInfo implements core.HDWalletInfo, core.ETHWalletInfo return btc.btcSupportsCoin(coin); } - public async btcSupportsScriptType(coin: core.Coin, scriptType: core.BTCInputScriptType): Promise { + public async btcSupportsScriptType(coin: core.Coin, scriptType: core.BTCScriptType): Promise { return btc.btcSupportsScriptType(coin, scriptType); } @@ -312,7 +312,7 @@ export class PortisHDWallet implements core.HDWallet, core.ETHWallet, core.BTCWa return this.info.btcSupportsCoin(coin); } - public async btcSupportsScriptType(coin: core.Coin, scriptType: core.BTCInputScriptType): Promise { + public async btcSupportsScriptType(coin: core.Coin, scriptType: core.BTCScriptType): Promise { return this.info.btcSupportsScriptType(coin, scriptType); } diff --git a/packages/hdwallet-trezor/src/bitcoin.ts b/packages/hdwallet-trezor/src/bitcoin.ts index ebb204425..7742649c7 100644 --- a/packages/hdwallet-trezor/src/bitcoin.ts +++ b/packages/hdwallet-trezor/src/bitcoin.ts @@ -30,29 +30,29 @@ function translateCoin(coin: core.Coin): string { const segwitCoins = ["Bitcoin", "Litecoin", "BitcoinGold", "Testnet"]; -function translateInputScriptType(scriptType?: core.BTCInputScriptType): string { +function translateInputScriptType(scriptType?: core.BTCScriptType): string { switch (scriptType) { - case core.BTCInputScriptType.SpendAddress: + case core.BTCScriptType.Legacy: return "SPENDADDRESS"; - case core.BTCInputScriptType.SpendMultisig: + case core.BTCScriptType.LegacyMultisig: return "SPENDMULTISIG"; - case core.BTCInputScriptType.SpendWitness: + case core.BTCScriptType.SegwitNative: return "SPENDWITNESS"; - case core.BTCInputScriptType.SpendP2SHWitness: + case core.BTCScriptType.Segwit: return "SPENDP2SHWITNESS"; } throw new Error(`Un-handled enum entry: '${scriptType}'`); } -function translateOutputScriptType(scriptType?: core.BTCOutputScriptType): string { +function translateOutputScriptType(scriptType?: core.BTCScriptType): string { switch (scriptType) { - case core.BTCOutputScriptType.PayToAddress: + case core.BTCScriptType.Legacy: return "PAYTOADDRESS"; - case core.BTCOutputScriptType.PayToMultisig: + case core.BTCScriptType.LegacyMultisig: return "PAYTOMULTISIG"; - case core.BTCOutputScriptType.PayToWitness: + case core.BTCScriptType.SegwitNative: return "PAYTOWITNESS"; - case core.BTCOutputScriptType.PayToP2SHWitness: + case core.BTCScriptType.Segwit: return "PAYTOP2SHWITNESS"; } throw new Error(`Un-handled enum entry: '${scriptType}'`); @@ -62,10 +62,10 @@ export async function btcSupportsCoin(coin: core.Coin): Promise { return translateCoin(coin) !== undefined; } -export async function btcSupportsScriptType(coin: core.Coin, scriptType?: core.BTCInputScriptType): Promise { +export async function btcSupportsScriptType(coin: core.Coin, scriptType?: core.BTCScriptType): Promise { if (translateCoin(coin) === undefined) return false; - if (!segwitCoins.includes(coin) && scriptType === core.BTCInputScriptType.SpendP2SHWitness) return false; - if (!segwitCoins.includes(coin) && scriptType === core.BTCInputScriptType.SpendWitness) return false; + if (!segwitCoins.includes(coin) && scriptType === core.BTCScriptType.Segwit) return false; + if (!segwitCoins.includes(coin) && scriptType === core.BTCScriptType.SegwitNative) return false; return true; } @@ -120,7 +120,7 @@ export async function btcSignTx( return { address: output.address, amount: output.amount, - script_type: translateOutputScriptType(core.BTCOutputScriptType.PayToAddress), + script_type: translateOutputScriptType(core.BTCScriptType.Legacy), }; } @@ -199,17 +199,17 @@ export function btcGetAccountPaths(msg: core.BTCGetAccountPaths): Array>> 0 !== 0x80000000) return unknown; - - const purpose = path[0] & 0x7fffffff; - - if (![44, 49, 84].includes(purpose)) return unknown; - - if (purpose === 44 && scriptType !== core.BTCInputScriptType.SpendAddress) return unknown; - - if (purpose === 49 && scriptType !== core.BTCInputScriptType.SpendP2SHWitness) return unknown; - - if (purpose === 84 && scriptType !== core.BTCInputScriptType.SpendWitness) return unknown; - - const slip44 = core.slip44ByCoin(coin); - if (slip44 == undefined || path[1] !== 0x80000000 + slip44) return unknown; - - const wholeAccount = path.length === 3; - - let script = scriptType - ? ( - { - [core.BTCInputScriptType.SpendAddress]: " (Legacy)", - [core.BTCInputScriptType.SpendP2SHWitness]: "", - [core.BTCInputScriptType.SpendWitness]: " (Segwit Native)", - } as Partial> - )[scriptType] ?? "" - : ""; - - switch (coin) { - case "Bitcoin": - case "Litecoin": - case "BitcoinGold": - case "Testnet": - break; - default: - script = ""; - } - - const accountIdx = path[2] & 0x7fffffff; - - if (wholeAccount) { - return { - verbose: `${coin} Account #${accountIdx}${script}`, - scriptType, - coin, - accountIdx, - wholeAccount: true, - isKnown: true, - isPrefork: false, - }; - } else { - const change = path[3] === 1 ? "Change " : ""; - const addressIdx = path[4]; - return { - verbose: `${coin} Account #${accountIdx}, ${change}Address #${addressIdx}${script}`, - coin, - scriptType, - accountIdx, - addressIdx, - isChange: path[3] === 1, - wholeAccount: false, - isKnown: true, - isPrefork: false, - }; - } + return core.describeUTXOPath(path, coin, scriptType); } export class TrezorHDWalletInfo @@ -138,7 +67,7 @@ export class TrezorHDWalletInfo return Btc.btcSupportsCoin(coin); } - public async btcSupportsScriptType(coin: core.Coin, scriptType: core.BTCInputScriptType): Promise { + public async btcSupportsScriptType(coin: core.Coin, scriptType: core.BTCScriptType): Promise { return Btc.btcSupportsScriptType(coin, scriptType); } @@ -364,15 +293,15 @@ export class TrezorHDWallet implements core.HDWallet, core.BTCWallet, core.ETHWa return (res.payload as Array<{ xpubSegwit?: string; xpub?: string }>).map((result, i) => { const scriptType = msg[i].scriptType; switch (scriptType) { - case core.BTCInputScriptType.SpendP2SHWitness: - case core.BTCInputScriptType.SpendWitness: { + case core.BTCScriptType.Segwit: + case core.BTCScriptType.SegwitNative: { const xpub = result.xpubSegwit; if (!xpub) throw new Error("unable to get public key"); return { xpub, }; } - case core.BTCInputScriptType.SpendAddress: + case core.BTCScriptType.Legacy: default: { const xpub = result.xpub; if (!xpub) throw new Error("unable to get public key"); @@ -502,7 +431,7 @@ export class TrezorHDWallet implements core.HDWallet, core.BTCWallet, core.ETHWa return this.info.btcSupportsCoin(coin); } - public async btcSupportsScriptType(coin: core.Coin, scriptType: core.BTCInputScriptType): Promise { + public async btcSupportsScriptType(coin: core.Coin, scriptType: core.BTCScriptType): Promise { return this.info.btcSupportsScriptType(coin, scriptType); } diff --git a/packages/hdwallet-vultisig/src/bitcoin.ts b/packages/hdwallet-vultisig/src/bitcoin.ts index e44e90f77..e4bf3d0cc 100644 --- a/packages/hdwallet-vultisig/src/bitcoin.ts +++ b/packages/hdwallet-vultisig/src/bitcoin.ts @@ -1,6 +1,6 @@ import * as bitcoin from "@shapeshiftoss/bitcoinjs-lib"; import * as core from "@shapeshiftoss/hdwallet-core"; -import { BTCInputScriptType } from "@shapeshiftoss/hdwallet-core"; +import { BTCScriptType } from "@shapeshiftoss/hdwallet-core"; import { VultisigUtxoProvider } from "./types"; @@ -55,7 +55,7 @@ export const btcGetAccountPaths = (msg: core.BTCGetAccountPaths): Array { switch (input.scriptType) { - case BTCInputScriptType.SpendWitness: { + case BTCScriptType.SegwitNative: { psbt.addInput({ hash: input.txid, index: input.vout, @@ -84,6 +84,7 @@ async function addOutput( const outputAddress = await btcGetAddress(provider, { addressNList: output.addressNList, coin, + scriptType: output.scriptType, showDisplay: false, }); if (!outputAddress) throw new Error("Could not get address from wallet"); @@ -131,6 +132,7 @@ export async function bitcoinSignTx( const address = await btcGetAddress(provider, { addressNList: input.addressNList, coin: msg.coin, + scriptType: input.scriptType, showDisplay: false, }); diff --git a/packages/hdwallet-vultisig/src/vultisig.test.ts b/packages/hdwallet-vultisig/src/vultisig.test.ts index 9543637b5..b8a2aacb3 100644 --- a/packages/hdwallet-vultisig/src/vultisig.test.ts +++ b/packages/hdwallet-vultisig/src/vultisig.test.ts @@ -172,7 +172,7 @@ describe("VultisigHDWallet", () => { expect(paths).toHaveLength(1); expect(paths[0]).toEqual({ coin: "Bitcoin", - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, addressNList: [0x80000000 + 84, 0x80000000 + 0, 0x80000000 + 0], }); }); @@ -181,11 +181,11 @@ describe("VultisigHDWallet", () => { const paths = wallet.btcGetAccountPaths({ coin: "Bitcoin", accountIdx: 0, - scriptType: core.BTCInputScriptType.SpendWitness, + scriptType: core.BTCScriptType.SegwitNative, }); expect(paths).toHaveLength(1); - expect(paths[0].scriptType).toBe(core.BTCInputScriptType.SpendWitness); + expect(paths[0].scriptType).toBe(core.BTCScriptType.SegwitNative); }); it("should return empty array for unsupported coin", () => { @@ -200,19 +200,19 @@ describe("VultisigHDWallet", () => { describe("btcSupportsScriptType", () => { it("should support SpendWitness for Bitcoin", async () => { - const result = await wallet.btcSupportsScriptType("Bitcoin", core.BTCInputScriptType.SpendWitness); + const result = await wallet.btcSupportsScriptType("Bitcoin", core.BTCScriptType.SegwitNative); expect(result).toBe(true); }); it("should support SpendAddress for Bitcoin", async () => { - const result = await wallet.btcSupportsScriptType("Bitcoin", core.BTCInputScriptType.SpendAddress); + const result = await wallet.btcSupportsScriptType("Bitcoin", core.BTCScriptType.Legacy); expect(result).toBe(true); }); it("should NOT support SpendP2SHWitness for any coin", async () => { const coins = ["Bitcoin"]; for (const coin of coins) { - const result = await wallet.btcSupportsScriptType(coin, core.BTCInputScriptType.SpendP2SHWitness); + const result = await wallet.btcSupportsScriptType(coin, core.BTCScriptType.Segwit); expect(result).toBe(false); } }); @@ -226,10 +226,7 @@ describe("VultisigHDWallet", () => { }); it("should return false for unsupported coin", async () => { - const result = await wallet.btcSupportsScriptType( - "UnsupportedCoin" as any, - core.BTCInputScriptType.SpendWitness - ); + const result = await wallet.btcSupportsScriptType("UnsupportedCoin" as any, core.BTCScriptType.SegwitNative); expect(result).toBe(false); }); }); diff --git a/packages/hdwallet-vultisig/src/vultisig.ts b/packages/hdwallet-vultisig/src/vultisig.ts index 4937d7696..d7c958488 100644 --- a/packages/hdwallet-vultisig/src/vultisig.ts +++ b/packages/hdwallet-vultisig/src/vultisig.ts @@ -132,14 +132,14 @@ export class VultisigHDWalletInfo return vultisigSupportedCoins.includes(coin.toLowerCase()); } - public async btcSupportsScriptType(coin: string, scriptType?: core.BTCInputScriptType | undefined): Promise { + public async btcSupportsScriptType(coin: string, scriptType?: core.BTCScriptType | undefined): Promise { if (!this.btcSupportsCoin(coin)) return false; const c = coin.toLowerCase(); switch (scriptType) { - case core.BTCInputScriptType.SpendAddress: + case core.BTCScriptType.Legacy: return ["bitcoin"].includes(c); - case core.BTCInputScriptType.SpendWitness: + case core.BTCScriptType.SegwitNative: return ["bitcoin"].includes(c); default: return false;