Skip to content
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

feat: add bls validation in mainsail #68

Merged
merged 13 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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: 1 addition & 0 deletions packages/mainsail/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@mainsail/crypto-config": "0.0.1-alpha.15",
"@mainsail/crypto-consensus-bls12-381": "0.0.1-alpha.15",
"@mainsail/crypto-hash-bcrypto": "0.0.1-alpha.15",
"@mainsail/crypto-key-pair-bls12-381": "0.0.1-alpha.15",
"@mainsail/crypto-key-pair-ecdsa": "0.0.1-alpha.15",
"@mainsail/crypto-signature-schnorr-secp256k1": "0.0.1-alpha.15",
"@mainsail/crypto-transaction": "0.0.1-alpha.15",
Expand Down
77 changes: 77 additions & 0 deletions packages/mainsail/source/public-key.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,70 @@ import { abort_if, abort_unless } from "@ardenthq/sdk-helpers";
import { BindingType } from "./coin.contract.js";
import { PublicKey as BasePublicKey } from "./crypto/identities/public-key.js";
import { Interfaces } from "./crypto/index.js";
import { Container } from "@mainsail/container";
import { Application } from "@mainsail/kernel";
import { Contracts } from "@mainsail/contracts";
import { PublicKeyFactory, KeyPairFactory } from "@mainsail/crypto-key-pair-bls12-381";
import { ServiceProvider as CoreValidation } from "@mainsail/validation";
import { ServiceProvider as CoreCryptoAddressBase58 } from "@mainsail/crypto-address-base58";
import { ServiceProvider as CoreCryptoConfig } from "@mainsail/crypto-config";
import { ServiceProvider as CoreCryptoConsensusBls12381 } from "@mainsail/crypto-consensus-bls12-381";
import { ServiceProvider as CoreCryptoValidation } from "@mainsail/crypto-validation";
import { ServiceProvider as CoreCryptoHashBcrypto } from "@mainsail/crypto-hash-bcrypto";
import { ServiceProvider as CoreCryptoKeyPairEcdsa } from "@mainsail/crypto-key-pair-ecdsa";
import { ServiceProvider as CoreCryptoSignatureSchnorr } from "@mainsail/crypto-signature-schnorr-secp256k1";
import { Identifiers } from "@mainsail/contracts";
import { milestones } from "./crypto/networks/devnet/milestones.js";
import { network } from "./crypto/networks/devnet/network.js";

import { ServiceProvider as CoreCryptoTransaction } from "@mainsail/crypto-transaction";
import { ServiceProvider as CoreCryptoMultipaymentTransfer } from "@mainsail/crypto-transaction-multi-payment";
import { ServiceProvider as CoreCryptoTransactionTransfer } from "@mainsail/crypto-transaction-transfer";
import { ServiceProvider as CoreCryptoTransactionUsername } from "@mainsail/crypto-transaction-username-registration";
import { ServiceProvider as CoreCryptoTransactionValidatorRegistration } from "@mainsail/crypto-transaction-validator-registration";
import { ServiceProvider as CoreCryptoTransactionVote, VoteBuilder } from "@mainsail/crypto-transaction-vote";
import { ServiceProvider as CoreCryptoTransactionValidatorResignation } from "@mainsail/crypto-transaction-validator-resignation";
import { ServiceProvider as CoreFees } from "@mainsail/fees";
import { ServiceProvider as CoreFeesStatic } from "@mainsail/fees-static";

export class PublicKeyService extends Services.AbstractPublicKeyService {
readonly #config!: Interfaces.NetworkConfig;
readonly #app: Application;
#isBooted: boolean;

public constructor(container: IoC.IContainer) {
super(container);

this.#config = container.get(BindingType.Crypto);

this.#app = new Application(new Container());

this.#boot();
}

async #boot(): Promise<void> {
await Promise.all([
ItsANameToo marked this conversation as resolved.
Show resolved Hide resolved
// this.#app.resolve(CoreValidation).register(),
// this.#app.resolve(CoreCryptoConfig).register(),
// this.#app.resolve(CoreCryptoValidation).register(),
// this.#app.resolve(CoreCryptoKeyPairEcdsa).register(),
// this.#app.resolve(CoreCryptoAddressBase58).register(),
// this.#app.resolve(CoreCryptoSignatureSchnorr).register(),
// this.#app.resolve(CoreCryptoHashBcrypto).register(),
// this.#app.resolve(CoreFees).register(),
// this.#app.resolve(CoreFeesStatic).register(),
// this.#app.resolve(CoreCryptoTransaction).register(),
// this.#app.resolve(CoreCryptoTransactionTransfer).register(),
// this.#app.resolve(CoreCryptoTransactionVote).register(),
// this.#app.resolve(CoreCryptoMultipaymentTransfer).register(),
// this.#app.resolve(CoreCryptoTransactionUsername).register(),
// this.#app.resolve(CoreCryptoTransactionValidatorRegistration).register(),
// this.#app.resolve(CoreCryptoTransactionValidatorResignation).register(),
// this.#app.resolve<Contracts.Crypto.PublicKeyFactory>(PublicKeyFactory),
ItsANameToo marked this conversation as resolved.
Show resolved Hide resolved
// this.#app.resolve(CoreCryptoConsensusBls12381).register(),
ItsANameToo marked this conversation as resolved.
Show resolved Hide resolved
]);

this.#isBooted = true;
}

public override async fromMnemonic(
Expand Down Expand Up @@ -48,4 +104,25 @@ export class PublicKeyService extends Services.AbstractPublicKeyService {
publicKey: BasePublicKey.fromWIF(wif),
};
}

public override async verifyPublicKeyWithBLS(publicKey: string): Promise<boolean> {
if (!this.#isBooted) {
await this.#boot();
}

const consensusKeyPairFactory: Contracts.Crypto.KeyPairFactory = this.#app.getTagged(
Identifiers.Cryptography.Identity.KeyPair.Factory,
"type",
"consensus",
);

const consensusPublicKeyFactory: Contracts.Crypto.PublicKeyFactory = this.#app.getTagged(
Identifiers.Cryptography.Identity.PublicKey.Factory,
"type",
"consensus",
);

console.log("verifying", { publicKey });
return await consensusPublicKeyFactory.verify(publicKey);
}
}
1 change: 1 addition & 0 deletions packages/sdk/source/extended-public-key.contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { IdentityOptions } from "./shared.contract.js";

export interface ExtendedPublicKeyService {
fromMnemonic(mnemonic: string, options?: IdentityOptions): Promise<string>;
verifyPublicKeyWithBLS(publicKey: string): Promise<boolean>;
}
1 change: 1 addition & 0 deletions packages/sdk/source/public-key.contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface PublicKeyService {
fromMultiSignature(min: number, publicKeys: string[]): Promise<PublicKeyDataTransferObject>;
fromWIF(wif: string): Promise<PublicKeyDataTransferObject>;
fromSecret(secret: string): Promise<PublicKeyDataTransferObject>;
verifyPublicKeyWithBLS(publicKey: string): Promise<boolean>;
}
4 changes: 4 additions & 0 deletions packages/sdk/source/public-key.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ export class AbstractPublicKeyService implements PublicKeyService {
public async fromSecret(secret: string): Promise<PublicKeyDataTransferObject> {
throw new NotImplemented(this.constructor.name, this.fromSecret.name);
}

public async verifyPublicKeyWithBLS(secret: string): Promise<boolean> {
throw new NotImplemented(this.constructor.name, this.verifyPublicKeyWithBLS.name);
}
}
9 changes: 6 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading