-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: encapsulate
set
fields in CborSet
Conwy era CDDL defines [sets](https://github.com/IntersectMBO/cardano-ledger/blob/master/eras/conway/impl/cddl-files/extra.cddl#L5) as mathematical finite sets, encoded in a cbor 258 tag. https://github.com/input-output-hk/cbor-sets-spec/blob/master/CBOR_SETS.md BREAKING_CHANGE: All CBOR serialization classes now use CborSet when constructing, setting or retrieveing fields that are defined by the CDDL as `set/nonempty_set/`. See for example `TransactionBody referenceInputs` which is now a `CborSet<Cardano.TxIn, TransactionInput>`, and this is the type used when calling `referenceInputs() or setReferenceInputs(...)`.
- Loading branch information
1 parent
3fdd115
commit 06269ab
Showing
16 changed files
with
389 additions
and
362 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Cardano } from '../..'; | ||
import { CborReader, CborWriter } from '../CBOR'; | ||
import { Hash28ByteBase16 } from '@cardano-sdk/crypto'; | ||
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util'; | ||
import { hexToBytes } from '../../util/misc'; | ||
|
||
const CREDENTIAL_ARRAY_SIZE = 2; | ||
|
||
export class Credential { | ||
#value: Cardano.Credential; | ||
|
||
private constructor(value: Cardano.Credential) { | ||
this.#value = value; | ||
} | ||
|
||
toCbor() { | ||
const writer = new CborWriter(); | ||
writer.writeStartArray(CREDENTIAL_ARRAY_SIZE); | ||
writer.writeInt(this.#value.type); | ||
writer.writeByteString(hexToBytes(this.#value.hash as unknown as HexBlob)); | ||
return writer.encodeAsHex(); | ||
} | ||
|
||
static fromCbor(cbor: HexBlob): Credential { | ||
const reader = new CborReader(cbor); | ||
if (reader.readStartArray() !== CREDENTIAL_ARRAY_SIZE) | ||
throw new InvalidArgumentError( | ||
'cbor', | ||
`Expected an array of ${CREDENTIAL_ARRAY_SIZE} elements, but got an array of ${length} elements` | ||
); | ||
|
||
const type = Number(reader.readUInt()); | ||
const hash = HexBlob.fromBytes(reader.readByteString()) as unknown as Hash28ByteBase16; | ||
|
||
reader.readEndArray(); | ||
return new Credential({ hash, type }); | ||
} | ||
|
||
toCore(): Cardano.Credential { | ||
return { ...this.#value }; | ||
} | ||
|
||
static fromCore(credential: Cardano.Credential): Credential { | ||
return new Credential({ ...credential }); | ||
} | ||
|
||
value() { | ||
return { ...this.#value }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { CborReader, CborWriter } from '../CBOR'; | ||
import { HexBlob } from '@cardano-sdk/util'; | ||
|
||
export class Hash<T extends string> { | ||
#value: T; | ||
|
||
constructor(value: T) { | ||
this.#value = value; | ||
} | ||
|
||
toCbor() { | ||
const writer = new CborWriter(); | ||
writer.writeByteString(Buffer.from(this.#value, 'hex')); | ||
return writer.encodeAsHex(); | ||
} | ||
|
||
static fromCbor<T extends string>(cbor: HexBlob): Hash<T> { | ||
const reader = new CborReader(cbor); | ||
return new Hash<T>(HexBlob.fromBytes(reader.readByteString()) as unknown as T); | ||
} | ||
|
||
toCore() { | ||
return this.#value; | ||
} | ||
|
||
static fromCore<T extends string>(hash: T): Hash<T> { | ||
return new Hash<T>(hash); | ||
} | ||
|
||
value() { | ||
return this.#value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.