mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-04 18:31:41 +00:00
feature/1121 implement ucan validation (#1176)
- **refactor: remove unused auth components** - **refactor: improve devbox configuration and deployment process** - **refactor: improve devnet and testnet setup** - **fix: update templ version to v0.2.778** - **refactor: rename pkl/net.matrix to pkl/matrix.net** - **refactor: migrate webapp components to nebula** - **refactor: protobuf types** - **chore: update dependencies for improved security and stability** - **feat: implement landing page and vault gateway servers** - **refactor: Migrate data models to new module structure and update related files** - **feature/1121-implement-ucan-validation** - **refactor: Replace hardcoded constants with model types in attns.go** - **feature/1121-implement-ucan-validation** - **chore: add origin Host struct and update main function to handle multiple hosts** - **build: remove unused static files from dwn module** - **build: remove unused static files from dwn module** - **refactor: Move DWN models to common package** - **refactor: move models to pkg/common** - **refactor: move vault web app assets to embed module** - **refactor: update session middleware import path** - **chore: configure port labels and auto-forwarding behavior** - **feat: enhance devcontainer configuration** - **feat: Add UCAN middleware for Echo with flexible token validation** - **feat: add JWT middleware for UCAN authentication** - **refactor: update package URI and versioning in PklProject files** - **fix: correct sonr.pkl import path** - **refactor: move JWT related code to auth package** - **feat: introduce vault configuration retrieval and management** - **refactor: Move vault components to gateway module and update file paths** - **refactor: remove Dexie and SQLite database implementations** - **feat: enhance frontend with PWA features and WASM integration** - **feat: add Devbox features and streamline Dockerfile** - **chore: update dependencies to include TigerBeetle** - **chore(deps): update go version to 1.23** - **feat: enhance devnet setup with PATH environment variable and updated PWA manifest** - **fix: upgrade tigerbeetle-go dependency and remove indirect dependency** - **feat: add PostgreSQL support to devnet and testnet deployments** - **refactor: rename keyshare cookie to token cookie** - **feat: upgrade Go version to 1.23.3 and update dependencies** - **refactor: update devnet and testnet configurations** - **feat: add IPFS configuration for devnet** - **I'll help you update the ipfs.config.pkl to include all the peers from the shell script. Here's the updated configuration:** - **refactor: move mpc package to crypto directory** - **feat: add BIP32 support for various cryptocurrencies** - **feat: enhance ATN.pkl with additional capabilities** - **refactor: simplify smart account and vault attenuation creation** - **feat: add new capabilities to the Attenuation type** - **refactor: Rename MPC files for clarity and consistency** - **feat: add DIDKey support for cryptographic operations** - **feat: add devnet and testnet deployment configurations** - **fix: correct key derivation in bip32 package** - **refactor: rename crypto/bip32 package to crypto/accaddr** - **fix: remove duplicate indirect dependency** - **refactor: move vault package to root directory** - **refactor: update routes for gateway and vault** - **refactor: remove obsolete web configuration file** - **refactor: remove unused TigerBeetle imports and update host configuration** - **refactor: adjust styles directory path** - **feat: add broadcastTx and simulateTx functions to gateway** - **feat: add PinVault handler**
This commit is contained in:
Executable
+232
@@ -0,0 +1,232 @@
|
||||
// Copyright (c) 2014 Dropbox, Inc.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this
|
||||
// list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
// this list of conditions and the following disclaimer in the documentation
|
||||
// and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors
|
||||
// may be used to endorse or promote products derived from this software without
|
||||
// specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package mina
|
||||
|
||||
// A BitVector is a variable sized vector of bits. It supports
|
||||
// lookups, sets, appends, insertions, and deletions.
|
||||
//
|
||||
// This class is not thread safe.
|
||||
type BitVector struct {
|
||||
data []byte
|
||||
length int
|
||||
}
|
||||
|
||||
// NewBitVector creates and initializes a new bit vector with length
|
||||
// elements, using data as its initial contents.
|
||||
func NewBitVector(data []byte, length int) *BitVector {
|
||||
return &BitVector{
|
||||
data: data,
|
||||
length: length,
|
||||
}
|
||||
}
|
||||
|
||||
// Bytes returns a slice of the contents of the bit vector. If the caller changes the returned slice,
|
||||
// the contents of the bit vector may change.
|
||||
func (vector *BitVector) Bytes() []byte {
|
||||
return vector.data
|
||||
}
|
||||
|
||||
// Length returns the current number of elements in the bit vector.
|
||||
func (vector *BitVector) Length() int {
|
||||
return vector.length
|
||||
}
|
||||
|
||||
// This function shifts a byte slice one bit lower (less significant).
|
||||
// bit (either 1 or 0) contains the bit to put in the most significant
|
||||
// position of the last byte in the slice.
|
||||
// This returns the bit that was shifted off of the last byte.
|
||||
func shiftLower(bit byte, b []byte) byte {
|
||||
bit = bit << 7
|
||||
for i := len(b) - 1; i >= 0; i-- {
|
||||
newByte := b[i] >> 1
|
||||
newByte |= bit
|
||||
bit = (b[i] & 1) << 7
|
||||
b[i] = newByte
|
||||
}
|
||||
return bit >> 7
|
||||
}
|
||||
|
||||
// This function shifts a byte slice one bit higher (more significant).
|
||||
// bit (either 1 or 0) contains the bit to put in the least significant
|
||||
// position of the first byte in the slice.
|
||||
// This returns the bit that was shifted off the last byte.
|
||||
func shiftHigher(bit byte, b []byte) byte {
|
||||
for i := 0; i < len(b); i++ {
|
||||
newByte := b[i] << 1
|
||||
newByte |= bit
|
||||
bit = (b[i] & 0x80) >> 7
|
||||
b[i] = newByte
|
||||
}
|
||||
return bit
|
||||
}
|
||||
|
||||
// Returns the minimum number of bytes needed for storing the bit vector.
|
||||
func (vector *BitVector) bytesLength() int {
|
||||
lastBitIndex := vector.length - 1
|
||||
lastByteIndex := lastBitIndex >> 3
|
||||
return lastByteIndex + 1
|
||||
}
|
||||
|
||||
// Panics if the given index is not within the bounds of the bit vector.
|
||||
func (vector *BitVector) indexAssert(i int) {
|
||||
if i < 0 || i >= vector.length {
|
||||
panic("Attempted to access element outside buffer")
|
||||
}
|
||||
}
|
||||
|
||||
// Append adds a bit to the end of a bit vector.
|
||||
func (vector *BitVector) Append(bit byte) {
|
||||
index := uint32(vector.length)
|
||||
vector.length++
|
||||
|
||||
if vector.bytesLength() > len(vector.data) {
|
||||
vector.data = append(vector.data, 0)
|
||||
}
|
||||
|
||||
byteIndex := index >> 3
|
||||
byteOffset := index % 8
|
||||
oldByte := vector.data[byteIndex]
|
||||
var newByte byte
|
||||
if bit == 1 {
|
||||
newByte = oldByte | 1<<byteOffset
|
||||
} else {
|
||||
// Set all bits except the byteOffset
|
||||
mask := byte(^(1 << byteOffset))
|
||||
newByte = oldByte & mask
|
||||
}
|
||||
|
||||
vector.data[byteIndex] = newByte
|
||||
}
|
||||
|
||||
// Element returns the bit in the ith index of the bit vector.
|
||||
// Returned value is either 1 or 0.
|
||||
func (vector *BitVector) Element(i int) byte {
|
||||
vector.indexAssert(i)
|
||||
byteIndex := i >> 3
|
||||
byteOffset := uint32(i % 8)
|
||||
b := vector.data[byteIndex]
|
||||
// Check the offset bit
|
||||
return (b >> byteOffset) & 1
|
||||
}
|
||||
|
||||
// Set changes the bit in the ith index of the bit vector to the value specified in
|
||||
// bit.
|
||||
func (vector *BitVector) Set(bit byte, index int) {
|
||||
vector.indexAssert(index)
|
||||
byteIndex := uint32(index >> 3)
|
||||
byteOffset := uint32(index % 8)
|
||||
|
||||
oldByte := vector.data[byteIndex]
|
||||
|
||||
var newByte byte
|
||||
if bit == 1 {
|
||||
// turn on the byteOffset'th bit
|
||||
newByte = oldByte | 1<<byteOffset
|
||||
} else {
|
||||
// turn off the byteOffset'th bit
|
||||
removeMask := byte(^(1 << byteOffset))
|
||||
newByte = oldByte & removeMask
|
||||
}
|
||||
vector.data[byteIndex] = newByte
|
||||
}
|
||||
|
||||
// Insert inserts bit into the supplied index of the bit vector. All
|
||||
// bits in positions greater than or equal to index before the call will
|
||||
// be shifted up by one.
|
||||
func (vector *BitVector) Insert(bit byte, index int) {
|
||||
vector.indexAssert(index)
|
||||
vector.length++
|
||||
|
||||
// Append an additional byte if necessary.
|
||||
if vector.bytesLength() > len(vector.data) {
|
||||
vector.data = append(vector.data, 0)
|
||||
}
|
||||
|
||||
byteIndex := uint32(index >> 3)
|
||||
byteOffset := uint32(index % 8)
|
||||
var bitToInsert byte
|
||||
if bit == 1 {
|
||||
bitToInsert = 1 << byteOffset
|
||||
}
|
||||
|
||||
oldByte := vector.data[byteIndex]
|
||||
// This bit will need to be shifted into the next byte
|
||||
leftoverBit := (oldByte & 0x80) >> 7
|
||||
// Make masks to pull off the bits below and above byteOffset
|
||||
// This mask has the byteOffset lowest bits set.
|
||||
bottomMask := byte((1 << byteOffset) - 1)
|
||||
// This mask has the 8 - byteOffset top bits set.
|
||||
topMask := ^bottomMask
|
||||
top := (oldByte & topMask) << 1
|
||||
newByte := bitToInsert | (oldByte & bottomMask) | top
|
||||
|
||||
vector.data[byteIndex] = newByte
|
||||
// Shift the rest of the bytes in the slice one higher, append
|
||||
// the leftoverBit obtained above.
|
||||
shiftHigher(leftoverBit, vector.data[byteIndex+1:])
|
||||
}
|
||||
|
||||
// Delete removes the bit in the supplied index of the bit vector. All
|
||||
// bits in positions greater than or equal to index before the call will
|
||||
// be shifted down by one.
|
||||
func (vector *BitVector) Delete(index int) {
|
||||
vector.indexAssert(index)
|
||||
vector.length--
|
||||
byteIndex := uint32(index >> 3)
|
||||
byteOffset := uint32(index % 8)
|
||||
|
||||
oldByte := vector.data[byteIndex]
|
||||
|
||||
// Shift all the bytes above the byte we're modifying, return the
|
||||
// leftover bit to include in the byte we're modifying.
|
||||
bit := shiftLower(0, vector.data[byteIndex+1:])
|
||||
|
||||
// Modify oldByte.
|
||||
// At a high level, we want to select the bits above byteOffset,
|
||||
// and shift them down by one, removing the bit at byteOffset.
|
||||
|
||||
// This selects the bottom bits
|
||||
bottomMask := byte((1 << byteOffset) - 1)
|
||||
// This selects the top (8 - byteOffset - 1) bits
|
||||
topMask := byte(^((1 << (byteOffset + 1)) - 1))
|
||||
// newTop is the top bits, shifted down one, combined with the leftover bit from shifting
|
||||
// the other bytes.
|
||||
newTop := (oldByte&topMask)>>1 | (bit << 7)
|
||||
// newByte takes the bottom bits and combines with the new top.
|
||||
newByte := (bottomMask & oldByte) | newTop
|
||||
vector.data[byteIndex] = newByte
|
||||
|
||||
// The desired length is the byte index of the last element plus one,
|
||||
// where the byte index of the last element is the bit index of the last
|
||||
// element divided by 8.
|
||||
byteLength := vector.bytesLength()
|
||||
if byteLength < len(vector.data) {
|
||||
vector.data = vector.data[:byteLength]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user