Skip to content

feat: 🎸 add local signing key endpoint #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ services:
start_period: 10s

subquery:
platform: 'linux/amd64'
image: '${SUBQUERY_IMAGE}'
init: true
restart: unless-stopped
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/metadata.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export class MetadataController {
@ApiParam({
name: 'type',
description: 'The type of Asset Metadata',
enum: MetadataType.Local,
enum: MetadataType,
example: MetadataType.Local,
})
@ApiParam({
Expand Down
20 changes: 20 additions & 0 deletions src/signing/dto/add-local-signer.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';

export class AddLocalSignerDto {
@ApiProperty({
description: 'The value used to reference the key when signing',
example: 'alice',
})
@IsString()
@IsNotEmpty()
handle: string;

@ApiProperty({
description: 'The 12 word mnemonic for the signer',
example: 'clap reveal pledge miss useful motion pair goat book snow scrub bag',
})
@IsString()
@IsNotEmpty()
mnemonic: string;
}
23 changes: 22 additions & 1 deletion src/signing/services/local-signing.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LocalSigningManager } from '@polymeshassociation/local-signing-manager'
import { PolkadotSigner } from '@polymeshassociation/signing-manager-types';
import { when } from 'jest-when';

import { AppNotFoundError } from '~/common/errors';
import { AppConflictError, AppNotFoundError } from '~/common/errors';
import { mockPolymeshLoggerProvider } from '~/logger/mock-polymesh-logger';
import { PolymeshLogger } from '~/logger/polymesh-logger.service';
import { POLYMESH_API } from '~/polymesh/polymesh.consts';
Expand Down Expand Up @@ -102,4 +102,25 @@ describe('LocalSigningService', () => {
expect(result).toEqual(signature);
});
});

describe('addSigner', () => {
it('should add a new signer and return its address', async () => {
const handle = 'newSigner';
const mnemonic = '//Alice';
const expectedAddress = '15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5';

const result = await service.addSigner(handle, mnemonic);

expect(result).toBe(expectedAddress);
});

it('should throw AppConflictError when adding a signer with existing handle', async () => {
const handle = 'existingSigner';
const mnemonic = '//Alice';

await service.addSigner(handle, mnemonic);

await expect(service.addSigner(handle, mnemonic)).rejects.toThrow(AppConflictError);
});
});
});
14 changes: 14 additions & 0 deletions src/signing/services/local-signing.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LocalSigningManager } from '@polymeshassociation/local-signing-manager';
import { forEach } from 'lodash';

import { AppConflictError } from '~/common/errors';
import { PolymeshLogger } from '~/logger/polymesh-logger.service';
import { PolymeshService } from '~/polymesh/polymesh.service';
import { SigningService } from '~/signing/services/signing.service';
Expand Down Expand Up @@ -44,4 +45,17 @@ export class LocalSigningService extends SigningService {
private logKey(handle: string, address: string): void {
this.logger.log(`Key "${handle}" with address "${address}" was loaded`);
}

public async addSigner(handle: string, mnemonic: string): Promise<string> {
const existingAddress = this.addressBook[handle];
if (existingAddress) {
throw new AppConflictError(existingAddress, handle);
}

const address = this.signingManager.addAccount({ mnemonic });
this.setAddressByHandle(handle, address);
this.logKey(handle, address);

return address;
}
}
7 changes: 6 additions & 1 deletion src/signing/services/signing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
import { TransactionPayload } from '@polymeshassociation/polymesh-sdk/types';
import { SigningManager } from '@polymeshassociation/signing-manager-types';

import { AppNotFoundError } from '~/common/errors';
import { AppInternalError, AppNotFoundError } from '~/common/errors';
import { PolymeshService } from '~/polymesh/polymesh.service';

@Injectable()
Expand All @@ -29,4 +29,9 @@ export abstract class SigningService {
protected throwNoSigner(handle: string): never {
throw new AppNotFoundError(handle, 'signer');
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public async addSigner(handle: string, mnemonic: string): Promise<string> {
throw new AppInternalError('Adding signers is not supported with the configured service');
}
}
8 changes: 7 additions & 1 deletion src/signing/services/vault-signing.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { HashicorpVaultSigningManager } from '@polymeshassociation/hashicorp-vault-signing-manager';

import { AppNotFoundError } from '~/common/errors';
import { AppInternalError, AppNotFoundError } from '~/common/errors';
import { LoggerModule } from '~/logger/logger.module';
import { mockPolymeshLoggerProvider } from '~/logger/mock-polymesh-logger';
import { PolymeshLogger } from '~/logger/polymesh-logger.service';
Expand Down Expand Up @@ -87,4 +87,10 @@ describe('VaultSigningService', () => {
return expect(service.getAddressByHandle('badId')).rejects.toBeInstanceOf(AppNotFoundError);
});
});

describe('add signer', () => {
it('should throw a not implemented error', () => {
return expect(service.addSigner('bad', 'apple')).rejects.toThrow(AppInternalError);
});
});
});
11 changes: 11 additions & 0 deletions src/signing/signing.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,15 @@ describe('SigningController', () => {
return expect(controller.getSignerAddress({ signer })).resolves.toEqual(expectedResult);
});
});

describe('addSigner', () => {
it('should call the service and return the result', () => {
const handle = 'test-handle';
const mnemonic = 'test mnemonic phrase';
const expectedResult = new SignerModel({ address });

when(signingService.addSigner).calledWith(handle, mnemonic).mockResolvedValue(address);
return expect(controller.addSigner({ handle, mnemonic })).resolves.toEqual(expectedResult);
});
});
});
22 changes: 21 additions & 1 deletion src/signing/signing.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Controller, Get, Param } from '@nestjs/common';
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import {
ApiBadRequestResponse,
ApiCreatedResponse,
ApiNotFoundResponse,
ApiOkResponse,
ApiOperation,
ApiParam,
ApiTags,
} from '@nestjs/swagger';

import { AddLocalSignerDto } from '~/signing/dto/add-local-signer.dto';
import { SignerDetailsDto } from '~/signing/dto/signer-details.dto';
import { SignerModel } from '~/signing/models/signer.model';
import { SigningService } from '~/signing/services';
Expand Down Expand Up @@ -44,4 +46,22 @@ export class SigningController {

return new SignerModel({ address });
}

@ApiOperation({
summary: 'Add a new signer',
description: 'Adds a new key to the signing manager',
})
@ApiCreatedResponse({
description: 'The signer was successfully added',
type: SignerModel,
})
@ApiBadRequestResponse({
description: 'Invalid mnemonic or handle provided',
})
@Post()
public async addSigner(@Body() { handle, mnemonic }: AddLocalSignerDto): Promise<SignerModel> {
const address = await this.signingService.addSigner(handle, mnemonic);

return new SignerModel({ address });
}
}
Loading