protocol

package
v0.0.0-...-2325473 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2022 License: MIT Imports: 6 Imported by: 25

Documentation

Overview

license: https://mit-license.org * * Ming-Ke-Ming : Decentralized User Identity Authentication * * Written in 2020 by Moky <albert.moky@gmail.com> * * ============================================================================== * The MIT License (MIT) * * Copyright (c) 2020 Albert Moky * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * ==============================================================================

license: https://mit-license.org * * Ming-Ke-Ming : Decentralized User Identity Authentication * * Written in 2021 by Moky <albert.moky@gmail.com> * * ============================================================================== * The MIT License (MIT) * * Copyright (c) 2021 Albert Moky * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * ==============================================================================

license: https://mit-license.org * * Ming-Ke-Ming : Decentralized User Identity Authentication * * Written in 2020 by Moky <albert.moky@gmail.com> * * ============================================================================== * The MIT License (MIT) * * Copyright (c) 2020 Albert Moky * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * ==============================================================================

license: https://mit-license.org * * Ming-Ke-Ming : Decentralized User Identity Authentication * * Written in 2020 by Moky <albert.moky@gmail.com> * * ============================================================================== * The MIT License (MIT) * * Copyright (c) 2020 Albert Moky * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * ==============================================================================

license: https://mit-license.org * * Ming-Ke-Ming : Decentralized User Identity Authentication * * Written in 2020 by Moky <albert.moky@gmail.com> * * ============================================================================== * The MIT License (MIT) * * Copyright (c) 2020 Albert Moky * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * ==============================================================================

license: https://mit-license.org * * Ming-Ke-Ming : Decentralized User Identity Authentication * * Written in 2020 by Moky <albert.moky@gmail.com> * * ============================================================================== * The MIT License (MIT) * * Copyright (c) 2020 Albert Moky * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * ==============================================================================

Index

Constants

View Source
const (
	Anywhere   = "anywhere"
	Everywhere = "everywhere"
)

*

  • Address for broadcast
View Source
const (
	// Document types
	VISA     = "visa"     // for login/communication
	PROFILE  = "profile"  // for user info
	BULLETIN = "bulletin" // for group info
)
View Source
const (
	Moky     = "moky"
	Anyone   = "anyone"
	Everyone = "everyone"
)

*

  • ID for broadcast

Variables

This section is empty.

Functions

func AddressSetFactory

func AddressSetFactory(factory AddressFactory)

func CreateBroadcastIdentifiers

func CreateBroadcastIdentifiers()

func DocumentGetType

func DocumentGetType(doc map[string]interface{}) string

func DocumentSetFactory

func DocumentSetFactory(docType string, factory DocumentFactory)

func IDRevert

func IDRevert(members []ID) []string

func IDSetFactory

func IDSetFactory(factory IDFactory)

func MetaCheck

func MetaCheck(meta Meta) bool

*

  • Check meta valid
  • (must call this when received a new meta from network)

func MetaMatchID

func MetaMatchID(meta Meta, identifier ID) bool

func MetaMatchKey

func MetaMatchKey(meta Meta, key VerifyKey) bool

func MetaSetFactory

func MetaSetFactory(version MetaType, factory MetaFactory)

func MetaTypeGetAlias

func MetaTypeGetAlias(version MetaType) string

func MetaTypeHasSeed

func MetaTypeHasSeed(metaType MetaType) bool

func MetaTypeSetAlias

func MetaTypeSetAlias(version MetaType, alias string)

func NetworkTypeGetAlias

func NetworkTypeGetAlias(network NetworkType) string

func NetworkTypeIsGroup

func NetworkTypeIsGroup(networkType NetworkType) bool

func NetworkTypeIsUser

func NetworkTypeIsUser(networkType NetworkType) bool

func NetworkTypeSetAlias

func NetworkTypeSetAlias(network NetworkType, alias string)

Types

type Address

type Address interface {
	Stringer

	/**
	 *  get address type
	 *
	 * @return network type
	 */
	Network() NetworkType

	IsUser() bool
	IsGroup() bool
	IsBroadcast() bool
}

*

  • Address for MKM ID
  • ~~~~~~~~~~~~~~~~~~
  • This class is used to build address for ID
var ANYWHERE Address = nil // "anywhere"

Broadcast addresses for User/Group

var EVERYWHERE Address = nil // "everywhere"

func AddressCreate

func AddressCreate(address string) Address

func AddressGenerate

func AddressGenerate(meta Meta, network NetworkType) Address

Factory methods

