mirror of
https://github.com/fluencelabs/fluence-js.git
synced 2025-06-28 23:31:33 +00:00
21
packages/fluence-keypair/.gitignore
vendored
Normal file
21
packages/fluence-keypair/.gitignore
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
bundle/
|
||||
|
||||
dist
|
||||
esm
|
||||
types
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
.idea
|
12
packages/fluence-keypair/.npmignore
Normal file
12
packages/fluence-keypair/.npmignore
Normal file
@ -0,0 +1,12 @@
|
||||
.idea
|
||||
.gitignore
|
||||
node_modules
|
||||
types
|
||||
|
||||
src/
|
||||
|
||||
tsconfig.json
|
||||
webpack.config.js
|
||||
|
||||
bundle
|
||||
pkg
|
8
packages/fluence-keypair/.prettierrc.js
Normal file
8
packages/fluence-keypair/.prettierrc.js
Normal file
@ -0,0 +1,8 @@
|
||||
module.exports = {
|
||||
semi: true,
|
||||
trailingComma: "all",
|
||||
singleQuote: true,
|
||||
printWidth: 120,
|
||||
tabWidth: 4,
|
||||
useTabs: false
|
||||
};
|
13
packages/fluence-keypair/CONTRIBUTING.md
Normal file
13
packages/fluence-keypair/CONTRIBUTING.md
Normal file
@ -0,0 +1,13 @@
|
||||
## Contribute Code
|
||||
|
||||
You are welcome to contribute to Fluence.
|
||||
|
||||
Things you need to know:
|
||||
|
||||
1. You need to **agree to the Contributors License Agreement**. This is a common practice in all major Open Source projects. At the current moment we are unable to accept contributions made on behalf of a company. Only individual contributions will be accepted.
|
||||
2. **Not all proposed contributions can be accepted**. Some features may e.g. just fit a third-party add-on better. The contribution must fit the overall direction of Fluence and really improve it. The more effort you invest, the better you should clarify in advance whether the contribution fits: the best way would be to just open an issue to discuss the contribution you plan to make.
|
||||
|
||||
### Contributor License Agreement
|
||||
|
||||
When you contribute, you have to be aware that your contribution is covered by **Apache License 2.0**, but might relicensed under few other software licenses mentioned in the **Contributor License Agreement**.
|
||||
In particular you need to agree to the [Contributor License Agreement](https://gist.github.com/fluencelabs-org/3f4cbb3cc14c1c0fb9ad99d8f7316ed7). If you agree to its content, you simply have to click on the link posted by the CLA assistant as a comment to the pull request. Click it to check the CLA, then accept it on the following screen if you agree to it. CLA assistant will save this decision for upcoming contributions and will notify you if there is any change to the CLA in the meantime.
|
11
packages/fluence-keypair/README.md
Normal file
11
packages/fluence-keypair/README.md
Normal file
@ -0,0 +1,11 @@
|
||||
# FluenceJS Keypair
|
||||
|
||||
This package is a part of FluenceJS, the official implementation of the Fluence Peer in typescript. See the [FluenceJS repo](https://github.com/fluencelabs/fluence-js) for more info
|
||||
|
||||
## Contributing
|
||||
|
||||
While the project is still in the early stages of development, you are welcome to track progress and contribute. As the project is undergoing rapid changes, interested contributors should contact the team before embarking on larger pieces of work. All contributors should consult with and agree to our [basic contributing rules](CONTRIBUTING.md).
|
||||
|
||||
## License
|
||||
|
||||
[Apache 2.0](LICENSE)
|
25
packages/fluence-keypair/package.json
Normal file
25
packages/fluence-keypair/package.json
Normal file
@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "@fluencelabs/keypair",
|
||||
"version": "0.1.0",
|
||||
"description": "Keypair implementation for Fluence JS Peer",
|
||||
"main": "./dist/index.js",
|
||||
"typings": "./dist/index.d.ts",
|
||||
"engines": {
|
||||
"node": ">=10",
|
||||
"pnpm": ">=3"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc"
|
||||
},
|
||||
"repository": "https://github.com/fluencelabs/fluence-js",
|
||||
"author": "Fluence Labs",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"peer-id": "0.16.0",
|
||||
"libp2p-crypto": "0.21.2",
|
||||
"js-base64": "^3.7.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^4.6.4"
|
||||
}
|
||||
}
|
75
packages/fluence-keypair/src/index.ts
Normal file
75
packages/fluence-keypair/src/index.ts
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2020 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.
|
||||
*/
|
||||
|
||||
import * as PeerId from 'peer-id';
|
||||
import { keys } from 'libp2p-crypto';
|
||||
import { toUint8Array } from 'js-base64';
|
||||
|
||||
export class KeyPair {
|
||||
/**
|
||||
* Key pair in libp2p format. Used for backward compatibility with the current FluencePeer implementation
|
||||
*/
|
||||
public Libp2pPeerId: PeerId;
|
||||
|
||||
constructor(libp2pPeerId: PeerId) {
|
||||
this.Libp2pPeerId = libp2pPeerId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates new KeyPair from ed25519 private key represented as a 32 byte array
|
||||
* @param key - Any sequence of 32 bytes
|
||||
* @returns - Promise with the created KeyPair
|
||||
*/
|
||||
static async fromEd25519SK(arr: Uint8Array): Promise<KeyPair> {
|
||||
// generateKeyPairFromSeed takes seed and copies it to private key as is
|
||||
const privateKey = await keys.generateKeyPairFromSeed('Ed25519', arr, 256);
|
||||
const lib2p2Pid = await PeerId.createFromPrivKey(privateKey.bytes);
|
||||
return new KeyPair(lib2p2Pid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates new KeyPair with a random secret key
|
||||
* @returns - Promise with the created KeyPair
|
||||
*/
|
||||
static async randomEd25519(): Promise<KeyPair> {
|
||||
const lib2p2Pid = await PeerId.create({ keyType: 'Ed25519' });
|
||||
return new KeyPair(lib2p2Pid);
|
||||
}
|
||||
|
||||
toB58String(): string {
|
||||
return this.Libp2pPeerId.toB58String();
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns 32 byte private key
|
||||
*/
|
||||
toEd25519PrivateKey(): Uint8Array {
|
||||
return this.Libp2pPeerId.privKey.marshal().subarray(0, 32);
|
||||
}
|
||||
|
||||
signBytes(data: Uint8Array): Promise<Uint8Array> {
|
||||
return this.Libp2pPeerId.privKey.sign(data);
|
||||
}
|
||||
|
||||
verify(data: Uint8Array, signature: Uint8Array): Promise<boolean> {
|
||||
return this.Libp2pPeerId.privKey.public.verify(data, signature);
|
||||
}
|
||||
}
|
||||
|
||||
export const keyPairFromBase64Sk = (sk: string): Promise<KeyPair> => {
|
||||
const arr = toUint8Array(sk);
|
||||
return KeyPair.fromEd25519SK(arr);
|
||||
};
|
12
packages/fluence-keypair/tsconfig.json
Normal file
12
packages/fluence-keypair/tsconfig.json
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist/",
|
||||
"lib": ["ES2015"],
|
||||
"target": "ES5",
|
||||
"sourceMap": true,
|
||||
"declaration": true,
|
||||
"module": "commonjs",
|
||||
},
|
||||
"exclude": ["node_modules", "dist"],
|
||||
"include": ["src/**/*"]
|
||||
}
|
Reference in New Issue
Block a user