-
Notifications
You must be signed in to change notification settings - Fork 34
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
raw token converters #207
Open
gandlafbtc
wants to merge
1
commit into
development
Choose a base branch
from
raw-token-utils
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
raw token converters #207
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -228,6 +228,17 @@ export function getEncodedTokenV4(token: Token): string { | |
if (nonHex) { | ||
throw new Error('can not encode to v4 token if proofs contain non-hex keyset id'); | ||
} | ||
|
||
const tokenTemplate = templateFromToken(token); | ||
|
||
const encodedData = encodeCBOR(tokenTemplate); | ||
const prefix = 'cashu'; | ||
const version = 'B'; | ||
const base64Data = encodeUint8toBase64Url(encodedData); | ||
return prefix + version + base64Data; | ||
} | ||
|
||
function templateFromToken(token: Token): TokenV4Template { | ||
const idMap: { [id: string]: Array<Proof> } = {}; | ||
const mint = token.mint; | ||
for (let i = 0; i < token.proofs.length; i++) { | ||
|
@@ -261,16 +272,36 @@ export function getEncodedTokenV4(token: Token): string { | |
}) | ||
) | ||
} as TokenV4Template; | ||
|
||
if (token.memo) { | ||
tokenTemplate.d = token.memo; | ||
} | ||
return tokenTemplate; | ||
} | ||
|
||
const encodedData = encodeCBOR(tokenTemplate); | ||
const prefix = 'cashu'; | ||
const version = 'B'; | ||
const base64Data = encodeUint8toBase64Url(encodedData); | ||
return prefix + version + base64Data; | ||
function tokenFromTemplate(template: TokenV4Template): Token { | ||
const proofs: Array<Proof> = []; | ||
template.t.forEach((t) => | ||
t.p.forEach((p) => { | ||
proofs.push({ | ||
secret: p.s, | ||
C: bytesToHex(p.c), | ||
amount: p.a, | ||
id: bytesToHex(t.i), | ||
...(p.d && { | ||
dleq: { | ||
r: bytesToHex(p.d.r), | ||
s: bytesToHex(p.d.s), | ||
e: bytesToHex(p.d.e) | ||
} as SerializedDLEQ | ||
}) | ||
}); | ||
}) | ||
); | ||
const decodedToken: Token = { mint: template.m, proofs, unit: template.u || 'sat' }; | ||
if (template.d) { | ||
decodedToken.memo = template.d; | ||
} | ||
return decodedToken; | ||
} | ||
|
||
/** | ||
|
@@ -316,28 +347,7 @@ export function handleTokens(token: string): Token { | |
} else if (version === 'B') { | ||
const uInt8Token = encodeBase64toUint8(encodedToken); | ||
const tokenData = decodeCBOR(uInt8Token) as TokenV4Template; | ||
const proofs: Array<Proof> = []; | ||
tokenData.t.forEach((t) => | ||
t.p.forEach((p) => { | ||
proofs.push({ | ||
secret: p.s, | ||
C: bytesToHex(p.c), | ||
amount: p.a, | ||
id: bytesToHex(t.i), | ||
...(p.d && { | ||
dleq: { | ||
r: bytesToHex(p.d.r), | ||
s: bytesToHex(p.d.s), | ||
e: bytesToHex(p.d.e) | ||
} as SerializedDLEQ | ||
}) | ||
}); | ||
}) | ||
); | ||
const decodedToken: Token = { mint: tokenData.m, proofs, unit: tokenData.u || 'sat' }; | ||
if (tokenData.d) { | ||
decodedToken.memo = tokenData.d; | ||
} | ||
const decodedToken = tokenFromTemplate(tokenData); | ||
return decodedToken; | ||
} | ||
throw new Error('Token version is not supported'); | ||
|
@@ -521,3 +531,16 @@ export function hasValidDleq(proof: Proof, keyset: MintKeys): boolean { | |
|
||
return true; | ||
} | ||
|
||
export function tokenToRawToken(token: string | Token): Uint8Array { | ||
if (typeof token === 'string') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer this to only accept token and |
||
token = getDecodedToken(token); | ||
} | ||
const template = templateFromToken(token); | ||
return encodeCBOR(template); | ||
} | ||
|
||
export function rawTokenToToken(bytes: Uint8Array): Token { | ||
const decoded = decodeCBOR(bytes) as TokenV4Template; | ||
return tokenFromTemplate(decoded); | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This feels like bait :P
I think we should come up with a proper name for this. We call the the serialised version TokenV4 and the object notation TokenV4Template, so maybe this can be TokenV4Binary?