func AddressParse

func AddressParse(address interface{}) Address

type AddressFactory

type AddressFactory interface {

	/**
	*  Generate address with meta & network
	*
	* @param meta - meta info
	* @param network - address type
	@ @return Address
	*/
	GenerateAddress(meta Meta, network NetworkType) Address

	/**
	 *  Create address from string
	 *
	 * @param address - address string
	 * @return Address
	 */
	CreateAddress(address string) Address

	/**
	 *  Parse string object to address
	 *
	 * @param address - address string
	 * @return Address
	 */
	ParseAddress(address string) Address
}

*

  • Address Factory
  • ~~~~~~~~~~~~~~~

func AddressGetFactory

func AddressGetFactory() AddressFactory

type Bulletin

type Bulletin interface {
	Document

	/**
	 *  Get group assistants
	 *
	 * @return bot ID list
	 */
	Assistants() []ID

	/**
	 *  Set group assistants
	 *
	 * @param assistants - bot ID list
	 */
	SetAssistants(assistants []ID)
}

*

  • Group Document
  • ~~~~~~~~~~~~~~

type Document

type Document interface {
	Mapper
	TAI

	/**
	 *  Get document type
	 *
	 * @return document type
	 */
	Type() string

	/**
	 *  Get entity ID
	 *
	 * @return entity ID
	 */
	ID() ID

	/**
	 * Get sign time
	 */
	Time() Time

	/**
	 *  Get entity name
	 *
	 * @return name string
	 */
	Name() string

	/**
	 *  Set entity name
	 *
	 * @param name - nickname of user; title of group
	 */
	SetName(name string)
}

*

  • User/Group Profile
  • ~~~~~~~~~~~~~~~~~~
  • This class is used to generate entity profile *
  • data format: {
  • ID: "EntityID", // entity ID
  • data: "{JSON}", // data = json_encode(info)
  • signature: "..." // signature = sign(data, SK);
  • }

func DocumentCreate

func DocumentCreate(docType string, identifier ID, data string, signature string) Document

Factory methods

func DocumentParse

func DocumentParse(doc interface{}) Document

type DocumentFactory

type DocumentFactory interface {

	/**
	 *  Create document with data & signature loaded from local storage
	 *  (If data & signature empty, create a new empty document with entity ID)
	 *
	 * @param identifier - entity ID
	 * @param data       - document data (JsON)
	 * @param signature  - document signature (Base64)
	 * @return Document
	 */
	CreateDocument(identifier ID, data string, signature string) Document

	/**
	 *  Parse map object to entity document
	 *
	 * @param doc - info
	 * @return Document
	 */
	ParseDocument(doc map[string]interface{}) Document
}

*

  • Document Factory
  • ~~~~~~~~~~~~~~~~

func DocumentGetFactory

func DocumentGetFactory(docType string) DocumentFactory

type ID

type ID interface {
	Stringer

	Name() string
	Address() Address
	Terminal() string

	/**
	 *  get ID type
	 *
	 * @return network type
	 */
	Type() NetworkType

	IsUser() bool
	IsGroup() bool
	IsBroadcast() bool
}

*

  • ID for entity (User/Group) *
  • data format: "name@address[/terminal]" *
  • fields:
  • name - entity name, the seed of fingerprint to build address
  • address - a string to identify an entity
  • terminal - entity login resource(device), OPTIONAL
var ANYONE ID = nil // "anyone@anywhere"
var EVERYONE ID = nil // "everyone@everywhere"
var FOUNDER ID = nil // "moky@anywhere"

func DocumentGetID

func DocumentGetID(doc map[string]interface{}) ID

func IDConvert

func IDConvert(members interface{}) []ID

func IDCreate

func IDCreate(name string, address Address, terminal string) ID

func IDGenerate

func IDGenerate(meta Meta, network NetworkType, terminal string) ID

Factory methods

func IDParse

func IDParse(identifier interface{}) ID

type IDFactory

type IDFactory interface {

	/**
	 *  Generate ID
	 *
	 * @param meta - meta info
	 * @param network - ID.type
	 * @param terminal - ID.terminal
	 * @return ID
	 */
	GenerateID(meta Meta, network NetworkType, terminal string) ID

	/**
	 *  Create ID
	 *
	 * @param name     - ID.name
	 * @param address  - ID.address
	 * @param terminal - ID.terminal
	 * @return ID
	 */
	CreateID(name string, address Address, terminal string) ID

	/**
	 *  Parse string object to ID
	 *
	 * @param identifier - ID string
	 * @return ID
	 */
	ParseID(identifier string) ID
}

