UNPKG

byte-encodings

Version:

Utilities for encoding and decoding common formats like hex, base64, and varint. Ported from Deno's @std/encoding.

2 lines (1 loc) 2.19 kB
var d=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var w=(t,e)=>{for(var n in e)d(t,n,{get:e[n],enumerable:!0})},U=(t,e,n,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of m(e))!A.call(t,r)&&r!==n&&d(t,r,{get:()=>e[r],enumerable:!(i=g(e,r))||i.enumerable});return t};var b=t=>U(d({},"__esModule",{value:!0}),t);var T={};w(T,{decodeBase58:()=>v,encodeBase58:()=>k});module.exports=b(T);var x=new TextEncoder;function B(t){var n;let e=typeof t;return e!=="object"?e:t===null?"null":((n=t==null?void 0:t.constructor)==null?void 0:n.name)??"object"}function h(t){if(typeof t=="string")return x.encode(t);if(t instanceof Uint8Array)return t;if(t instanceof ArrayBuffer)return new Uint8Array(t);throw new TypeError(`Cannot validate the input as it must be a Uint8Array, a string, or an ArrayBuffer: received a value of the type ${B(t)}`)}var E={1:0,2:1,3:2,4:3,5:4,6:5,7:6,8:7,9:8,A:9,B:10,C:11,D:12,E:13,F:14,G:15,H:16,J:17,K:18,L:19,M:20,N:21,P:22,Q:23,R:24,S:25,T:26,U:27,V:28,W:29,X:30,Y:31,Z:32,a:33,b:34,c:35,d:36,e:37,f:38,g:39,h:40,i:41,j:42,k:43,m:44,n:45,o:46,p:47,q:48,r:49,s:50,t:51,u:52,v:53,w:54,x:55,y:56,z:57},M="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".split("");function k(t){let e=h(t),n=0,i=0,r=0;for(;e[r]===0;)i++,r++;let y=e.slice(r),p=Math.round(e.length*138/100+1),s=[];y.forEach(a=>{let c=0,o=a;for(let l=p-1;(o>0||c<n)&&l!==-1;l--,c++)o+=(s[l]??0)*256,s[l]=Math.round(o%58),o=Math.floor(o/58);n=c});let f=Array.from({length:s.length+i});return i>0&&f.fill("1",0,i),s.forEach(a=>f.push(M[a])),f.join("")}function v(t){let e=t.trim().split(""),n=0,i=0,r=0;for(;e[r]==="1";)i++,r++;let y=e.slice(r),p=Math.round(t.length*733/1e3+1),s=[];y.forEach((a,c)=>{let o=E[a],l=0;if(o===void 0)throw new TypeError(`Invalid base58 char at index ${c} with value ${a}`);for(let u=p-1;(o>0||l<n)&&u!==-1;u--,l++)o+=58*(s[u]??0),s[u]=Math.round(o%256),o=Math.floor(o/256);n=l});let f=s.filter(a=>a!==void 0);if(i>0){let a=Array.from({length:i}).fill(0,0,i);return new Uint8Array([...a,...f])}return new Uint8Array(f)}0&&(module.exports={decodeBase58,encodeBase58});