Skip to content
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
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
getEncodedTokenV4,
getDecodedToken,
deriveKeysetId,
decodePaymentRequest
decodePaymentRequest,
rawTokenToToken,
tokenToRawToken
} from './utils.js';

export * from './model/types/index.js';
Expand All @@ -22,5 +24,7 @@ export {
getEncodedTokenV4,
decodePaymentRequest,
deriveKeysetId,
setGlobalRequestOptions
setGlobalRequestOptions,
rawTokenToToken,
tokenToRawToken
};
79 changes: 51 additions & 28 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -521,3 +531,16 @@ export function hasValidDleq(proof: Proof, keyset: MintKeys): boolean {

return true;
}

export function tokenToRawToken(token: string | Token): Uint8Array {
Copy link
Collaborator

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?

if (typeof token === 'string') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer this to only accept token and outsource conversion to the implementer. I think the readability improves if functions only do one (or close to one) thing.

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);
}
30 changes: 29 additions & 1 deletion test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
constructProofFromPromise,
serializeProof
} from '@cashu/crypto/modules/client';
import { Keys, Proof } from '../src/model/types/index.js';
import { Keys, Proof, Token } from '../src/model/types/index.js';
import * as utils from '../src/utils.js';
import { PUBKEYS } from './consts.js';
import { createDLEQProof } from '@cashu/crypto/modules/mint/NUT12';
Expand Down Expand Up @@ -414,3 +414,31 @@ describe('test zero-knowledge utilities', () => {
expect(exc).toEqual(new Error('undefined key for amount 1'));
});
});

describe('test raw tokens', () => {
test('raw token from token', () => {
const token: Token = {
mint: 'https://example.com/mint',
proofs: [
{
id: '009a1f293253e41e',
amount: 1,
secret: 'acc12435e7b8484c3cf1850149218af90f716a52bf4a5ed347e48ecc13f77388',
C: '0244538319de485d55bed3b29a642bee5879375ab9e7a620e11e48ba482421f3cf'
}
],
memo: 'memo',
unit: 'sat'
};
const tokenHex =
'a4616d781868747470733a2f2f6578616d706c652e636f6d2f6d696e74617563736174617481a2616948009a1f293253e41e617081a36161016173784061636331323433356537623834383463336366313835303134393231386166393066373136613532626634613565643334376534386563633133663737333838616358210244538319de485d55bed3b29a642bee5879375ab9e7a620e11e48ba482421f3cf6164646d656d6f';

const rawToken = utils.tokenToRawToken(token);
const rawTokenHex = bytesToHex(rawToken);
expect(rawTokenHex).toBe(tokenHex);

const tokenBytes = hexToBytes(tokenHex);
const decodedToken = utils.rawTokenToToken(tokenBytes);
expect(decodedToken).toEqual(token);
});
});
Loading