From d63ab3907f4363d69acb80fb9552a3ed63e27681 Mon Sep 17 00:00:00 2001 From: Vincent Tse Date: Thu, 28 Sep 2023 17:50:07 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20Add=20support=20to=20?= =?UTF-8?q?use=20to=20same=20path=20when=20importing=20account?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 +- src/lib/account.ts | 1 - src/lib/crypto.ts | 29 +------ src/pages/wallet/Import.vue | 157 +++++++++++++++++++++++++----------- yarn.lock | 18 +---- 5 files changed, 115 insertions(+), 93 deletions(-) diff --git a/package.json b/package.json index 658708c..7ebaaff 100644 --- a/package.json +++ b/package.json @@ -41,8 +41,7 @@ "decimal.js": "^10.4.3", "ecpair": "^2.1.0", "memfs": "^4.5.0", - "meta-contract": "0.3.29", - "mvc-scrypt": "0.1.1", + "meta-contract": "^0.3.29", "mvc-std-lib": "^1.1.4", "object-hash": "^3.0.0", "qrcode": "^1.5.3", diff --git a/src/lib/account.ts b/src/lib/account.ts index f860227..e3594cb 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -177,7 +177,6 @@ export async function addAccount(newAccount: Omit) { async function getAccountProperty(chain: Chain, key: keyof ChainDetail[Chain]): Promise { const account = await getCurrentAccount() - console.log('current account', account) if (!account) { return '' diff --git a/src/lib/crypto.ts b/src/lib/crypto.ts index 02d7601..a710c76 100644 --- a/src/lib/crypto.ts +++ b/src/lib/crypto.ts @@ -1,5 +1,6 @@ import { BN, mvc } from 'meta-contract' import { Buffer } from 'buffer' + import { getMvcRootPath, type Account } from './account' import { parseLocalTransaction } from './metadata' @@ -116,39 +117,11 @@ export const signTransaction = async ( } } - const Interp = mvc.Script.Interpreter - const flags = - Interp.SCRIPT_ENABLE_MAGNETIC_OPCODES | - Interp.SCRIPT_ENABLE_MONOLITH_OPCODES | - Interp.SCRIPT_VERIFY_STRICTENC | - Interp.SCRIPT_ENABLE_SIGHASH_FORKID | - Interp.SCRIPT_VERIFY_LOW_S | - Interp.SCRIPT_VERIFY_NULLFAIL | - Interp.SCRIPT_VERIFY_DERSIG | - Interp.SCRIPT_VERIFY_MINIMALDATA | - Interp.SCRIPT_VERIFY_NULLDUMMY | - Interp.SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS | - Interp.SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY | - Interp.SCRIPT_VERIFY_CHECKSEQUENCEVERIFY | - // @ts-ignore - Interp.SCRIPT_VERIFY_CLEANSTACK - - const script = mvc.Script.fromBuffer(Buffer.from(scriptHex, 'hex')) - // @ts-ignore - const sig2 = mvc.Transaction.sighash - .sign(tx, privateKey, sigtype, inputIndex, script, new BN(satoshis), flags) - .toTxFormat() - .toString('hex') - - // const sig3 = toHex(signTx(tx, privateKey, script, Number(satoshis), inputIndex)) - return { publicKey: publicKey.toString(), r: sig.r.toString('hex'), s: sig.s.toString('hex'), sig: sig.set({ nhashtype: sigtype }).toTxFormat().toString('hex'), - sig2, - // sig3, sigtype, txid: tx.id, } diff --git a/src/pages/wallet/Import.vue b/src/pages/wallet/Import.vue index c46114f..3f5892e 100644 --- a/src/pages/wallet/Import.vue +++ b/src/pages/wallet/Import.vue @@ -11,14 +11,19 @@ import { ListboxButton, ListboxOptions, ListboxOption, + Switch, + SwitchGroup, + SwitchLabel, } from '@headlessui/vue' -import { TrashIcon, CheckIcon, ChevronUpDownIcon } from '@heroicons/vue/24/solid' +import { TrashIcon, CheckIcon, ChevronUpDownIcon, ChevronRightIcon } from '@heroicons/vue/24/solid' import MetaletLogoImg from '@/assets/images/metalet-logo.png?url' import { addAccount } from '@/lib/account' import { deriveAllAddresses, scripts, type AddressType } from '@/lib/bip32-deriver' -const selectedScript = ref(scripts[3]) +// Remove the last one +const selectableScripts = scripts.slice(0, -1) +const selectedScript = ref(selectableScripts[0]) const router = useRouter() @@ -51,6 +56,8 @@ const onPasteWords = (e: ClipboardEvent) => { const pathDepth = ref('10001') +const useSamePath = ref(true) + const finished = computed(() => words.value.every((word) => word.length > 0)) const error = ref('') @@ -62,8 +69,8 @@ const onSubmit = async () => { // 转化成私钥 try { - const btcPath = selectedScript.value.path const fullPath = `m/44'/${pathDepth.value}'/0'/0/0` + const btcPath = useSamePath.value ? fullPath : selectedScript.value.path const allAddresses = deriveAllAddresses({ mnemonic: mnemonicStr, @@ -83,7 +90,7 @@ const onSubmit = async () => { }, btc: { path: btcPath, - addressType: selectedScript.value.addressType, + addressType: useSamePath.value ? 'P2PKH' : selectedScript.value.addressType, mainnetAddress: allAddresses.btcMainnetAddress, testnetAddress: allAddresses.btcTestnetAddress, }, @@ -126,10 +133,12 @@ const onSubmit = async () => {
-
+
{{ `${length} words` }}
@@ -139,8 +148,15 @@ const onSubmit = async () => {
- +
@@ -149,12 +165,17 @@ const onSubmit = async () => { MVC Path - + - +

What is a derivation path?

@@ -172,41 +193,85 @@ const onSubmit = async () => {

- BTC Path - - -
- - {{ selectedScript.path }} - - - - - - - -
  • - {{ script.path }} - - -
  • -
    -
    -
    -
    -
    +
    + BTC Path + + + + Use same path as MVC + + + + + + Use the same path as MVC + + + +
    + + {{ selectedScript.path }} + + + + + + + +
  • + {{ script.path }} + + +
  • +
    +
    +
    +
    +
    +
    -
    diff --git a/yarn.lock b/yarn.lock index b625177..4736d76 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2536,9 +2536,9 @@ merge2@^1.3.0: resolved "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz" integrity sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg== -meta-contract@0.3.29: +meta-contract@^0.3.29: version "0.3.29" - resolved "https://registry.npmjs.org/meta-contract/-/meta-contract-0.3.29.tgz" + resolved "https://registry.yarnpkg.com/meta-contract/-/meta-contract-0.3.29.tgz#c5132674c3623658a0bfbbcac483b1e02f9274e8" integrity sha512-8WrJSaCoHxvGl2IVuEw3lWBXjvdWxhK6Y4pVD57F5G4Xie6t24BDDGFTXJe77hyOoz/e9OzPK2UzfDdMklrzSQ== dependencies: mvc-scrypt "^0.1.2" @@ -2645,20 +2645,6 @@ mvc-lib@^1.0.5: inherits "2.0.3" unorm "1.4.1" -mvc-scrypt@0.1.1: - version "0.1.1" - resolved "https://registry.yarnpkg.com/mvc-scrypt/-/mvc-scrypt-0.1.1.tgz#841a8179be9cc6bdb0b60200ba5dbb716e5eb53f" - integrity sha512-csJ5QVontTNHTRKR/Mmh9r0JMg6vBFoEFa99Ey5D/9BwhoxFQ73/jtvzEC6WcG9xsWoLwisVh65WXN418sKsYw== - dependencies: - "@discoveryjs/json-ext" "^0.5.7" - compare-versions "^3.6.0" - json-bigint "^1.0.0" - md5 "^2.2.1" - mvc-lib "^1.0.5" - node-fetch "^3.0.0" - patch-package "^6.4.7" - sourcemap-codec "^1.4.8" - mvc-scrypt@^0.1.2: version "0.1.6" resolved "https://registry.yarnpkg.com/mvc-scrypt/-/mvc-scrypt-0.1.6.tgz#b64041b017900705220dff6db3f8d7510d318e19"