Skip to content

Commit

Permalink
Revert cbor code
Browse files Browse the repository at this point in the history
  • Loading branch information
minibits-cash committed Jul 26, 2024
1 parent c8932a0 commit 8043e5e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 277 deletions.
249 changes: 0 additions & 249 deletions src/cbor.ts

This file was deleted.

44 changes: 16 additions & 28 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AmountPreference, Keys, Proof, Token, TokenEntry, TokenV2 } from './mod
import { TOKEN_PREFIX, TOKEN_VERSION } from './utils/Constants';
import { bytesToHex, hexToBytes } from '@noble/curves/abstract/utils';
import { sha256 } from '@noble/hashes/sha256';
import { decodeCBOR } from './cbor';


function splitAmount(value: number, amountPreference?: Array<AmountPreference>): Array<number> {
const chunks: Array<number> = [];
Expand Down Expand Up @@ -82,9 +82,9 @@ function getEncodedToken(token: Token): string {
* @param token an encoded cashu token (cashuAey...)
* @returns cashu token object
*/
function getDecodedToken(token: string) {
function getDecodedToken(token: string): Token {
// remove prefixes
const uriPrefixes = ['web+cashu://', 'cashu://', 'cashu:', 'cashu'];
const uriPrefixes = ['web+cashu://', 'cashu://', 'cashu:', 'cashuA'];
uriPrefixes.forEach((prefix) => {
if (!token.startsWith(prefix)) {
return;
Expand All @@ -99,32 +99,20 @@ function getDecodedToken(token: string) {
* @returns
*/
function handleTokens(token: string): Token {
const version = token.slice(0, 1);
const encodedToken = token.slice(1);
if (version === 'A') {
return encodeBase64ToJson<Token>(encodedToken);
} else if (version === 'B') {
const uInt8Token = encodeBase64toUint8(encodedToken);
const tokenData = decodeCBOR(uInt8Token) as {
t: { p: { a: number; s: string; c: Uint8Array }[]; i: Uint8Array }[];
m: string;
d: string;
};
const mergedTokenEntry: TokenEntry = { mint: tokenData.m, proofs: [] };
tokenData.t.forEach((tokenEntry) =>
tokenEntry.p.forEach((p) => {
mergedTokenEntry.proofs.push({
secret: p.s,
C: bytesToHex(p.c),
amount: p.a,
id: bytesToHex(tokenEntry.i)
});
})
);
return { token: [mergedTokenEntry], memo: tokenData.d || '' };
} else {
throw new Error('Token version is not supported');
const obj = encodeBase64ToJson<TokenV2 | Array<Proof> | Token>(token);

// check if v3
if ('token' in obj) {
return obj;
}

// check if v1
if (Array.isArray(obj)) {
return { token: [{ proofs: obj, mint: '' }] };
}

// if v2 token return v3 format
return { token: [{ proofs: obj.proofs, mint: obj?.mints[0]?.url ?? '' }] };
}
/**
* Returns the keyset id of a set of keys
Expand Down

0 comments on commit 8043e5e

Please sign in to comment.