Skip to content

Commit

Permalink
👌 IMPROVE: Add support to use to same path when importing account
Browse files Browse the repository at this point in the history
  • Loading branch information
riverrun46 committed Sep 28, 2023
1 parent 84b2fd8 commit d63ab39
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 93 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion src/lib/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ export async function addAccount(newAccount: Omit<Account, 'id' | 'name'>) {

async function getAccountProperty(chain: Chain, key: keyof ChainDetail[Chain]): Promise<string> {
const account = await getCurrentAccount()
console.log('current account', account)

if (!account) {
return ''
Expand Down
29 changes: 1 addition & 28 deletions src/lib/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BN, mvc } from 'meta-contract'
import { Buffer } from 'buffer'

import { getMvcRootPath, type Account } from './account'
import { parseLocalTransaction } from './metadata'

Expand Down Expand Up @@ -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,
}
Expand Down
157 changes: 111 additions & 46 deletions src/pages/wallet/Import.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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('')
Expand All @@ -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,
Expand All @@ -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,
},
Expand Down Expand Up @@ -126,10 +133,12 @@ const onSubmit = async () => {
<RadioGroup v-model="selectedWordsLength">
<div class="flex items-center gap-x-2">
<RadioGroupOption v-slot="{ checked }" :value="length" v-for="length of wordsLengths" class="rounded">
<div :class="[
checked ? 'bg-blue-100 text-blue-500' : 'text-gray-500',
'w-full cursor-pointer rounded-inherit px-2 py-0.5 text-center text-xs',
]">
<div
:class="[
checked ? 'bg-blue-100 text-blue-500' : 'text-gray-500',
'w-full cursor-pointer rounded-inherit px-2 py-0.5 text-center text-xs',
]"
>
{{ `${length} words` }}
</div>
</RadioGroupOption>
Expand All @@ -139,8 +148,15 @@ const onSubmit = async () => {

<div class="grid grid-cols-3 gap-2">
<!-- input框 绑定粘贴事件 -->
<input v-for="(word, index) in words" :key="index" type="text" class="pit-input gradient-text font-bold"
:placeholder="(index + 1).toString()" v-model="words[index]" @paste.prevent="onPasteWords" />
<input
v-for="(word, index) in words"
:key="index"
type="text"
class="pit-input gradient-text font-bold"
:placeholder="(index + 1).toString()"
v-model="words[index]"
@paste.prevent="onPasteWords"
/>
</div>
</div>

Expand All @@ -149,12 +165,17 @@ const onSubmit = async () => {
<Disclosure v-slot="{ open }">
<DisclosureButton class="flex items-center gap-1">
<span class="text-xs text-gray-500">MVC Path</span>
<!-- <ChevronRightIcon :class="['h-4 w-4 text-gray-400 transition duration-200', open && 'rotate-90 transform']" /> -->
<ChevronRightIcon :class="['h-4 w-4 text-gray-400 transition duration-200', open && 'rotate-90 transform']" />
</DisclosureButton>

<transition enter-active-class="transition duration-100 ease-out" enter-from-class="transform scale-95 opacity-0"
enter-to-class="transform scale-100 opacity-100" leave-active-class="transition duration-75 ease-out"
leave-from-class="transform scale-100 opacity-100" leave-to-class="transform scale-95 opacity-0">
<transition
enter-active-class="transition duration-100 ease-out"
enter-from-class="transform scale-95 opacity-0"
enter-to-class="transform scale-100 opacity-100"
leave-active-class="transition duration-75 ease-out"
leave-from-class="transform scale-100 opacity-100"
leave-to-class="transform scale-95 opacity-0"
>
<DisclosurePanel class="mt-1 space-y-2 rounded-lg bg-gray-100 p-4 text-sm text-gray-500 shadow-inner">
<h3 class="text-sm font-bold text-gray-900">What is a derivation path?</h3>
<p class="">
Expand All @@ -172,41 +193,85 @@ const onSubmit = async () => {
</div>
</div>

<span class="text-xs text-gray-500">BTC Path</span>

<Listbox v-model="selectedScript">
<div class="relative mt-1">
<ListboxButton
class="relative w-full cursor-default rounded-lg bg-[#f5f5f5] py-2 pl-3 pr-10 text-left shadow-md focus:outline-none focus-visible:border-indigo-500 focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-opacity-75 focus-visible:ring-offset-2 focus-visible:ring-offset-orange-300 sm:text-sm">
<span class="block truncate">{{ selectedScript.path }}</span>
<span class="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2">
<ChevronUpDownIcon class="h-5 w-5 text-gray-400" aria-hidden="true" />
</span>
</ListboxButton>

<transition leave-active-class="transition duration-100 ease-in" leave-from-class="opacity-100"
leave-to-class="opacity-0">
<ListboxOptions
class="absolute mt-1 max-h-60 w-full overflow-auto rounded-md bg-[#f5f5f5] py-2 text-base shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm">
<ListboxOption v-slot="{ selected }" v-for="script in scripts" :key="script.name" :value="script"
as="template">
<li :class="['text-gray-900', 'relative cursor-pointer select-none py-1 pl-3 pr-4']">
<span :class="[selected ? 'font-medium' : 'font-normal', 'block truncate']">{{ script.path }}</span>
<span v-if="selected"
class="absolute inset-y-2 right-2 flex h-5 w-5 items-center justify-center rounded-md bg-[#1E2BFF] text-white">
<CheckIcon class="h-4 w-4" aria-hidden="true" />
</span>
</li>
</ListboxOption>
</ListboxOptions>
</transition>
</div>
</Listbox>
<div class="mt-8">
<span class="text-xs text-gray-500">BTC Path</span>

<SwitchGroup as="div" class="flex items-center mt-2">
<Switch
v-model="useSamePath"
class="group relative inline-flex h-5 w-10 flex-shrink-0 cursor-pointer items-center justify-center rounded-full"
>
<span class="sr-only">Use same path as MVC</span>
<span aria-hidden="true" class="pointer-events-none absolute h-full w-full rounded-md bg-white"></span>
<span
aria-hidden="true"
:class="[
useSamePath ? 'bg-btn-blue' : 'bg-gray-200',
'pointer-events-none absolute mx-auto h-4 w-9 rounded-full transition-colors duration-200 ease-in-out',
]"
></span>
<span
aria-hidden="true"
:class="[
useSamePath ? 'translate-x-5' : 'translate-x-0',
'pointer-events-none absolute left-0 inline-block h-5 w-5 transform rounded-full border border-gray-200 bg-white shadow ring-0 transition-transform duration-200 ease-in-out',
]"
></span>
</Switch>

<SwitchLabel as="span" class="ml-3 text-sm text-gray-500"> Use the same path as MVC </SwitchLabel>
</SwitchGroup>

<Listbox v-model="selectedScript" v-if="!useSamePath">
<div class="relative mt-4">
<ListboxButton
class="relative w-full cursor-default rounded-lg bg-[#f5f5f5] py-2 pl-3 pr-10 text-left shadow-md focus:outline-none focus-visible:border-indigo-500 focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-opacity-75 focus-visible:ring-offset-2 focus-visible:ring-offset-orange-300 sm:text-sm"
>
<span class="block truncate">{{ selectedScript.path }}</span>
<span class="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2">
<ChevronUpDownIcon class="h-5 w-5 text-gray-400" aria-hidden="true" />
</span>
</ListboxButton>

<transition
leave-active-class="transition duration-100 ease-in"
leave-from-class="opacity-100"
leave-to-class="opacity-0"
>
<ListboxOptions
class="absolute mt-1 max-h-60 w-full overflow-auto rounded-md bg-[#f5f5f5] py-2 text-base shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm"
>
<ListboxOption
v-slot="{ selected }"
v-for="script in selectableScripts"
:key="script.name"
:value="script"
as="template"
>
<li :class="['text-gray-900', 'relative cursor-pointer select-none py-1 pl-3 pr-4']">
<span :class="[selected ? 'font-medium' : 'font-normal', 'block truncate']">{{ script.path }}</span>
<span
v-if="selected"
class="absolute inset-y-2 right-2 flex h-5 w-5 items-center justify-center rounded-md bg-[#1E2BFF] text-white"
>
<CheckIcon class="h-4 w-4" aria-hidden="true" />
</span>
</li>
</ListboxOption>
</ListboxOptions>
</transition>
</div>
</Listbox>
</div>

<!-- ok -->
<div class="mt-32 flex items-center justify-center">
<button class="main-btn-bg mt-8 grow rounded-md py-3 text-sm font-bold text-sky-50" :class="[!finished && 'muted']"
:disabled="!finished" @click="onSubmit">
<button
class="main-btn-bg mt-8 grow rounded-md py-3 text-sm font-bold text-sky-50"
:class="[!finished && 'muted']"
:disabled="!finished"
@click="onSubmit"
>
OK
</button>
</div>
Expand Down
18 changes: 2 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==

[email protected]:
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"
Expand Down Expand Up @@ -2645,20 +2645,6 @@ mvc-lib@^1.0.5:
inherits "2.0.3"
unorm "1.4.1"

[email protected]:
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"
Expand Down

0 comments on commit d63ab39

Please sign in to comment.