Skip to content

Commit

Permalink
Merge pull request #274 from logion-network/feature/human-readable-sign
Browse files Browse the repository at this point in the history
Human readable sign-in
  • Loading branch information
benoitdevos authored Jul 1, 2024
2 parents 4642b96 + c9d249d commit 3d7d92b
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 24 deletions.
5 changes: 3 additions & 2 deletions packages/client-node/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ services:
- IPFS_HOST=/dns4/ipfs1/tcp/5001
- IPFS_MIN_REPLICA=1
- IPFS_MAX_REPLICA=2
- ENC_PASSWORD=test
- ENC_PASSWORD_5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY=test1
- ENC_PASSWORD_5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y=test2
depends_on:
- private-database1
- ipfs-cluster1
Expand Down Expand Up @@ -79,7 +80,7 @@ services:
- IPFS_HOST=/dns4/ipfs2/tcp/5001
- IPFS_MIN_REPLICA=1
- IPFS_MAX_REPLICA=2
- ENC_PASSWORD=test
- ENC_PASSWORD_5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty=test
depends_on:
- private-database2
- ipfs-cluster2
Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@logion/client",
"version": "0.46.1-1",
"version": "0.46.1-2",
"description": "logion SDK for client applications",
"main": "dist/index.js",
"packageManager": "[email protected]",
Expand Down
7 changes: 2 additions & 5 deletions packages/client/src/AuthenticationClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,14 @@ export class AuthenticationClient {
const addresses = validAccountIds.map(validAccountId => validAccountId.toKey());
const signInResponse = await axios.post("/api/auth/sign-in", { addresses });
const sessionId = signInResponse.data.sessionId;
const attributes = [ sessionId ];

const signatures: Record<string, AuthenticationSignature> = {};
for(const validAccountId of validAccountIds) {
const signedOn = DateTime.now();
const signature = await signer.signRaw({
signerId: validAccountId,
resource: 'authentication',
operation: 'login',
signedOn,
attributes,
sessionId,
});
signatures[validAccountId.toKey()] = {
signature: signature.signature,
Expand All @@ -66,7 +63,7 @@ export class AuthenticationClient {
};
}

const authenticateResponse = await axios.post(`/api/auth/${sessionId}/authenticate`, {
const authenticateResponse = await axios.post(`/api/auth/${sessionId}/authenticate/v2`, {
signatures
});
const authenticatedAddresses: AuthenticationResponse = authenticateResponse.data.tokens;
Expand Down
13 changes: 2 additions & 11 deletions packages/client/src/Signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ import { ISubmittableResult } from '@polkadot/types/types';
import { ExtrinsicStatus } from '@polkadot/types/interfaces/author';
import { Registry } from '@polkadot/types-codec/types';
import { base64Encode } from '@polkadot/util-crypto';
import { stringToHex } from '@polkadot/util';
import { Hash } from 'fast-sha256';
import { DateTime } from "luxon";
import { toIsoString } from "./DateTimeUtil.js";
import { requireDefined } from "./assertions.js";

export interface SignRawParameters {
signerId: ValidAccountId;
resource: string;
operation: string;
signedOn: DateTime;
attributes: string[];
sessionId: string;
}

export type SignatureType = "POLKADOT" | "ETHEREUM" | "CROSSMINT_ETHEREUM" | "MULTIVERSX";
Expand Down Expand Up @@ -107,13 +104,7 @@ export abstract class BaseSigner implements FullSigner {
abstract signToHex(signerId: ValidAccountId, message: string): Promise<TypedSignature>;

buildMessage(parameters: SignRawParameters): string {
return stringToHex(hashAttributes(this.buildAttributes(parameters)));
}

buildAttributes(parameters: SignRawParameters): string[] {
const signedOn = toIsoString(parameters.signedOn);
const attributes = [parameters.resource, parameters.operation, signedOn];
return attributes.concat(parameters.attributes);
return `logion-auth: ${ parameters.sessionId } on ${ toIsoString(parameters.signedOn) }`
}

async signAndSend(parameters: SignParameters): Promise<SuccessfulSubmission> {
Expand Down
8 changes: 3 additions & 5 deletions packages/client/test/AuthenticationClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ function setupSignIn(axiosInstance: Mock<AxiosInstance>, validAccountIds: ValidA
function setupSignatures(signer: Mock<RawSigner>, addresses: ValidAccountId[], sessionId: string, signatures: string[]) {
for(let i = 0; i < addresses.length; ++i) {
signer.setup(instance => instance.signRaw(It.Is<SignRawParameters>(params =>
params.resource === "authentication"
&& params.signerId === addresses[i]
&& params.operation === "login"
&& params.attributes[0] === sessionId
params.signerId === addresses[i]
&& params.sessionId === sessionId
))).returns(Promise.resolve({ signature: signatures[i], type: "POLKADOT"}));
}
}
Expand All @@ -131,7 +129,7 @@ function setupAuthenticate(axiosInstance: Mock<AxiosInstance>, accounts: ValidAc
authenticateResponse.setup(instance => instance.data).returns({
tokens
});
axiosInstance.setup(instance => instance.post(`/api/auth/${sessionId}/authenticate`, It.Is<any>(body =>
axiosInstance.setup(instance => instance.post(`/api/auth/${sessionId}/authenticate/v2`, It.Is<any>(body =>
bodyIncludesSignatures(body, accounts, signatures)
))).returns(Promise.resolve(authenticateResponse.object()));
}
Expand Down

0 comments on commit 3d7d92b

Please sign in to comment.