@metaphi/airwallet-api
Version:
Metaphi Airwallet API to add whitelabel, non-custodial wallets to dApps
74 lines (66 loc) • 2.38 kB
JavaScript
// Source: https://github.com/MetaMask/eth-sig-util/blob/8f5a90bed37e6891fe4e9ab98a8cd4f62188d5c4/src/utils.ts
import {
addHexPrefix,
bufferToInt,
fromSigned,
toUnsigned,
} from "ethereumjs-util";
import { intToHex, isHexString, stripHexPrefix } from "ethjs-util";
/**
* Pads the front of the given hex string with zeroes until it reaches the
* target length. If the input string is already longer than or equal to the
* target length, it is returned unmodified.
*
* If the input string is "0x"-prefixed or not a hex string, an error will be
* thrown.
*
* @param hexString - The hexadecimal string to pad with zeroes.
* @param targetLength - The target length of the hexadecimal string.
* @returns The input string front-padded with zeroes, or the original string
* if it was already greater than or equal to to the target length.
*/
export function padWithZeroes(hexString, targetLength) {
if (hexString !== "" && !/^[a-f0-9]+$/iu.test(hexString)) {
throw new Error(
`Expected an unprefixed hex string. Received: ${hexString}`
);
}
if (targetLength < 0) {
throw new Error(
`Expected a non-negative integer target length. Received: ${targetLength}`
);
}
return String.prototype.padStart.call(hexString, targetLength, "0");
}
/**
* Concatenate an extended ECDSA signature into a single '0x'-prefixed hex string.
*
* @param v - The 'v' portion of the signature.
* @param r - The 'r' portion of the signature.
* @param s - The 's' portion of the signature.
* @returns The concatenated ECDSA signature as a '0x'-prefixed string.
*/
export function concatSig(v, r, s) {
const rSig = fromSigned(r);
const sSig = fromSigned(s);
const vSig = bufferToInt(v);
const rStr = padWithZeroes(toUnsigned(rSig).toString("hex"), 64);
const sStr = padWithZeroes(toUnsigned(sSig).toString("hex"), 64);
const vStr = stripHexPrefix(intToHex(vSig));
return addHexPrefix(rStr.concat(sStr, vStr));
}
// Source: https://stackoverflow.com/a/2117523/3545099
export function uuidv4() {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) =>
(
c ^
(crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))
).toString(16),
);
}
export function getMetaphiIframeDomain (env) {
if (env === 'development') {
return 'http://localhost:8080'
}
return 'https://www.metaphi.xyz'
}