Swiftpack.co - swift-libp2p/swift-peer-id as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by swift-libp2p.
swift-libp2p/swift-peer-id 0.0.1
Abstraction for managing Libp2p public / private key pairs and identities
⭐️ 0
🕓 2 weeks ago
iOS macOS
.package(url: "https://github.com/swift-libp2p/swift-peer-id.git", from: "0.0.1")

PeerID

Swift Package Manager compatible

An API / abstraction for managing libp2p public/private key pairs and identities

Table of Contents

Overview

Libp2p uses cryptographic key pairs to sign & verify messages and derive unique peer identities (Peer ID's). This library wraps a public / private key pair in a PeerID object that exposes certain functionality for use with Libp2p nodes.

A Peer ID is the SHA-256 multihash of a public key.

The public key is a base64 encoded string of a protobuf containing an RSA DER buffer.

Note:

Install

Include the following dependency in your Package.swift file

let package = Package(
    ...
    dependencies: [
        ...
        .package(url: "https://github.com/swift-libp2p/swift-peer-id.git", .upToNextMajor(from: "0.0.1"))
    ],
    ...
        .target(
            ...
            dependencies: [
                ...
                .product(name: "PeerID", package: "swift-peer-id"),
            ]),
    ...
)

Usage

Example

check out the tests for more examples


import PeerID

/// Generate a new PeerID
let peerID = try PeerID(.Ed25519)

peerID.b58String                        // -> QmVJHUwJBshjMa7Ctngt34MXgXMTDeM5RjvgQNGqsiPLzB (libp2p PeerID standard)
peerID.keyPair                          // Access to the underlying key pair
peerID.keyPair?.keyType == .ed25519     // The type of Key
peerID.keyPair?.privateKey              // Access to the private key (for signing)
peerID.keyPair?.publicKey               // Access to the public key (for verifying signatures)

API

/// Initializers
/// Generate a new PeerID with an underlying Key Pair (defaults to 2048 bit RSA)
PeerID.init(_ keyType:LibP2PCrypto.Keys.KeyPairType = .RSA(bits: .B2048)) throws

/// Use an existing Key Pair to instantiate a PeerID
PeerID.init(keyPair:LibP2PCrypto.Keys.KeyPair) throws

/// Inits a `PeerID` based solely on an ID value with no underlying `KeyPair`
PeerID.init(fromHexID hex:String) throws

/// Inits a `PeerID` based solely on an ID value with no underlying `KeyPair`
PeerID.init(fromBytesID bytes:[UInt8]) throws

/// Inits a `PeerID` from a v0 dag-pb or v1 libp2p-key CID complient string
PeerID.init(cid:String) throws

/// Inits a `PeerID` from a v0 dag-pb or v1 libp2p-key CID
PeerID.init(cid:CID) throws 

/// Inits a `PeerID` from a marshaled `PeerID` string
PeerID.init(marshaledPeerID:String, base: BaseEncoding? = nil) throws

/// Inits a `PeerID` from a marshaled `PeerID`
PeerID.init(marshaledPeerID data:Data) throws

/// Inits a `PeerID` from a marshaled public key string
PeerID.init(marshaledPublicKey str:String, base:BaseEncoding) throws

/// Inits a `PeerID` from a marshaled public key
PeerID.init(marshaledPublicKey key:Data) throws

/// Inits a `PeerID` from a marshaled private key string
PeerID.init(marshaledPrivateKey str:String, base:BaseEncoding) throws

/// Inits a `PeerID` from a marshaled private key
PeerID.init(marshaledPrivateKey data:Data) throws


/// Properties
/// Returns the PeerID's id as a base58 string (multihash/CIDv0).
PeerID.b58String:String

/// Returns the PeerID's id as a hex string.
PeerID.hexString:String

/// A base32 encoded, version 1 CID, representing this PeerID
PeerID.cidString:String

/// A simple way of checking a PeerID's type (id only, public key & id, or private key, public key and id)
PeerID.type:PeerType

/// Returns the id of this PeerID as bytes
PeerID.bytes:[UInt8] 


/// Methods
/// Returns a protocol-buffers encoded version of the id, public key and, if `includingPrivateKey` is set to `true`, the private key.
PeerID.marshal(includingPrivateKey:Bool = false) throws -> [UInt8] 

/// Exports our PeerID as a JSON object
PeerID.toJSON(includingPrivateKey:Bool = false) throws -> Data

/// Exports our PeerID as a JSON string
PeerID.toJSONString(includingPrivateKey:Bool = false) throws -> String?


/// Signing and Verifying
// Signs data using this PeerID's private key. This signature can then be verified by a remote peer using this PeerID's public key
PeerID.signature(for msg:Data) throws -> Data

// Using this PeerID's public key, this method checks to see if the signature data was in fact signed by this peer and is a valid signature for the expected data
PeerID.isValidSignature(_ signature:Data, for expectedData:Data) throws -> Bool 

Contributing

Contributions are welcomed! This code is very much a proof of concept. I can guarantee you there's a better / safer way to accomplish the same results. Any suggestions, improvements, or even just critques, are welcome!

Let's make this code better together! 🤝

Credits

License

MIT © 2022 Breth Inc.

GitHub

link
Stars: 0
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics