-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: molecule codec #88
Conversation
🦋 Changeset detectedLatest commit: bfbab2c The changes in this PR will be included in the next version bump. This PR includes changesets to release 16 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
packages/core/src/molecule/codec.ts
Outdated
numToBytes, | ||
} from "../num/index.js"; | ||
|
||
export interface Codec<Encodable, Decodable = BytesLike> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export interface Codec<Encodable, Decoded = Encodable> {
byteLength?: number;
encode: (encodable: Encodable) => Bytes;
decode: (decodable: BytesLike) => Decoded;
}
So we can have
const TransactionCodec: Codec<ccc.TransactionLike, ccc.Transaction>;
How do you think? It seems that the original Decodable
is never used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good pattern, but I'm afraid it's not possible to explicitly declare the Decoded as ccc.Transaction, because the generic Decoded cannot recognize the from
method in ccc.Transaction, while we cannot directly use as
to cast ccc.TransactionLike to ccc.Transaction.
so in case of molecule, I think making the Decoded as xxxLike can fit into most of cases, for example, users should manually use ccc.Transaction.from to cast the decoded result of molecule decoders
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good pattern, but I'm afraid it's not possible to explicitly declare the Decoded as ccc.Transaction, because the generic Decoded cannot recognize the
from
method in ccc.Transaction, while we cannot directly useas
to cast ccc.TransactionLike to ccc.Transaction.so in case of molecule, I think making the Decoded as xxxLike can fit into most of cases, for example, users should manually use ccc.Transaction.from to cast the decoded result of molecule decoders
Maybe we can add a method?
Codec<Encodable, Decoded>.map<NewDecoded>(transformer: (decoded: Decoded) => NewDecoded): Codec<Encodable, NewDecoded>
packages/core/src/barrel.ts
Outdated
@@ -6,6 +6,7 @@ export * from "./fixedPoint/index.js"; | |||
export * from "./hasher/index.js"; | |||
export * from "./hex/index.js"; | |||
export * from "./keystore/index.js"; | |||
export * as molecule from "./molecule/index.js"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe mol
for easier typing?
itemCodec.encode(item), | ||
); | ||
return bytesConcat( | ||
Uint32LE.encode(BigInt(userDefinedItems.length)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above, this is why Uint32LE should be Codec<NumLike, Num>
Uint32LE.encode(BigInt(userDefinedItems.length)), | ||
encodedArray.reduce( | ||
(concated, item) => bytesConcat(concated, item), | ||
new ArrayBuffer(0), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bytes
is Uint8Array
, new Uint8Array([])
is more precise or []
is simpler.
const field = keys[i]; | ||
const codec = codecLayout[field]; | ||
const payload = value.slice(start, end); | ||
Object.assign(object, { [field]: codec.decode(payload) }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just object[field] = codec.decode(payload);
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
R extends object = { [key in keyof T]: EncodableType<T[key]> }, | ||
>(codecLayout: T): Codec<R> { | ||
const codecArray = Object.values(codecLayout); | ||
if (codecArray.find((codec) => codec.byteLength === undefined)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Array.some()
refactor: improve types
feat: spore searcher
@Hanssen0 already merged "spore searcher" into this PR, if no more problems, this PR could also be merged. |
#Description
migrate whole @ckb-lumos/codec package into ccc and replace with this new codec of ccc for native Spore package, which test cases have passed