Skip to content

Commit

Permalink
[Issue-3925] feat: support cardano derive
Browse files Browse the repository at this point in the history
  • Loading branch information
bluezdot committed Dec 24, 2024
1 parent 6310ef3 commit 3ba31ae
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { assert } from '@polkadot/util';

import { AccountBaseHandler } from './Base';

const validDeriveKeypairTypes: KeypairType[] = [...SubstrateKeypairTypes, ...EthereumKeypairTypes, 'ton'];
const validDeriveKeypairTypes: KeypairType[] = [...SubstrateKeypairTypes, ...EthereumKeypairTypes, 'ton', 'cardano'];

/**
* @class AccountDeriveHandler
Expand Down
19 changes: 14 additions & 5 deletions packages/extension-base/src/utils/account/derive/info/solo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { t } from 'i18next';

import { assert } from '@polkadot/util';

import { validateEvmDerivationPath, validateOtherSubstrateDerivationPath, validateSr25519DerivationPath, validateTonDerivationPath, validateUnifiedDerivationPath } from '../validate';
import { validateCardanoDerivationPath, validateEvmDerivationPath, validateOtherSubstrateDerivationPath, validateSr25519DerivationPath, validateTonDerivationPath, validateUnifiedDerivationPath } from '../validate';

export const parseUnifiedSuriToDerivationPath = (suri: string, type: KeypairType): string => {
const reg = /^\/\/(\d+)(\/\/\d+)?$/;
Expand All @@ -25,12 +25,16 @@ export const parseUnifiedSuriToDerivationPath = (suri: string, type: KeypairType
return `m/44'/60'/0'/0/${first}/${secondIndex}`;
} else if (type === 'ton') {
return `m/44'/607'/${first}'/${secondIndex}'`;
} else if (type === 'cardano') {
return `m/1852'/1815'/${first}'/${secondIndex}'`;
}
} else {
if (type === 'ethereum') {
return `m/44'/60'/0'/0/${first}`;
} else if (type === 'ton') {
return `m/44'/607'/${first}'`;
} else if (type === 'cardano') {
return `m/1852'/1815'/${first}'`;
}
}

Expand All @@ -51,7 +55,9 @@ export const getSoloDerivationInfo = (type: KeypairType, metadata: AccountDerive
? validateEvmDerivationPath
: type === 'ton'
? validateTonDerivationPath
: () => undefined; // todo: add derive cardano
: type === 'cardano'
? validateCardanoDerivationPath
: () => undefined; // todo: add derive cardano
const validateTypeRs = validateTypeFunc(derivePath);

if (validateTypeRs) {
Expand Down Expand Up @@ -114,7 +120,9 @@ export const getSoloDerivationInfo = (type: KeypairType, metadata: AccountDerive
? validateEvmDerivationPath
: type === 'ton'
? validateTonDerivationPath
: () => undefined;
: type === 'cardano'
? validateCardanoDerivationPath
: () => undefined;
const validateTypeRs = validateTypeFunc(derivePath);

if (validateTypeRs) {
Expand Down Expand Up @@ -242,6 +250,7 @@ export const derivePair = (parentPair: KeyringPair, name: string, suri: string,

const isEvm = EthereumKeypairTypes.includes(parentPair.type);
const isTon = parentPair.type === 'ton';
const isCardano = parentPair.type === 'cardano';

const meta = {
name,
Expand All @@ -255,8 +264,8 @@ export const derivePair = (parentPair: KeyringPair, name: string, suri: string,
meta.tonContractVersion = parentPair.ton.contractVersion;
}

if (derivationPath && (isEvm || isTon)) {
return isEvm ? parentPair.evm.deriveCustom(derivationPath, meta) : parentPair.ton.deriveCustom(derivationPath, meta);
if (derivationPath && (isEvm || isTon || isCardano)) {
return isEvm ? parentPair.evm.deriveCustom(derivationPath, meta) : isTon ? parentPair.ton.deriveCustom(derivationPath, meta) : parentPair.cardano.deriveCustom(derivationPath, meta);
} else {
return parentPair.substrate.derive(suri, meta);
}
Expand Down
44 changes: 43 additions & 1 deletion packages/extension-base/src/utils/account/derive/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,46 @@ export const validateTonDerivationPath = (raw: string): IDerivePathInfo_ | undef
}
};

export const validateCardanoDerivationPath = (raw: string): IDerivePathInfo_ | undefined => {
const reg = /^m\/1852'\/1815'\/(\d+)'(\/\d+')?$/; // todo: recheck derive path

if (raw.match(reg)) {
const [, firstIndex, secondData] = raw.match(reg) as string[];

const first = parseInt(firstIndex, 10);
const autoIndexes: number[] = [first];

let depth: number;
let suri = `//${first}`;

if (first === 0) {
depth = 0;
} else {
depth = 1;
}

if (secondData) {
const [, secondIndex] = secondData.match(/\/(\d+)/) as string[];

const second = parseInt(secondIndex, 10);

autoIndexes.push(second);
depth = 2;
suri += `//${second}`;
}

return {
depth,
type: 'cardano',
suri,
derivationPath: raw,
autoIndexes
};
} else {
return undefined;
}
};

export const validateSr25519DerivationPath = (raw: string): IDerivePathInfo_ | undefined => {
const reg = /\/(\/?)([^/]+)/g;
const parts = raw.match(reg);
Expand Down Expand Up @@ -197,10 +237,12 @@ export const validateDerivationPath = (raw: string, type?: KeypairType): DeriveP
return validateSr25519DerivationPath(raw);
} else if (type === 'ed25519' || type === 'ecdsa') {
return validateOtherSubstrateDerivationPath(raw, type);
} else if (type === 'cardano') {
return validateCardanoDerivationPath(raw);
} else {
return undefined;
}
} else {
return validateUnifiedDerivationPath(raw) || validateEvmDerivationPath(raw) || validateTonDerivationPath(raw) || validateSr25519DerivationPath(raw);
return validateUnifiedDerivationPath(raw) || validateEvmDerivationPath(raw) || validateTonDerivationPath(raw) || validateSr25519DerivationPath(raw) || validateCardanoDerivationPath(raw);
}
};

0 comments on commit 3ba31ae

Please sign in to comment.