Skip to content
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
64 changes: 23 additions & 41 deletions packages/crypto-address-base58/source/address.factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,28 @@ import cryptoJson from "../../core/bin/config/devnet/core/crypto.json";
import { describe } from "@mainsail/test-runner";
import { AddressFactory } from "./address.factory";

const mnemonic = "this is a top secret passphrase";
const wif = "SGq4xLgZKCGxs7bjmwnBrWcT4C1ADFEermj846KC97FSv1WFD1dA";
const mnemonic = "this is a top secret mnemonic";
const wif = "UfDzkBsi7xxjq491zm5tk7rCZ1EouBXsFUWaCvQWxAortbh1zq5T";

describe<{ app: Application }>("AddressFactory", ({ assert, beforeEach, it }) => {
describe<{ app: Application; factory: AddressFactory }>("AddressFactory", ({ assert, beforeEach, it }) => {
beforeEach(async (context) => {
context.app = new Application();
context.app.get<Contracts.Kernel.Repository>(Identifiers.Config.Repository).set("crypto", cryptoJson);
await context.app.resolve(ValidationServiceProvider).register();
await context.app.resolve(CryptoConfigServiceProvider).register();

context.app.get<Contracts.Crypto.Configuration>(Identifiers.Cryptography.Configuration).set("network.wif", 170);

await context.app.resolve<ECDSA>(ECDSA).register();
await context.app.resolve<CryptoHashBcrypto>(CryptoHashBcrypto).register();
context.factory = context.app.resolve(AddressFactory);
});

it("should derive an address from an mnemonic", async (context) => {
assert.is(
await context.app.resolve(AddressFactory).fromMnemonic(mnemonic),
"D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
);
it("#fromMnemonic - should derive an address from an mnemonic", async ({ factory }) => {
assert.is(await factory.fromMnemonic(mnemonic), "DLsMhiUzAVEXBXDTY1NGNZteWz8SDvphfa");
});

it("should derive an address from multi signature address", async (context) => {
it("#fromMultiSignatureAsset - should derive an address from multi signature address", async ({ factory }) => {
assert.is(
await context.app.resolve(AddressFactory).fromMultiSignatureAsset({
await factory.fromMultiSignatureAsset({
min: 3,
publicKeys: [
"0235d486fea0193cbe77e955ab175b8f6eb9eaf784de689beffbd649989f5d6be3",
Expand All @@ -47,55 +43,41 @@ describe<{ app: Application }>("AddressFactory", ({ assert, beforeEach, it }) =>
);
});

it("should derive an address from a public key", async (context) => {
it("#fromPublicKey - should derive an address from a public key", async ({ factory }) => {
assert.is(
await context.app
.resolve(AddressFactory)
.fromPublicKey("034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192"),
await factory.fromPublicKey("034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192"),
"D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
);
});

it("should derive an address from wif", async (context) => {
assert.is(await context.app.resolve(AddressFactory).fromWIF(wif), "D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib");
it("#fromWIF - should derive an address from wif", async ({ factory }) => {
assert.is(await factory.fromWIF(wif), "DLsMhiUzAVEXBXDTY1NGNZteWz8SDvphfa");
});

it("should validate addresses", async (context) => {
assert.true(await context.app.resolve(AddressFactory).validate("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib"));
assert.false(
await context.app
.resolve(AddressFactory)
.validate("m0d1q05ypy7qw2hhqqz28rwetc6dauge6g6g65npy2qht5pjuheqwrse7gxkhwv"),
);
it("#validate - should validate addresses", async ({ factory }) => {
assert.true(await factory.validate("DLsMhiUzAVEXBXDTY1NGNZteWz8SDvphfa"));
assert.false(await factory.validate("m0d1q05ypy7qw2hhqqz28rwetc6dauge6g6g65npy2qht5pjuheqwrse7gxkhwv"));
});

it("should convert between buffer", async (context) => {
const addressFactory = context.app.resolve(AddressFactory);

it("#toBuffer & #fromBuffer - should convert between buffer", async ({ factory }) => {
assert.equal(
await addressFactory.fromBuffer(await addressFactory.toBuffer("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib")),
"D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib",
await factory.fromBuffer(await factory.toBuffer("DLsMhiUzAVEXBXDTY1NGNZteWz8SDvphfa")),
"DLsMhiUzAVEXBXDTY1NGNZteWz8SDvphfa",
);
});

it("should throw if pubKeyHash doesn't match", async (context) => {
const addressFactory = context.app.resolve(AddressFactory);

context.app
.get<Contracts.Crypto.Configuration>(Identifiers.Cryptography.Configuration)
.set("network.pubKeyHash", 44);
it("should throw if pubKeyHash doesn't match", async ({ factory, app }) => {
app.get<Contracts.Crypto.Configuration>(Identifiers.Cryptography.Configuration).set("network.pubKeyHash", 44);

await assert.rejects(
() => addressFactory.toBuffer("D61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib"),
() => factory.toBuffer("DLsMhiUzAVEXBXDTY1NGNZteWz8SDvphfa"),
"Expected address network byte 44, but got 30.",
);
});

it("should throw invalid checksum", async (context) => {
const addressFactory = context.app.resolve(AddressFactory);

it("#toBuffer - should throw invalid checksum", async ({ factory }) => {
await assert.rejects(
() => addressFactory.toBuffer("E61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib"),
() => factory.toBuffer("E61mfSggzbvQgTUe6JhYKH2doHaqJ3Dyib"),
"Invalid checksum for base58 string.",
);
});
Expand Down
6 changes: 3 additions & 3 deletions packages/crypto-address-base58/source/schemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe<{
}
});

it("address - should be ok", ({ validator }) => {
it("#legacyAddress - should be ok", ({ validator }) => {
assert.undefined(validator.validate("legacyAddress", "a".repeat(length)).error);

const validChars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
Expand All @@ -44,7 +44,7 @@ describe<{
}
});

it("address - should be ok for factory", async (context) => {
it("#legacyAddress - should be ok for factory", async (context) => {
await context.app.resolve<ECDSA>(ECDSA).register();

assert.undefined(
Expand All @@ -55,7 +55,7 @@ describe<{
);
});

it("address - should not be ok", ({ validator }) => {
it("#legacyAddress - should not be ok", ({ validator }) => {
assert.defined(validator.validate("legacyAddress", "a".repeat(length - 2)).error);
assert.defined(validator.validate("legacyAddress", "a".repeat(length + 1)).error);
assert.defined(validator.validate("legacyAddress", 123).error);
Expand Down
110 changes: 57 additions & 53 deletions packages/crypto-address-keccak256/source/address.factory.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,63 @@
import { Identifiers } from "@mainsail/constants";
import { Configuration } from "@mainsail/crypto-config";
import { ServiceProvider as ECDSA } from "@mainsail/crypto-key-pair-ecdsa";
import { Application } from "@mainsail/kernel";
import { Contracts } from "@mainsail/contracts";
import { ServiceProvider as ValidationServiceProvider } from "@mainsail/validation";
import { ServiceProvider as CryptoConfigServiceProvider } from "@mainsail/crypto-config";

import { describe } from "@mainsail/test-runner";
import { AddressFactory } from "./address.factory";

const mnemonic =
"program fragile industry scare sun visit race erase daughter empty anxiety cereal cycle hunt airport educate giggle picture sunset apart jewel similar pulp moment";
import { wallets } from "../../crypto-wif/test/index.js";
import cryptoJson from "../../core/bin/config/devnet/core/crypto.json";

const wif = "SDuW66dyGZ1zPZdN7ncEevbJdjaQTj9pT4LcmKzQ7eLFoyCXEdkx";

describe<{ app: Application }>("AddressFactory", ({ assert, beforeEach, it }) => {
describe<{ app: Application; factory: AddressFactory }>("AddressFactory", ({ assert, beforeEach, it, each }) => {
beforeEach(async (context) => {
context.app = new Application();
context.app.bind(Identifiers.Cryptography.Configuration).to(Configuration).inSingletonScope();

context.app.get<Contracts.Kernel.Repository>(Identifiers.Config.Repository).set("crypto", cryptoJson);
await context.app.resolve(ValidationServiceProvider).register();
await context.app.resolve(CryptoConfigServiceProvider).register();

await context.app.resolve<ECDSA>(ECDSA).register();

context.factory = context.app.resolve(AddressFactory);
});

it("should derive an address from an mnemonic", async (context) => {
assert.is(
await context.app.resolve(AddressFactory).fromMnemonic(mnemonic),
"0xC7C50f33278bDe272ffe23865fF9fBd0155a5175",
);
each(
"#fromMnemonic - should derive an address from an mnemonic",
async ({ context: { factory }, dataset: wallet }) => {
assert.is(await factory.fromMnemonic(wallet.mnemonic), wallet.address);
},
wallets,
);

each(
"#fromPublicKey - should derive an address from a public key",
async ({ context: { factory }, dataset: wallet }) => {
assert.is(await factory.fromPublicKey(wallet.publicKey), wallet.address);
},
wallets,
);

it("#fromPublicKey - should throw if public key doesn't have 65 chars", async ({ factory }) => {
await assert.rejects(() => factory.fromPublicKey("0".repeat(66 * 2)), "Invalid uncompressed public key");
});

it("#fromPublicKey - should throw if public key doesn't start with 0x04", async ({ factory }) => {
await assert.rejects(() => factory.fromPublicKey("0".repeat(65 * 2)), "Invalid uncompressed public key");
});

it("should derive an address from multi signature address", async (context) => {
each(
"#fromWIF - should derive an address from wif",
async ({ context: { factory }, dataset: wallet }) => {
assert.is(await factory.fromWIF(wallet.wif), wallet.address);
},
wallets,
);

it("#fromMultiSignatureAsset - should derive an address from multi signature address", async ({ factory }) => {
assert.is(
await context.app.resolve(AddressFactory).fromMultiSignatureAsset({
await factory.fromMultiSignatureAsset({
min: 3,
publicKeys: [
"0235d486fea0193cbe77e955ab175b8f6eb9eaf784de689beffbd649989f5d6be3",
Expand All @@ -42,49 +69,26 @@ describe<{ app: Application }>("AddressFactory", ({ assert, beforeEach, it }) =>
);
});

it("should derive an address from a public key", async (context) => {
assert.is(
await context.app
.resolve(AddressFactory)
.fromPublicKey("03e84093c072af70004a38dd95e34def119d2348d5261228175d032e5f2070e19f"),
"0xC7C50f33278bDe272ffe23865fF9fBd0155a5175",
);
});

it("should throw if public key doesn't have 65 chars", async (context) => {
await assert.rejects(
() => context.app.resolve(AddressFactory).fromPublicKey("0".repeat(66 * 2)),
"Invalid uncompressed public key",
);
});
each(
"#validate - should be valid",
async ({ context: { factory }, dataset: address }) => {
assert.true(await factory.validate(address));
},
["0xC7C50f33278bDe272ffe23865fF9fBd0155a5175", "0xC7C50f33278bDe272ffe23865fF9fBd0155a5175"].concat(
wallets.map((wallet) => wallet.address),
),
);

it("should throw if public key doesn't start with 0x04", async (context) => {
await assert.rejects(
() => context.app.resolve(AddressFactory).fromPublicKey("0".repeat(65 * 2)),
"Invalid uncompressed public key",
);
});

it("should derive an address from wif", async (context) => {
assert.is(await context.app.resolve(AddressFactory).fromWIF(wif), "0xC7C50f33278bDe272ffe23865fF9fBd0155a5175");
});

it("should validate addresses", async (context) => {
assert.true(await context.app.resolve(AddressFactory).validate("0xC7C50f33278bDe272ffe23865fF9fBd0155a5175"));
assert.true(await context.app.resolve(AddressFactory).validate("0xC7C50f33278bDe272ffe23865fF9fBd0155a5175"));
assert.false(await context.app.resolve(AddressFactory).validate("0xC7C50f33278bde272ffe23865ff9fbd0155a5175"));
assert.false(
await context.app
.resolve(AddressFactory)
.validate("m0d1q05ypy7qw2hhqqz28rwetc6dauge6g6g65npy2qht5pjuheqwrse7gxkhwv"),
);
it("#validate - should be invalid", async ({ factory }) => {
assert.false(await factory.validate("0xC7C50f33278bde272ffe23865ff9fbd0155a5175"));
assert.false(await factory.validate("m0d1q05ypy7qw2hhqqz28rwetc6dauge6g6g65npy2qht5pjuheqwrse7gxkhwv"));
});

it("should convert from and to buffer", async (context) => {
const buffer = await context.app.resolve(AddressFactory).toBuffer("0xC7C50f33278bDe272ffe23865fF9fBd0155a5175");
it("#toBuffer and #fromBuffer - should convert from and to buffer", async ({ factory }) => {
const buffer = await factory.toBuffer("0xC7C50f33278bDe272ffe23865fF9fBd0155a5175");
assert.equal(buffer.byteLength, 20);

const restored = await context.app.resolve(AddressFactory).fromBuffer(buffer);
const restored = await factory.fromBuffer(buffer);
assert.equal(restored, "0xC7C50f33278bDe272ffe23865fF9fBd0155a5175");
});
});
6 changes: 3 additions & 3 deletions packages/crypto-address-keccak256/source/schemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe<{
}
});

it("address - should be ok", ({ validator }) => {
it("#address - should be ok", ({ validator }) => {
const prefix = "0x";

assert.undefined(validator.validate("address", prefix + "a".repeat(length - prefix.length)).error);
Expand All @@ -43,7 +43,7 @@ describe<{
}
});

it("address - should be ok for factory", async (context) => {
it("#address - should be ok for factory", async (context) => {
await context.app.resolve<ECDSA>(ECDSA).register();

assert.undefined(
Expand All @@ -54,7 +54,7 @@ describe<{
);
});

it("address - should not be ok", ({ validator }) => {
it("#address - should not be ok", ({ validator }) => {
const prefix = "0x";
const invalidPrefix = "1x";

Expand Down
33 changes: 17 additions & 16 deletions packages/crypto-address-keccak256/source/serializer.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { Identifiers } from "@mainsail/constants";
import { Configuration } from "@mainsail/crypto-config";
import { ServiceProvider as ECDSA } from "@mainsail/crypto-key-pair-ecdsa";
import { ServiceProvider as ValidationServiceProvider } from "@mainsail/validation";
import { ByteBuffer } from "@mainsail/utils";
import { Buffer } from "buffer";

Expand All @@ -10,33 +7,37 @@ import { describe } from "@mainsail/test-runner";
import { AddressFactory } from "./address.factory";
import { AddressSerializer } from "./serializer";

import { wallets } from "../../crypto-wif/test/index.js";

describe<{
app: Application;
serializer: AddressSerializer;
factory: AddressFactory;
}>("AddressSerializer", ({ it, assert, beforeEach }) => {
}>("AddressSerializer", ({ it, assert, beforeEach, each }) => {
beforeEach(async (context) => {
context.app = new Application();
context.app.bind(Identifiers.Cryptography.Configuration).to(Configuration).inSingletonScope();

await context.app.resolve(ValidationServiceProvider).register();
await context.app.resolve<ECDSA>(ECDSA).register();
context.app.bind(Identifiers.Cryptography.Identity.KeyPair.Factory).toConstantValue({});
context.app.bind(Identifiers.Cryptography.Identity.PublicKey.Factory).toConstantValue({});

context.serializer = context.app.resolve(AddressSerializer);
context.factory = context.app.resolve(AddressFactory);
});

it("should serialize and deserialize address", async ({ factory, serializer }) => {
const address = "0xC7C50f33278bDe272ffe23865fF9fBd0155a5175";
const buffer = await factory.toBuffer(address);
each(
"#serialize & #deserialize - should serialize and deserialize address",
async ({ context: { factory, serializer }, dataset: wallet }) => {
const buffer = await factory.toBuffer(wallet.address);

const byteBuffer = ByteBuffer.fromBuffer(Buffer.alloc(100));
const byteBuffer = ByteBuffer.fromBuffer(Buffer.alloc(100));

serializer.serialize(byteBuffer, buffer);
byteBuffer.reset();
serializer.serialize(byteBuffer, buffer);
byteBuffer.reset();

const readBuffer = serializer.deserialize(byteBuffer);
const readBuffer = serializer.deserialize(byteBuffer);

assert.equal(await factory.fromBuffer(readBuffer), address);
});
assert.equal(await factory.fromBuffer(readBuffer), wallet.address);
},
wallets,
);
});
4 changes: 2 additions & 2 deletions packages/crypto-key-pair-bls12-381/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
"@mainsail/kernel": "workspace:*",
"@mainsail/utils": "workspace:*",
"@scure/bip39": "2.0.1",
"bls12-381-keygen": "0.2.4",
"wif": "5.0.0"
"bls12-381-keygen": "0.2.4"
},
"devDependencies": {
"@mainsail/contracts": "workspace:*",
"@mainsail/crypto-config": "workspace:*",
"@mainsail/crypto-wif": "workspace:*",
"@mainsail/test-runner": "workspace:*",
"@mainsail/validation": "workspace:*",
"bip39": "3.1.0",
Expand Down
Loading
Loading