Skip to content

Commit

Permalink
feat: Add escape character encoding (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinoosss authored Feb 6, 2024
1 parent 62f2e7c commit 9c2b1fe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/wallet/utility/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,15 @@ export const defaultAddressPrefix = 'g';
export const stringToUTF8 = (str: string): Uint8Array => {
return new TextEncoder().encode(str);
};

/**
* Escapes <,>,& in string.
* Golang's json marshaller escapes <,>,& by default.
* https://cs.opensource.google/go/go/+/refs/tags/go1.20.6:src/encoding/json/encode.go;l=46-53
*/
export function encodeCharacterSet(data: string) {
return data
.replace(/</g, '\\u003c')
.replace(/>/g, '\\u003e')
.replace(/&/g, '\\u0026');
}
9 changes: 7 additions & 2 deletions src/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import { Signer } from './signer';
import { LedgerSigner } from './ledger';
import { KeySigner } from './key';
import { Secp256k1 } from '@cosmjs/crypto';
import { generateEntropy, generateKeyPair, stringToUTF8 } from './utility';
import {
encodeCharacterSet,
generateEntropy,
generateKeyPair,
stringToUTF8,
} from './utility';
import { LedgerConnector } from '@cosmjs/ledger-amino';
import { entropyToMnemonic } from '@cosmjs/crypto/build/bip39';
import { Any, PubKeySecp256k1, Tx, TxSignature } from '../proto';
Expand Down Expand Up @@ -271,7 +276,7 @@ export class Wallet {
// a sorted JSON object, so the payload needs to be sorted
// before signing
const signBytes: Uint8Array = stringToUTF8(
sortedJsonStringify(signPayload)
encodeCharacterSet(sortedJsonStringify(signPayload))
);

// The public key needs to be encoded using protobuf for Amino
Expand Down

0 comments on commit 9c2b1fe

Please sign in to comment.