*

  • ID Factory
  • ~~~~~~~~~~

func IDGetFactory

func IDGetFactory() IDFactory

type Meta

type Meta interface {
	Mapper

	/**
	 *  Meta algorithm version
	 *
	 *      0x01 - username@address
	 *      0x02 - btc_address
	 *      0x03 - username@btc_address
	 */
	Type() MetaType

	/**
	 *  Public key (used for signature)
	 *
	 *      RSA / ECC
	 */
	Key() VerifyKey

	/**
	 *  Seed to generate fingerprint
	 *
	 *      Username / Group-X
	 */
	Seed() string

	/**
	 *  Fingerprint to verify ID and public key
	 *
	 *      Build: fingerprint = sign(seed, privateKey)
	 *      Check: verify(seed, fingerprint, publicKey)
	 */
	Fingerprint() []byte

	/**
	 *  Generate address with network(type)
	 *
	 * @param network - ID.type
	 * @return Address
	 */
	GenerateAddress(network NetworkType) Address
}

*

  • User/Group Meta data
  • ~~~~~~~~~~~~~~~~~~~~
  • This class is used to generate entity ID *
  • data format: {
  • version: 1, // algorithm version
  • key: {public key}, // PK = secp256k1(SK);
  • seed: "moKy", // user/group name
  • fingerprint: "..." // CT = sign(seed, SK);
  • } *
  • algorithm:
  • fingerprint = sign(seed, SK);

func MetaCreate

func MetaCreate(version MetaType, key VerifyKey, seed string, fingerprint []byte) Meta

Factory methods

func MetaGenerate

func MetaGenerate(version MetaType, sKey SignKey, seed string) Meta

func MetaParse

func MetaParse(meta interface{}) Meta

type MetaFactory

type MetaFactory interface {

	/**
	 *  Create meta
	 *
	 * @param key         - public key
	 * @param seed        - ID.name
	 * @param fingerprint - sKey.sign(seed)
	 * @return Meta
	 */
	CreateMeta(key VerifyKey, seed string, fingerprint []byte) Meta

	/**
	 *  Generate meta
	 *
	 * @param sKey    - private key
	 * @param seed    - ID.name
	 * @return Meta
	 */
	GenerateMeta(sKey SignKey, seed string) Meta

	/**
	 *  Parse map object to meta
	 *
	 * @param meta - meta info
	 * @return Meta
	 */
	ParseMeta(meta map[string]interface{}) Meta
}

*

  • Meta Factory
  • ~~~~~~~~~~~~

func MetaGetFactory

func MetaGetFactory(version MetaType) MetaFactory

type MetaType

type MetaType uint8

* enum MKMMetaVersion * * @abstract Defined for algorithm that generating address. * * discussion Generate and check ID/Address * * MKMMetaVersion_MKM give a seed string first, and sign this seed to get * fingerprint; after that, use the fingerprint to generate address. * This will get a firmly relationship between (username, address and key). * * MKMMetaVersion_BTC use the key data to generate address directly. * This can build a BTC address for the entity ID (no username). * * MKMMetaVersion_ExBTC use the key data to generate address directly, and * sign the seed to get fingerprint (just for binding username and key). * This can build a BTC address, and bind a username to the entity ID. * * Bits: * 0000 0001 - this meta contains seed as ID.name * 0000 0010 - this meta generate BTC address * 0000 0100 - this meta generate ETH address * ...

const (
	DEFAULT MetaType = 0x01
	MKM     MetaType = 0x01 // 0000 0001

	BTC   MetaType = 0x02 // 0000 0010
	ExBTC MetaType = 0x03 // 0000 0011

	ETH   MetaType = 0x04 // 0000 0100
	ExETH MetaType = 0x05 // 0000 0101
)

func MetaGetType

func MetaGetType(meta map[string]interface{}) MetaType

func MetaTypeParse

func MetaTypeParse(version interface{}) MetaType

func (MetaType) String

func (version MetaType) String() string

type NetworkType

type NetworkType uint8

