Skip to content

Commit

Permalink
feat(local-signing-manager): add derivationPath for account (#33)
Browse files Browse the repository at this point in the history
* feat(local-signing-manager): add `derivationPath` for account
  • Loading branch information
MehdiRaash authored Feb 6, 2023
1 parent e9387a9 commit af131b6
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,74 @@ describe('LocalSigningManager Class', () => {
"Cannot call 'addAccount' before calling 'setSs58Format'. Did you forget to use this Signing Manager to connect with the Polymesh SDK?"
);
});

it('should add a new Account(by seed) with a derivation path', async () => {
signingManager = await LocalSigningManager.create({
accounts: [
{
seed: '0xb5da7610352f87452fe5fa4d9af35a3fbb613e7afee2c72056333db0b94d6f98',
derivationPath: '//test',
},
],
});

signingManager.setSs58Format(42);

expect((await signingManager.getAccounts())[0]).toBe(
'5FQwqu3rSRgr48cKxvU9zhumx2iAbtQp8NvRA99j8DKgdFua'
);
});

it('should add a new Account(by uri) with a derivation path', async () => {
signingManager = await LocalSigningManager.create({
accounts: [
{
uri: '//Alice',
derivationPath: '//test',
},
],
});

signingManager.setSs58Format(42);

expect((await signingManager.getAccounts())[0]).toBe(
'5DfkbGYYVW5DHGrZtiAWVF6umYUTpYMPuhXhcAMor8nPGmJG'
);
});

it('should add a new Account(by mnemonic) with a derivation path', async () => {
signingManager = await LocalSigningManager.create({
accounts: [
{
mnemonic:
'bachelor nurse busy spot cannon equal drip outer autumn fork virtual thunder',
derivationPath: '//test',
},
],
});

signingManager.setSs58Format(42);

expect((await signingManager.getAccounts())[0]).toBe(
'5FbhYPC4ShEdEpKjv37ZvVpJ68nuCTNbRShWmg8iSVCEpwmr'
);
});

it('should throw an error if the soft path was provided on `ed25519` curve type', async () => {
expect(
async () =>
await LocalSigningManager.create({
accounts: [
{
mnemonic:
'bachelor nurse busy spot cannon equal drip outer autumn fork virtual thunder',
derivationPath: '//hard/soft',
},
],
type: 'ed25519',
})
).rejects.toThrow('A soft key was found in the path and is not supported');
});
});

describe('method: generateAccount', () => {
Expand Down
22 changes: 19 additions & 3 deletions packages/local-signing-manager/src/lib/local-signing-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,27 @@ export class LocalSigningManager implements SigningManager {
let address: string;

if ('uri' in account) {
({ address } = keyring.addFromUri(account.uri));
const accountUri = account.derivationPath
? `${account.uri}${account.derivationPath}`
: account.uri;
address = keyring.addFromUri(accountUri).address;
} else if ('mnemonic' in account) {
({ address } = keyring.addFromMnemonic(account.mnemonic));
const accountMnemonic = account.derivationPath
? `${account.mnemonic}${account.derivationPath}`
: account.mnemonic;
address = keyring.addFromUri(accountMnemonic).address;
} else {
({ address } = keyring.addFromSeed(hexToU8a(account.seed)));
const seedInU8a = hexToU8a(account.seed);

if (account.derivationPath) {
address = keyring.addPair(
new Keyring({ type: this.keyring.type })
.addFromSeed(seedInU8a)
.derive(account.derivationPath)
).address;
} else {
address = keyring.addFromSeed(seedInU8a).address;
}
}

return address;
Expand Down
18 changes: 8 additions & 10 deletions packages/local-signing-manager/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/**
* URI|mnemonic|hex representation of a private key
*/
export type PrivateKey =
| {
uri: string;
}
| {
mnemonic: string;
}
| {
seed: string;
};
export type PrivateKey = ({ uri: string } | { mnemonic: string } | { seed: string }) & {
/**
* (optional) a specify a derivation path to apply to the secret. This allows for multiple accounts to be created with only one secret
*
* @note if using this option the account can only be recreated by knowing the path and the secret. Be sure to use a consistent scheme when making the path
*/
derivationPath?: string;
};

/**
* Supported key types. Generally `sr25519` is preferred, but `ed25519` is more widely supported
Expand Down

0 comments on commit af131b6

Please sign in to comment.