2023-04-03 21:52:40 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2023 Fluence Labs Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-02-13 17:41:35 +03:00
|
|
|
import { CallParams, PeerIdB58 } from '@fluencelabs/interfaces';
|
2023-04-03 21:52:40 +04:00
|
|
|
import { KeyPair } from '../keypair/index.js';
|
|
|
|
import { FluencePeer } from '../jsPeer/FluencePeer.js';
|
|
|
|
import { SigDef } from './_aqua/services.js';
|
2023-02-13 17:41:35 +03:00
|
|
|
import { allowOnlyParticleOriginatedAt, allowServiceFn, and, or, SecurityGuard } from './securityGuard.js';
|
2022-02-04 22:39:41 +03:00
|
|
|
|
|
|
|
export const defaultSigGuard = (peerId: PeerIdB58) => {
|
2022-11-03 21:22:10 +03:00
|
|
|
return and<'data'>(
|
2022-02-04 22:39:41 +03:00
|
|
|
allowOnlyParticleOriginatedAt(peerId),
|
|
|
|
or(
|
|
|
|
allowServiceFn('trust-graph', 'get_trust_bytes'),
|
|
|
|
allowServiceFn('trust-graph', 'get_revocation_bytes'),
|
2022-04-13 12:04:48 +04:00
|
|
|
allowServiceFn('registry', 'get_key_bytes'),
|
2022-02-04 22:39:41 +03:00
|
|
|
allowServiceFn('registry', 'get_record_bytes'),
|
2022-09-23 14:45:42 +04:00
|
|
|
allowServiceFn('registry', 'get_record_metadata_bytes'),
|
|
|
|
allowServiceFn('registry', 'get_tombstone_bytes'),
|
2022-02-04 22:39:41 +03:00
|
|
|
),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export class Sig implements SigDef {
|
2023-04-03 21:52:40 +04:00
|
|
|
constructor(private keyPair: KeyPair) {}
|
2022-02-04 22:39:41 +03:00
|
|
|
|
|
|
|
/**
|
2022-11-03 21:22:10 +03:00
|
|
|
* Configurable security guard for sign method
|
2022-02-04 22:39:41 +03:00
|
|
|
*/
|
2022-11-03 21:22:10 +03:00
|
|
|
securityGuard: SecurityGuard<'data'> = (params) => {
|
2022-02-04 22:39:41 +03:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the public key of KeyPair. Required by aqua
|
|
|
|
*/
|
2022-09-05 19:27:19 +03:00
|
|
|
get_peer_id() {
|
2023-04-03 21:52:40 +04:00
|
|
|
return this.keyPair.getPeerId();
|
2022-02-04 22:39:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Signs the data using key pair's private key. Required by aqua
|
|
|
|
*/
|
|
|
|
async sign(
|
|
|
|
data: number[],
|
|
|
|
callParams: CallParams<'data'>,
|
|
|
|
): Promise<{ error: string | null; signature: number[] | null; success: boolean }> {
|
|
|
|
if (!this.securityGuard(callParams)) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
error: 'Security guard validation failed',
|
|
|
|
signature: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-03 21:52:40 +04:00
|
|
|
const signedData = await this.keyPair.signBytes(Uint8Array.from(data));
|
2022-02-04 22:39:41 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
error: null,
|
|
|
|
signature: Array.from(signedData),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies the signature. Required by aqua
|
|
|
|
*/
|
|
|
|
verify(signature: number[], data: number[]): Promise<boolean> {
|
2023-04-03 21:52:40 +04:00
|
|
|
return this.keyPair.verify(Uint8Array.from(data), Uint8Array.from(signature));
|
2022-02-04 22:39:41 +03:00
|
|
|
}
|
|
|
|
}
|
2023-04-03 21:52:40 +04:00
|
|
|
|
|
|
|
export const getDefaultSig = (peer: FluencePeer) => {
|
|
|
|
peer.registerMarineService;
|
|
|
|
};
|