* @enum MKMNetworkID * * @abstract A network type to indicate what kind the entity is. * * @discussion An address can identify a person, a group of people, * a team, even a thing. * * MKMNetwork_Main indicates this entity is a person's account. * An account should have a public key, which proved by meta data. * * MKMNetwork_Group indicates this entity is a group of people, * which should have a founder (also the owner), and some members. * * MKMNetwork_Moments indicates a special personal social network, * where the owner can share information and interact with its friends. * The owner is the king here, it can do anything and no one can stop it. * * MKMNetwork_Polylogue indicates a virtual (temporary) social network. * It's created to talk with multi-people (but not too much, e.g. less than 100). * Any member can invite people in, but only the founder can expel member. * * MKMNetwork_Chatroom indicates a massive (persistent) social network. * It's usually more than 100 people in it, so we need administrators * to help the owner to manage the group. * * MKMNetwork_SocialEntity indicates this entity is a social entity. * * MKMNetwork_Organization indicates an independent organization. * * MKMNetwork_Company indicates this entity is a company. * * MKMNetwork_School indicates this entity is a school. * * MKMNetwork_Government indicates this entity is a government department. * * MKMNetwork_Department indicates this entity is a department. * * MKMNetwork_Thing this is reserved for IoT (Internet of Things). * * Bits: * 0000 0001 - this entity's branch is independent (clear division). * 0000 0010 - this entity can contains other group (big organization). * 0000 0100 - this entity is top organization. * 0000 1000 - (Main) this entity acts like a human. * * 0001 0000 - this entity contains members (Group) * 0010 0000 - this entity needs other administrators (big organization) * 0100 0000 - this is an entity in reality. * 1000 0000 - (IoT) this entity is a 'Thing'. * * (All above are just some advices to help choosing numbers :P)

const (
	BTCMain NetworkType = 0x00 // 0000 0000

	/*
	 *  Person Account
	 */
	MAIN NetworkType = 0x08 // 0000 1000 (Person)

	/*
	 *  Virtual Groups
	 */
	GROUP NetworkType = 0x10 // 0001 0000 (Multi-Persons)

	//MOMENTS    NetworkType = 0x18  // 0001 1000 (Twitter)
	POLYLOGUE NetworkType = 0x10 // 0001 0000 (Multi-Persons Chat, N < 100)
	CHATROOM  NetworkType = 0x30 // 0011 0000 (Multi-Persons Chat, N >= 100)

	/*
	 *  Network
	 */
	PROVIDER NetworkType = 0x76 // 0111 0110 (Service Provider)
	STATION  NetworkType = 0x88 // 1000 1000 (Server Node)

	/*
	 *  Internet of Things
	 */
	THING NetworkType = 0x80 // 1000 0000 (IoT)
	ROBOT NetworkType = 0xC8 // 1100 1000
)

func NetworkTypeParse

func NetworkTypeParse(network interface{}) NetworkType

func (NetworkType) String

func (network NetworkType) String() string

type TAI

type TAI interface {

	/**
	 *  Check if signature matched
	 *
	 * @return False on signature not matched
	 */
	IsValid() bool

	/**
	 *  Verify 'data' and 'signature' with public key
	 *
	 * @param publicKey - public key as meta.key
	 * @return true on signature matched
	 */
	Verify(publicKey VerifyKey) bool

	/**
	 *  Encode properties to 'data' and sign it to 'signature'
	 *
	 * @param privateKey - private key match meta.key
	 * @return signature
	 */
	Sign(privateKey SignKey) []byte

	/**
	 *  Get all properties
	 *
	 * @return properties
	 */
	Properties() map[string]interface{}

	/**
	 *  Get property data with key
	 *
	 * @param name - property name
	 * @return property data
	 */
	GetProperty(name string) interface{}

	/**
	 *  Update property with key and data
	 *  (this will reset 'data' and 'signature')
	 *
	 * @param name - property name
	 * @param value - property data
	 */
	SetProperty(name string, value interface{})
}

*

  • The Additional Information *
  • 'Meta' is the information for entity which never changed,
  • which contains the key for verify signature;
  • 'TAI' is the variable part,
  • which could contain a public key for asymmetric encryption.

type Visa

type Visa interface {
	Document

	/**
	 *  Get public key to encrypt message for user
	 *
	 * @return public key as visa.key
	 */
	Key() EncryptKey

	/**
	 *  Set public key for other user to encrypt message
	 *
	 * @param publicKey - public key as visa.key
	 */
	SetKey(publicKey EncryptKey)

	/**
	 *  Get avatar URL
	 *
	 * @return URL string
	 */
	Avatar() string

	/**
	 *  Set avatar URL
	 *
	 * @param url - URL string
	 */
	SetAvatar(url string)
}

*

  • User Document
  • ~~~~~~~~~~~~~
  • This interface is defined for authorizing other apps to login,
  • which can generate a temporary asymmetric key pair for messaging.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL